/core/fields/radio.php

  1. <?php 
  2.  
  3. class acf_field_radio 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 = 'radio'; 
  16. $this->label = __("Radio Button", 'acf'); 
  17. $this->category = __("Choice", 'acf'); 
  18. $this->defaults = array( 
  19. 'layout' => 'vertical',  
  20. 'choices' => array(),  
  21. 'default_value' => '',  
  22. 'other_choice' => 0,  
  23. 'save_other_choice' => 0,  
  24. ); 
  25.  
  26.  
  27. // do not delete! 
  28. parent::__construct(); 
  29.  
  30.  
  31.  
  32. /** 
  33. * create_field() 
  34. * 
  35. * Create the HTML interface for your field 
  36. * 
  37. * @param $field - an array holding all the field's data 
  38. * 
  39. * @type action 
  40. * @since 3.6 
  41. * @date 23/01/13 
  42. */ 
  43.  
  44. function create_field( $field ) 
  45. // vars 
  46. $i = 0; 
  47. $e = '<ul class="acf-radio-list ' . esc_attr($field['class']) . ' ' . esc_attr($field['layout']) . '">'; 
  48.  
  49.  
  50. // add choices 
  51. if( is_array($field['choices']) ) 
  52. foreach( $field['choices'] as $key => $value ) 
  53. // vars 
  54. $i++; 
  55. $atts = ''; 
  56.  
  57.  
  58. // if there is no value and this is the first of the choices, select this on by default 
  59. if( $field['value'] === false ) 
  60. if( $i === 1 ) 
  61. $atts = 'checked="checked" data-checked="checked"'; 
  62. else 
  63. if( strval($key) === strval($field['value']) ) 
  64. $atts = 'checked="checked" data-checked="checked"'; 
  65.  
  66.  
  67. // HTML 
  68. $e .= '<li><label><input id="' . esc_attr($field['id']) . '-' . esc_attr($key) . '" type="radio" name="' . esc_attr($field['name']) . '" value="' . esc_attr($key) . '" ' . esc_attr( $atts ) . ' />' . $value . '</label></li>'; 
  69.  
  70.  
  71. // other choice 
  72. if( $field['other_choice'] ) 
  73. // vars 
  74. $atts = ''; 
  75. $atts2 = 'name="" value="" style="display:none"'; 
  76.  
  77.  
  78. if( $field['value'] !== false ) 
  79. if( !isset($field['choices'][ $field['value'] ]) ) 
  80. $atts = 'checked="checked" data-checked="checked"'; 
  81. $atts2 = 'name="' . esc_attr($field['name']) . '" value="' . esc_attr($field['value']) . '"' ; 
  82.  
  83.  
  84. $e .= '<li><label><input id="' . esc_attr($field['id']) . '-other" type="radio" name="' . esc_attr($field['name']) . '" value="other" ' . $atts . ' />' . __("Other", 'acf') . '</label> <input type="text" ' . $atts2 . ' /></li>'; 
  85.  
  86.  
  87. $e .= '</ul>'; 
  88.  
  89. echo $e; 
  90.  
  91.  
  92.  
  93. /** 
  94. * create_options() 
  95. * 
  96. * Create extra options for your field. This is rendered when editing a field. 
  97. * The value of $field['name'] can be used (like bellow) to save extra data to the $field 
  98. * 
  99. * @type action 
  100. * @since 3.6 
  101. * @date 23/01/13 
  102. * 
  103. * @param $field - an array holding all the field's data 
  104. */ 
  105.  
  106. function create_options( $field ) 
  107. // vars 
  108. $key = $field['name']; 
  109.  
  110. // implode checkboxes so they work in a textarea 
  111. if( is_array($field['choices']) ) 
  112. {  
  113. foreach( $field['choices'] as $k => $v ) 
  114. $field['choices'][ $k ] = $k . ' : ' . $v; 
  115. $field['choices'] = implode("\n", $field['choices']); 
  116.  
  117. ?> 
  118. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  119. <td class="label"> 
  120. <label for=""><?php _e("Choices", 'acf'); ?></label> 
  121. <p class="description"><?php _e("Enter your choices one per line", 'acf'); ?><br /> 
  122. <br /> 
  123. <?php _e("Red", 'acf'); ?><br /> 
  124. <?php _e("Blue", 'acf'); ?><br /> 
  125. <br /> 
  126. <?php _e("red : Red", 'acf'); ?><br /> 
  127. <?php _e("blue : Blue", 'acf'); ?><br /> 
  128. </p> 
  129. </td> 
  130. <td> 
  131. <?php 
  132.  
  133. do_action('acf/create_field', array( 
  134. 'type' => 'textarea',  
  135. 'class' => 'textarea field_option-choices',  
  136. 'name' => 'fields['.$key.'][choices]',  
  137. 'value' => $field['choices'],  
  138. )); 
  139.  
  140. ?> 
  141. <div class="radio-option-other_choice"> 
  142. <?php 
  143.  
  144. do_action('acf/create_field', array( 
  145. 'type' => 'true_false',  
  146. 'name' => 'fields['.$key.'][other_choice]',  
  147. 'value' => $field['other_choice'],  
  148. 'message' => __("Add 'other' choice to allow for custom values", 'acf') 
  149. )); 
  150.  
  151. ?> 
  152. </div> 
  153. <div class="radio-option-save_other_choice" <?php if( !$field['other_choice'] ): ?>style="display:none"<?php endif; ?>> 
  154. <?php 
  155.  
  156. do_action('acf/create_field', array( 
  157. 'type' => 'true_false',  
  158. 'name' => 'fields['.$key.'][save_other_choice]',  
  159. 'value' => $field['save_other_choice'],  
  160. 'message' => __("Save 'other' values to the field's choices", 'acf') 
  161. )); 
  162.  
  163. ?> 
  164. </div> 
  165. </td> 
  166. </tr> 
  167. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  168. <td class="label"> 
  169. <label><?php _e("Default Value", 'acf'); ?></label> 
  170. </td> 
  171. <td> 
  172. <?php 
  173.  
  174. do_action('acf/create_field', array( 
  175. 'type' => 'text',  
  176. 'name' => 'fields['.$key.'][default_value]',  
  177. 'value' => $field['default_value'],  
  178. )); 
  179.  
  180. ?> 
  181. </td> 
  182. </tr> 
  183. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  184. <td class="label"> 
  185. <label for=""><?php _e("Layout", 'acf'); ?></label> 
  186. </td> 
  187. <td> 
  188. <?php 
  189.  
  190. do_action('acf/create_field', array( 
  191. 'type' => 'radio',  
  192. 'name' => 'fields['.$key.'][layout]',  
  193. 'value' => $field['layout'],  
  194. 'layout' => 'horizontal',  
  195. 'choices' => array( 
  196. 'vertical' => __("Vertical", 'acf'),  
  197. 'horizontal' => __("Horizontal", 'acf') 
  198. )); 
  199.  
  200. ?> 
  201. </td> 
  202. </tr> 
  203. <?php 
  204.  
  205.  
  206.  
  207. /** 
  208. * update_value() 
  209. * 
  210. * This filter is appied to the $value before it is updated in the db 
  211. * 
  212. * @type filter 
  213. * @since 3.6 
  214. * @date 23/01/13 
  215. * 
  216. * @param $value - the value which will be saved in the database 
  217. * @param $post_id - the $post_id of which the value will be saved 
  218. * @param $field - the field array holding all the field options 
  219. * 
  220. * @return $value - the modified value 
  221. */ 
  222.  
  223. function update_value( $value, $post_id, $field ) 
  224. // validate 
  225. if( $field['save_other_choice'] ) 
  226. // value isn't in choices yet 
  227. if( !isset($field['choices'][ $value ]) ) 
  228. // update $field 
  229. $field['choices'][ $value ] = $value; 
  230.  
  231.  
  232. // can save 
  233. if( isset($field['field_group']) ) 
  234. do_action('acf/update_field', $field, $field['field_group']); 
  235.  
  236. }  
  237.  
  238. return $value; 
  239.  
  240.  
  241. new acf_field_radio(); 
  242.  
  243. ?> 
.