acf_field_select

The Advanced Custom Fields acf field select class.

Defined (1)

The class is defined in the following location(s).

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