/core/fields/select.php

  1. <?php 
  2.  
  3. class acf_field_select extends acf_field 
  4. /** 
  5. * __construct 
  6. * 
  7. * Set name / label needed for actions / filters 
  8. * 
  9. * @since 3.6 
  10. * @date 23/01/13 
  11. */ 
  12.  
  13. function __construct() 
  14. // vars 
  15. $this->name = 'select'; 
  16. $this->label = __("Select", 'acf'); 
  17. $this->category = __("Choice", 'acf'); 
  18. $this->defaults = array( 
  19. 'multiple' => 0,  
  20. 'allow_null' => 0,  
  21. 'choices' => array(),  
  22. 'default_value' => '' 
  23. ); 
  24.  
  25.  
  26. // do not delete! 
  27. parent::__construct(); 
  28.  
  29.  
  30. // extra 
  31. //add_filter('acf/update_field/type=select', array($this, 'update_field'), 5, 2); 
  32. add_filter('acf/update_field/type=checkbox', array($this, 'update_field'), 5, 2); 
  33. add_filter('acf/update_field/type=radio', array($this, 'update_field'), 5, 2); 
  34.  
  35.  
  36. /** 
  37. * create_field() 
  38. * 
  39. * Create the HTML interface for your field 
  40. * 
  41. * @param $field - an array holding all the field's data 
  42. * 
  43. * @type action 
  44. * @since 3.6 
  45. * @date 23/01/13 
  46. */ 
  47.  
  48. function create_field( $field ) 
  49. // vars 
  50. $optgroup = false; 
  51.  
  52.  
  53. // determin if choices are grouped (2 levels of array) 
  54. if( is_array($field['choices']) ) 
  55. foreach( $field['choices'] as $k => $v ) 
  56. if( is_array($v) ) 
  57. $optgroup = true; 
  58.  
  59.  
  60. // value must be array 
  61. if( !is_array($field['value']) ) 
  62. // perhaps this is a default value with new lines in it? 
  63. if( strpos($field['value'], "\n") !== false ) 
  64. // found multiple lines, explode it 
  65. $field['value'] = explode("\n", $field['value']); 
  66. else 
  67. $field['value'] = array( $field['value'] ); 
  68.  
  69.  
  70. // trim value 
  71. $field['value'] = array_map('trim', $field['value']); 
  72.  
  73.  
  74. // multiple select 
  75. $multiple = ''; 
  76. if( $field['multiple'] ) 
  77. // create a hidden field to allow for no selections 
  78. echo '<input type="hidden" name="' . $field['name'] . '" />'; 
  79.  
  80. $multiple = ' multiple="multiple" size="5" '; 
  81. $field['name'] .= '[]'; 
  82. }  
  83.  
  84.  
  85. // html 
  86. echo '<select id="' . $field['id'] . '" class="' . $field['class'] . '" name="' . $field['name'] . '" ' . $multiple . ' >';  
  87.  
  88.  
  89. // null 
  90. if( $field['allow_null'] ) 
  91. echo '<option value="null">- ' . __("Select", 'acf') . ' -</option>'; 
  92.  
  93. // loop through values and add them as options 
  94. if( is_array($field['choices']) ) 
  95. foreach( $field['choices'] as $key => $value ) 
  96. if( $optgroup ) 
  97. // this select is grouped with optgroup 
  98. if($key != '') echo '<optgroup label="'.$key.'">'; 
  99.  
  100. if( is_array($value) ) 
  101. foreach($value as $id => $label) 
  102. $selected = in_array($id, $field['value']) ? 'selected="selected"' : ''; 
  103.  
  104. echo '<option value="'.$id.'" '.$selected.'>'.$label.'</option>'; 
  105.  
  106. if($key != '') echo '</optgroup>'; 
  107. else 
  108. $selected = in_array($key, $field['value']) ? 'selected="selected"' : ''; 
  109. echo '<option value="'.$key.'" '.$selected.'>'.$value.'</option>'; 
  110.  
  111. echo '</select>'; 
  112.  
  113.  
  114. /** 
  115. * create_options() 
  116. * 
  117. * Create extra options for your field. This is rendered when editing a field. 
  118. * The value of $field['name'] can be used (like bellow) to save extra data to the $field 
  119. * 
  120. * @type action 
  121. * @since 3.6 
  122. * @date 23/01/13 
  123. * 
  124. * @param $field - an array holding all the field's data 
  125. */ 
  126.  
  127. function create_options( $field ) 
  128. $key = $field['name']; 
  129.  
  130.  
  131. // implode choices so they work in a textarea 
  132. if( is_array($field['choices']) ) 
  133. {  
  134. foreach( $field['choices'] as $k => $v ) 
  135. $field['choices'][ $k ] = $k . ' : ' . $v; 
  136. $field['choices'] = implode("\n", $field['choices']); 
  137.  
  138. ?> 
  139. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  140. <td class="label"> 
  141. <label for=""><?php _e("Choices", 'acf'); ?></label> 
  142. <p><?php _e("Enter each choice on a new line.", 'acf'); ?></p> 
  143. <p><?php _e("For more control, you may specify both a value and label like this:", 'acf'); ?></p> 
  144. <p><?php _e("red : Red", 'acf'); ?><br /><?php _e("blue : Blue", 'acf'); ?></p> 
  145. </td> 
  146. <td> 
  147. <?php 
  148.  
  149. do_action('acf/create_field', array( 
  150. 'type' => 'textarea',  
  151. 'class' => 'textarea field_option-choices',  
  152. 'name' => 'fields['.$key.'][choices]',  
  153. 'value' => $field['choices'],  
  154. )); 
  155.  
  156. ?> 
  157. </td> 
  158. </tr> 
  159. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  160. <td class="label"> 
  161. <label><?php _e("Default Value", 'acf'); ?></label> 
  162. <p class="description"><?php _e("Enter each default value on a new line", 'acf'); ?></p> 
  163. </td> 
  164. <td> 
  165. <?php 
  166.  
  167. do_action('acf/create_field', array( 
  168. 'type' => 'textarea',  
  169. 'name' => 'fields['.$key.'][default_value]',  
  170. 'value' => $field['default_value'],  
  171. )); 
  172.  
  173. ?> 
  174. </td> 
  175. </tr> 
  176. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  177. <td class="label"> 
  178. <label><?php _e("Allow Null?", 'acf'); ?></label> 
  179. </td> 
  180. <td> 
  181. <?php  
  182. do_action('acf/create_field', array( 
  183. 'type' => 'radio',  
  184. 'name' => 'fields['.$key.'][allow_null]',  
  185. 'value' => $field['allow_null'],  
  186. 'choices' => array( 
  187. 1 => __("Yes", 'acf'),  
  188. 0 => __("No", 'acf'),  
  189. ),  
  190. 'layout' => 'horizontal',  
  191. )); 
  192. ?> 
  193. </td> 
  194. </tr> 
  195. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  196. <td class="label"> 
  197. <label><?php _e("Select multiple values?", 'acf'); ?></label> 
  198. </td> 
  199. <td> 
  200. <?php  
  201. do_action('acf/create_field', array( 
  202. 'type' => 'radio',  
  203. 'name' => 'fields['.$key.'][multiple]',  
  204. 'value' => $field['multiple'],  
  205. 'choices' => array( 
  206. 1 => __("Yes", 'acf'),  
  207. 0 => __("No", 'acf'),  
  208. ),  
  209. 'layout' => 'horizontal',  
  210. )); 
  211. ?> 
  212. </td> 
  213. </tr> 
  214. <?php 
  215.  
  216.  
  217.  
  218. /** 
  219. * format_value_for_api() 
  220. * 
  221. * This filter is appied to the $value after it is loaded from the db and before it is passed back to the api functions such as the_field 
  222. * 
  223. * @type filter 
  224. * @since 3.6 
  225. * @date 23/01/13 
  226. * 
  227. * @param $value - the value which was loaded from the database 
  228. * @param $post_id - the $post_id from which the value was loaded 
  229. * @param $field - the field array holding all the field options 
  230. * 
  231. * @return $value - the modified value 
  232. */ 
  233.  
  234. function format_value_for_api( $value, $post_id, $field ) 
  235. if( $value == 'null' ) 
  236. $value = false; 
  237.  
  238.  
  239. return $value; 
  240.  
  241.  
  242. /** 
  243. * update_field() 
  244. * 
  245. * This filter is appied to the $field before it is saved to the database 
  246. * 
  247. * @type filter 
  248. * @since 3.6 
  249. * @date 23/01/13 
  250. * 
  251. * @param $field - the field array holding all the field options 
  252. * @param $post_id - the field group ID (post_type = acf) 
  253. * 
  254. * @return $field - the modified field 
  255. */ 
  256.  
  257. function update_field( $field, $post_id ) 
  258.  
  259. // check if is array. Normal back end edit posts a textarea, but a user might use update_field from the front end 
  260. if( is_array( $field['choices'] )) 
  261. return $field; 
  262.  
  263.  
  264. // vars 
  265. $new_choices = array(); 
  266.  
  267.  
  268. // explode choices from each line 
  269. if( $field['choices'] ) 
  270. // stripslashes ("") 
  271. $field['choices'] = stripslashes_deep($field['choices']); 
  272.  
  273. if(strpos($field['choices'], "\n") !== false) 
  274. // found multiple lines, explode it 
  275. $field['choices'] = explode("\n", $field['choices']); 
  276. else 
  277. // no multiple lines!  
  278. $field['choices'] = array($field['choices']); 
  279.  
  280.  
  281. // key => value 
  282. foreach($field['choices'] as $choice) 
  283. if(strpos($choice, ' : ') !== false) 
  284. $choice = explode(' : ', $choice); 
  285. $new_choices[ trim($choice[0]) ] = trim($choice[1]); 
  286. else 
  287. $new_choices[ trim($choice) ] = trim($choice); 
  288.  
  289.  
  290. // update choices 
  291. $field['choices'] = $new_choices; 
  292.  
  293.  
  294. return $field; 
  295.  
  296.  
  297. new acf_field_select(); 
  298.  
  299. ?> 
.