radio.php
| 1 | <?php |
| 2 | |
| 3 | class acf_Radio extends acf_Field |
| 4 | { |
| 5 | |
| 6 | /*-------------------------------------------------------------------------------------- |
| 7 | * |
| 8 | * Constructor |
| 9 | * |
| 10 | * @author Elliot Condon |
| 11 | * @since 1.0.0 |
| 12 | * @updated 2.2.0 |
| 13 | * |
| 14 | *-------------------------------------------------------------------------------------*/ |
| 15 | |
| 16 | function __construct($parent) |
| 17 | { |
| 18 | parent::__construct($parent); |
| 19 | |
| 20 | $this->name = 'radio'; |
| 21 | $this->title = __('Radio Button','acf'); |
| 22 | |
| 23 | } |
| 24 | |
| 25 | |
| 26 | /*-------------------------------------------------------------------------------------- |
| 27 | * |
| 28 | * create_field |
| 29 | * |
| 30 | * @author Elliot Condon |
| 31 | * @since 2.0.5 |
| 32 | * @updated 2.2.0 |
| 33 | * |
| 34 | *-------------------------------------------------------------------------------------*/ |
| 35 | |
| 36 | function create_field($field) |
| 37 | { |
| 38 | // defaults |
| 39 | $field['layout'] = isset($field['layout']) ? $field['layout'] : 'vertical'; |
| 40 | $field['choices'] = isset($field['choices']) ? $field['choices'] : array(); |
| 41 | |
| 42 | // no choices |
| 43 | if(empty($field['choices'])) |
| 44 | { |
| 45 | echo '<p>' . __("No choices to choose from",'acf') . '</p>'; |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | echo '<ul class="radio_list ' . $field['class'] . ' ' . $field['layout'] . '">'; |
| 50 | |
| 51 | $i = 0; |
| 52 | foreach($field['choices'] as $key => $value) |
| 53 | { |
| 54 | $i++; |
| 55 | |
| 56 | // if there is no value and this is the first of the choices and there is no "0" choice, select this on by default |
| 57 | // the 0 choice would normally match a no value. This needs to remain possible for the create new field to work. |
| 58 | if(!$field['value'] && $i == 1 && !isset($field['choices']['0'])) |
| 59 | { |
| 60 | $field['value'] = $key; |
| 61 | } |
| 62 | |
| 63 | $selected = ''; |
| 64 | |
| 65 | if($key == $field['value']) |
| 66 | { |
| 67 | $selected = 'checked="checked" data-checked="checked"'; |
| 68 | } |
| 69 | |
| 70 | echo '<li><label><input type="radio" name="' . $field['name'] . '" value="' . $key . '" ' . $selected . ' />' . $value . '</label></li>'; |
| 71 | } |
| 72 | |
| 73 | echo '</ul>'; |
| 74 | |
| 75 | } |
| 76 | |
| 77 | |
| 78 | /*-------------------------------------------------------------------------------------- |
| 79 | * |
| 80 | * create_options |
| 81 | * |
| 82 | * @author Elliot Condon |
| 83 | * @since 2.0.6 |
| 84 | * @updated 2.2.0 |
| 85 | * |
| 86 | *-------------------------------------------------------------------------------------*/ |
| 87 | |
| 88 | function create_options($key, $field) |
| 89 | { |
| 90 | // defaults |
| 91 | $field['layout'] = isset($field['layout']) ? $field['layout'] : 'vertical'; |
| 92 | $field['default_value'] = isset($field['default_value']) ? $field['default_value'] : ''; |
| 93 | |
| 94 | |
| 95 | // implode checkboxes so they work in a textarea |
| 96 | if(isset($field['choices']) && is_array($field['choices'])) |
| 97 | { |
| 98 | foreach($field['choices'] as $choice_key => $choice_val) |
| 99 | { |
| 100 | $field['choices'][$choice_key] = $choice_key.' : '.$choice_val; |
| 101 | } |
| 102 | $field['choices'] = implode("\n", $field['choices']); |
| 103 | } |
| 104 | else |
| 105 | { |
| 106 | $field['choices'] = ""; |
| 107 | } |
| 108 | |
| 109 | ?> |
| 110 | |
| 111 | |
| 112 | <tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 113 | <td class="label"> |
| 114 | <label for=""><?php _e("Choices",'acf'); ?></label> |
| 115 | <p class="description"><?php _e("Enter your choices one per line<br /> |
| 116 | <br /> |
| 117 | Red<br /> |
| 118 | Blue<br /> |
| 119 | <br /> |
| 120 | or<br /> |
| 121 | <br /> |
| 122 | red : Red<br /> |
| 123 | blue : Blue",'acf'); ?></p> |
| 124 | </td> |
| 125 | <td> |
| 126 | <textarea rows="5" name="fields[<?php echo $key; ?>][choices]" id=""><?php echo $field['choices']; ?></textarea> |
| 127 | </td> |
| 128 | </tr> |
| 129 | <tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 130 | <td class="label"> |
| 131 | <label><?php _e("Default Value",'acf'); ?></label> |
| 132 | </td> |
| 133 | <td> |
| 134 | <?php |
| 135 | $this->parent->create_field(array( |
| 136 | 'type' => 'text', |
| 137 | 'name' => 'fields['.$key.'][default_value]', |
| 138 | 'value' => $field['default_value'], |
| 139 | )); |
| 140 | ?> |
| 141 | </td> |
| 142 | </tr> |
| 143 | <tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 144 | <td class="label"> |
| 145 | <label for=""><?php _e("Layout",'acf'); ?></label> |
| 146 | </td> |
| 147 | <td> |
| 148 | <?php |
| 149 | $this->parent->create_field(array( |
| 150 | 'type' => 'radio', |
| 151 | 'name' => 'fields['.$key.'][layout]', |
| 152 | 'value' => $field['layout'], |
| 153 | 'layout' => 'horizontal', |
| 154 | 'choices' => array( |
| 155 | 'vertical' => 'Vertical', |
| 156 | 'horizontal' => 'Horizontal' |
| 157 | ) |
| 158 | )); |
| 159 | ?> |
| 160 | </td> |
| 161 | </tr> |
| 162 | |
| 163 | |
| 164 | <?php |
| 165 | } |
| 166 | |
| 167 | |
| 168 | /*-------------------------------------------------------------------------------------- |
| 169 | * |
| 170 | * pre_save_field |
| 171 | * - called just before saving the field to the database. |
| 172 | * |
| 173 | * @author Elliot Condon |
| 174 | * @since 2.2.0 |
| 175 | * |
| 176 | *-------------------------------------------------------------------------------------*/ |
| 177 | |
| 178 | function pre_save_field($field) |
| 179 | { |
| 180 | // defaults |
| 181 | $field['choices'] = isset($field['choices']) ? $field['choices'] : ''; |
| 182 | |
| 183 | // vars |
| 184 | $new_choices = array(); |
| 185 | |
| 186 | // explode choices from each line |
| 187 | if(strpos($field['choices'], "\n") !== false) |
| 188 | { |
| 189 | // found multiple lines, explode it |
| 190 | $field['choices'] = explode("\n", $field['choices']); |
| 191 | } |
| 192 | else |
| 193 | { |
| 194 | // no multiple lines! |
| 195 | $field['choices'] = array($field['choices']); |
| 196 | } |
| 197 | |
| 198 | // key => value |
| 199 | foreach($field['choices'] as $choice) |
| 200 | { |
| 201 | if(strpos($choice, ' : ') !== false) |
| 202 | { |
| 203 | $choice = explode(' : ', $choice); |
| 204 | $new_choices[trim($choice[0])] = trim($choice[1]); |
| 205 | } |
| 206 | else |
| 207 | { |
| 208 | $new_choices[trim($choice)] = trim($choice); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | // update choices |
| 213 | $field['choices'] = $new_choices; |
| 214 | |
| 215 | // return updated field |
| 216 | return $field; |
| 217 | |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | ?> |