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