/core/fields/checkbox.php

  1. <?php 
  2.  
  3. class acf_field_checkbox extends acf_field 
  4.  
  5. /** 
  6. * __construct 
  7. * 
  8. * Set name / label needed for actions / filters 
  9. * 
  10. * @since 3.6 
  11. * @date 23/01/13 
  12. */ 
  13.  
  14. function __construct() 
  15. // vars 
  16. $this->name = 'checkbox'; 
  17. $this->label = __("Checkbox", 'acf'); 
  18. $this->category = __("Choice", 'acf'); 
  19. $this->defaults = array( 
  20. 'layout' => 'vertical',  
  21. 'choices' => array(),  
  22. 'default_value' => '',  
  23. ); 
  24.  
  25.  
  26. // do not delete! 
  27. parent::__construct(); 
  28.  
  29.  
  30. /** 
  31. * create_field() 
  32. * 
  33. * Create the HTML interface for your field 
  34. * 
  35. * @param $field - an array holding all the field's data 
  36. * 
  37. * @type action 
  38. * @since 3.6 
  39. * @date 23/01/13 
  40. */ 
  41.  
  42. function create_field( $field ) 
  43. // value must be array 
  44. if( !is_array($field['value']) ) 
  45. // perhaps this is a default value with new lines in it? 
  46. if( strpos($field['value'], "\n") !== false ) 
  47. // found multiple lines, explode it 
  48. $field['value'] = explode("\n", $field['value']); 
  49. else 
  50. $field['value'] = array( $field['value'] ); 
  51.  
  52.  
  53. // trim value 
  54. $field['value'] = array_map('trim', $field['value']); 
  55.  
  56.  
  57. // vars 
  58. $i = 0; 
  59. $e = '<input type="hidden" name="' . esc_attr($field['name']) . '" value="" />'; 
  60. $e .= '<ul class="acf-checkbox-list ' . esc_attr($field['class']) . ' ' . esc_attr($field['layout']) . '">'; 
  61.  
  62.  
  63. // checkbox saves an array 
  64. $field['name'] .= '[]'; 
  65.  
  66.  
  67. // foreach choices 
  68. foreach( $field['choices'] as $key => $value ) 
  69. // vars 
  70. $i++; 
  71. $atts = ''; 
  72.  
  73.  
  74. if( in_array($key, $field['value']) ) 
  75. $atts = 'checked="yes"'; 
  76. if( isset($field['disabled']) && in_array($key, $field['disabled']) ) 
  77. $atts .= ' disabled="true"'; 
  78.  
  79.  
  80. // each checkbox ID is generated with the $key, however, the first checkbox must not use $key so that it matches the field's label for attribute 
  81. $id = $field['id']; 
  82.  
  83. if( $i > 1 ) 
  84. $id .= '-' . $key; 
  85.  
  86. $e .= '<li><label><input id="' . esc_attr($id) . '" type="checkbox" class="' . esc_attr($field['class']) . '" name="' . esc_attr($field['name']) . '" value="' . esc_attr($key) . '" ' . $atts . ' />' . $value . '</label></li>'; 
  87.  
  88. $e .= '</ul>'; 
  89.  
  90.  
  91. // return 
  92. echo $e; 
  93.  
  94.  
  95. /** 
  96. * create_options() 
  97. * 
  98. * Create extra options for your field. This is rendered when editing a field. 
  99. * The value of $field['name'] can be used (like bellow) to save extra data to the $field 
  100. * 
  101. * @type action 
  102. * @since 3.6 
  103. * @date 23/01/13 
  104. * 
  105. * @param $field - an array holding all the field's data 
  106. */ 
  107.  
  108. function create_options( $field ) 
  109. // vars 
  110. $key = $field['name']; 
  111.  
  112.  
  113. // implode checkboxes so they work in a textarea 
  114. if( is_array($field['choices']) ) 
  115. {  
  116. foreach( $field['choices'] as $k => $v ) 
  117. $field['choices'][ $k ] = $k . ' : ' . $v; 
  118. $field['choices'] = implode("\n", $field['choices']); 
  119.  
  120. ?> 
  121. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  122. <td class="label"> 
  123. <label for=""><?php _e("Choices", 'acf'); ?></label> 
  124. <p><?php _e("Enter each choice on a new line.", 'acf'); ?></p> 
  125. <p><?php _e("For more control, you may specify both a value and label like this:", 'acf'); ?></p> 
  126. <p><?php _e("red : Red", 'acf'); ?><br /><?php _e("blue : Blue", 'acf'); ?></p> 
  127. </td> 
  128. <td> 
  129. <?php 
  130.  
  131. do_action('acf/create_field', array( 
  132. 'type' => 'textarea',  
  133. 'class' => 'textarea field_option-choices',  
  134. 'name' => 'fields['.$key.'][choices]',  
  135. 'value' => $field['choices'],  
  136. )); 
  137.  
  138. ?> 
  139. </td> 
  140. </tr> 
  141. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  142. <td class="label"> 
  143. <label><?php _e("Default Value", 'acf'); ?></label> 
  144. <p class="description"><?php _e("Enter each default value on a new line", 'acf'); ?></p> 
  145. </td> 
  146. <td> 
  147. <?php 
  148.  
  149. do_action('acf/create_field', array( 
  150. 'type' => 'textarea',  
  151. 'name' => 'fields['.$key.'][default_value]',  
  152. 'value' => $field['default_value'],  
  153. )); 
  154.  
  155. ?> 
  156. </td> 
  157. </tr> 
  158. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  159. <td class="label"> 
  160. <label for=""><?php _e("Layout", 'acf'); ?></label> 
  161. </td> 
  162. <td> 
  163. <?php 
  164.  
  165. do_action('acf/create_field', array( 
  166. 'type' => 'radio',  
  167. 'name' => 'fields['.$key.'][layout]',  
  168. 'value' => $field['layout'],  
  169. 'layout' => 'horizontal',  
  170. 'choices' => array( 
  171. 'vertical' => __("Vertical", 'acf'),  
  172. 'horizontal' => __("Horizontal", 'acf') 
  173. )); 
  174.  
  175. ?> 
  176. </td> 
  177. </tr> 
  178. <?php 
  179.  
  180.  
  181.  
  182. new acf_field_checkbox(); 
  183.  
  184. ?> 
.