radio.php
| 1 | <?php |
| 2 | |
| 3 | class acf_field_radio 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 = 'radio'; |
| 19 | $this->label = __("Radio Button",'acf'); |
| 20 | $this->category = __("Choice",'acf'); |
| 21 | |
| 22 | |
| 23 | // do not delete! |
| 24 | parent::__construct(); |
| 25 | |
| 26 | } |
| 27 | |
| 28 | |
| 29 | /* |
| 30 | * create_field() |
| 31 | * |
| 32 | * Create the HTML interface for your field |
| 33 | * |
| 34 | * @param $field - an array holding all the field's data |
| 35 | * |
| 36 | * @type action |
| 37 | * @since 3.6 |
| 38 | * @date 23/01/13 |
| 39 | */ |
| 40 | |
| 41 | function create_field( $field ) |
| 42 | { |
| 43 | // vars |
| 44 | $defaults = array( |
| 45 | 'layout' => 'vertical', |
| 46 | 'choices' => array(), |
| 47 | ); |
| 48 | |
| 49 | $field = array_merge($defaults, $field); |
| 50 | |
| 51 | |
| 52 | echo '<ul class="radio_list ' . $field['class'] . ' ' . $field['layout'] . '">'; |
| 53 | |
| 54 | $i = 0; |
| 55 | if( $field['choices'] ) |
| 56 | { |
| 57 | foreach($field['choices'] as $key => $value) |
| 58 | { |
| 59 | $i++; |
| 60 | |
| 61 | // if there is no value and this is the first of the choices and there is no "0" choice, select this on by default |
| 62 | // the 0 choice would normally match a no value. This needs to remain possible for the create new field to work. |
| 63 | if(!$field['value'] && $i == 1 && !isset($field['choices'][0])) |
| 64 | { |
| 65 | $field['value'] = $key; |
| 66 | } |
| 67 | |
| 68 | $selected = ''; |
| 69 | |
| 70 | if($key == $field['value']) |
| 71 | { |
| 72 | $selected = 'checked="checked" data-checked="checked"'; |
| 73 | } |
| 74 | |
| 75 | echo '<li><label><input id="' . $field['id'] . '-' . $key . '" type="radio" name="' . $field['name'] . '" value="' . $key . '" ' . $selected . ' />' . $value . '</label></li>'; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | echo '</ul>'; |
| 80 | } |
| 81 | |
| 82 | |
| 83 | /* |
| 84 | * create_options() |
| 85 | * |
| 86 | * Create extra options for your field. This is rendered when editing a field. |
| 87 | * The value of $field['name'] can be used (like bellow) to save extra data to the $field |
| 88 | * |
| 89 | * @type action |
| 90 | * @since 3.6 |
| 91 | * @date 23/01/13 |
| 92 | * |
| 93 | * @param $field - an array holding all the field's data |
| 94 | */ |
| 95 | |
| 96 | function create_options( $field ) |
| 97 | { |
| 98 | // vars |
| 99 | $defaults = array( |
| 100 | 'layout' => 'vertical', |
| 101 | 'default_value' => '', |
| 102 | 'choices' => '', |
| 103 | ); |
| 104 | |
| 105 | $field = array_merge($defaults, $field); |
| 106 | $key = $field['name']; |
| 107 | |
| 108 | // implode checkboxes so they work in a textarea |
| 109 | if( is_array($field['choices']) ) |
| 110 | { |
| 111 | foreach( $field['choices'] as $k => $v ) |
| 112 | { |
| 113 | $field['choices'][ $k ] = $k . ' : ' . $v; |
| 114 | } |
| 115 | $field['choices'] = implode("\n", $field['choices']); |
| 116 | } |
| 117 | |
| 118 | ?> |
| 119 | <tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 120 | <td class="label"> |
| 121 | <label for=""><?php _e("Choices",'acf'); ?></label> |
| 122 | <p class="description"><?php _e("Enter your choices one per line",'acf'); ?><br /> |
| 123 | <br /> |
| 124 | <?php _e("Red",'acf'); ?><br /> |
| 125 | <?php _e("Blue",'acf'); ?><br /> |
| 126 | <br /> |
| 127 | <?php _e("red : Red",'acf'); ?><br /> |
| 128 | <?php _e("blue : Blue",'acf'); ?><br /> |
| 129 | </p> |
| 130 | </td> |
| 131 | <td> |
| 132 | <?php |
| 133 | |
| 134 | do_action('acf/create_field', array( |
| 135 | 'type' => 'textarea', |
| 136 | 'class' => 'textarea field_option-choices', |
| 137 | 'name' => 'fields['.$key.'][choices]', |
| 138 | 'value' => $field['choices'], |
| 139 | )); |
| 140 | |
| 141 | ?> |
| 142 | </td> |
| 143 | </tr> |
| 144 | <tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 145 | <td class="label"> |
| 146 | <label><?php _e("Default Value",'acf'); ?></label> |
| 147 | </td> |
| 148 | <td> |
| 149 | <?php |
| 150 | |
| 151 | do_action('acf/create_field', array( |
| 152 | 'type' => 'text', |
| 153 | 'name' => 'fields['.$key.'][default_value]', |
| 154 | 'value' => $field['default_value'], |
| 155 | )); |
| 156 | |
| 157 | ?> |
| 158 | </td> |
| 159 | </tr> |
| 160 | <tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 161 | <td class="label"> |
| 162 | <label for=""><?php _e("Layout",'acf'); ?></label> |
| 163 | </td> |
| 164 | <td> |
| 165 | <?php |
| 166 | |
| 167 | do_action('acf/create_field', array( |
| 168 | 'type' => 'radio', |
| 169 | 'name' => 'fields['.$key.'][layout]', |
| 170 | 'value' => $field['layout'], |
| 171 | 'layout' => 'horizontal', |
| 172 | 'choices' => array( |
| 173 | 'vertical' => __("Vertical",'acf'), |
| 174 | 'horizontal' => __("Horizontal",'acf') |
| 175 | ) |
| 176 | )); |
| 177 | |
| 178 | ?> |
| 179 | </td> |
| 180 | </tr> |
| 181 | <?php |
| 182 | |
| 183 | } |
| 184 | |
| 185 | } |
| 186 | |
| 187 | new acf_field_radio(); |
| 188 | |
| 189 | ?> |