| 1 |
<?php |
| 2 |
|
| 3 |
class acf_field_select extends acf_field |
| 4 |
{ |
| 5 |
// vars |
| 6 |
var $defaults; |
| 7 |
|
| 8 |
|
| 9 |
/* |
| 10 |
* __construct |
| 11 |
* |
| 12 |
* Set name / label needed for actions / filters |
| 13 |
* |
| 14 |
* @since 3.6 |
| 15 |
* @date 23/01/13 |
| 16 |
*/ |
| 17 |
|
| 18 |
function __construct() |
| 19 |
{ |
| 20 |
// vars |
| 21 |
$this->name = 'select'; |
| 22 |
$this->label = __("Select",'acf'); |
| 23 |
$this->category = __("Choice",'acf'); |
| 24 |
$this->defaults = array( |
| 25 |
'multiple' => 0, |
| 26 |
'allow_null' => 0, |
| 27 |
'choices' => array(), |
| 28 |
'default_value' => '' |
| 29 |
); |
| 30 |
|
| 31 |
|
| 32 |
// do not delete! |
| 33 |
parent::__construct(); |
| 34 |
|
| 35 |
|
| 36 |
// extra |
| 37 |
//add_filter('acf/update_field/type=select', array($this, 'update_field'), 5, 2); |
| 38 |
add_filter('acf/update_field/type=checkbox', array($this, 'update_field'), 5, 2); |
| 39 |
add_filter('acf/update_field/type=radio', array($this, 'update_field'), 5, 2); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
/* |
| 44 |
* create_field() |
| 45 |
* |
| 46 |
* Create the HTML interface for your field |
| 47 |
* |
| 48 |
* @param $field - an array holding all the field's data |
| 49 |
* |
| 50 |
* @type action |
| 51 |
* @since 3.6 |
| 52 |
* @date 23/01/13 |
| 53 |
*/ |
| 54 |
|
| 55 |
function create_field( $field ) |
| 56 |
{ |
| 57 |
// vars |
| 58 |
$field = array_merge($this->defaults, $field); |
| 59 |
$optgroup = false; |
| 60 |
|
| 61 |
|
| 62 |
// determin if choices are grouped (2 levels of array) |
| 63 |
if( is_array($field['choices']) ) |
| 64 |
{ |
| 65 |
foreach( $field['choices'] as $k => $v ) |
| 66 |
{ |
| 67 |
if( is_array($v) ) |
| 68 |
{ |
| 69 |
$optgroup = true; |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
// value must be array |
| 76 |
if( !is_array($field['value']) ) |
| 77 |
{ |
| 78 |
// perhaps this is a default value with new lines in it? |
| 79 |
if( strpos($field['value'], "\n") !== false ) |
| 80 |
{ |
| 81 |
// found multiple lines, explode it |
| 82 |
$field['value'] = explode("\n", $field['value']); |
| 83 |
} |
| 84 |
else |
| 85 |
{ |
| 86 |
$field['value'] = array( $field['value'] ); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
// trim value |
| 92 |
$field['value'] = array_map('trim', $field['value']); |
| 93 |
|
| 94 |
|
| 95 |
// multiple select |
| 96 |
$multiple = ''; |
| 97 |
if( $field['multiple'] ) |
| 98 |
{ |
| 99 |
// create a hidden field to allow for no selections |
| 100 |
echo '<input type="hidden" name="' . $field['name'] . '" />'; |
| 101 |
|
| 102 |
$multiple = ' multiple="multiple" size="5" '; |
| 103 |
$field['name'] .= '[]'; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
// html |
| 108 |
echo '<select id="' . $field['id'] . '" class="' . $field['class'] . '" name="' . $field['name'] . '" ' . $multiple . ' >'; |
| 109 |
|
| 110 |
|
| 111 |
// null |
| 112 |
if( $field['allow_null'] ) |
| 113 |
{ |
| 114 |
echo '<option value="null"> - Select - </option>'; |
| 115 |
} |
| 116 |
|
| 117 |
// loop through values and add them as options |
| 118 |
if( is_array($field['choices']) ) |
| 119 |
{ |
| 120 |
foreach( $field['choices'] as $key => $value ) |
| 121 |
{ |
| 122 |
if( $optgroup ) |
| 123 |
{ |
| 124 |
// this select is grouped with optgroup |
| 125 |
if($key != '') echo '<optgroup label="'.$key.'">'; |
| 126 |
|
| 127 |
if( is_array($value) ) |
| 128 |
{ |
| 129 |
foreach($value as $id => $label) |
| 130 |
{ |
| 131 |
$selected = in_array($id, $field['value']) ? 'selected="selected"' : ''; |
| 132 |
|
| 133 |
echo '<option value="'.$id.'" '.$selected.'>'.$label.'</option>'; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
if($key != '') echo '</optgroup>'; |
| 138 |
} |
| 139 |
else |
| 140 |
{ |
| 141 |
$selected = in_array($key, $field['value']) ? 'selected="selected"' : ''; |
| 142 |
echo '<option value="'.$key.'" '.$selected.'>'.$value.'</option>'; |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
echo '</select>'; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
/* |
| 152 |
* create_options() |
| 153 |
* |
| 154 |
* Create extra options for your field. This is rendered when editing a field. |
| 155 |
* The value of $field['name'] can be used (like bellow) to save extra data to the $field |
| 156 |
* |
| 157 |
* @type action |
| 158 |
* @since 3.6 |
| 159 |
* @date 23/01/13 |
| 160 |
* |
| 161 |
* @param $field - an array holding all the field's data |
| 162 |
*/ |
| 163 |
|
| 164 |
function create_options( $field ) |
| 165 |
{ |
| 166 |
$field = array_merge($this->defaults, $field); |
| 167 |
$key = $field['name']; |
| 168 |
|
| 169 |
|
| 170 |
// implode choices so they work in a textarea |
| 171 |
if( is_array($field['choices']) ) |
| 172 |
{ |
| 173 |
foreach( $field['choices'] as $k => $v ) |
| 174 |
{ |
| 175 |
$field['choices'][ $k ] = $k . ' : ' . $v; |
| 176 |
} |
| 177 |
$field['choices'] = implode("\n", $field['choices']); |
| 178 |
} |
| 179 |
|
| 180 |
?> |
| 181 |
<tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 182 |
<td class="label"> |
| 183 |
<label for=""><?php _e("Choices",'acf'); ?></label> |
| 184 |
<p><?php _e("Enter each choice on a new line.",'acf'); ?></p> |
| 185 |
<p><?php _e("For more control, you may specify both a value and label like this:",'acf'); ?></p> |
| 186 |
<p><?php _e("red : Red",'acf'); ?><br /><?php _e("blue : Blue",'acf'); ?></p> |
| 187 |
</td> |
| 188 |
<td> |
| 189 |
<?php |
| 190 |
|
| 191 |
do_action('acf/create_field', array( |
| 192 |
'type' => 'textarea', |
| 193 |
'class' => 'textarea field_option-choices', |
| 194 |
'name' => 'fields['.$key.'][choices]', |
| 195 |
'value' => $field['choices'], |
| 196 |
)); |
| 197 |
|
| 198 |
?> |
| 199 |
</td> |
| 200 |
</tr> |
| 201 |
<tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 202 |
<td class="label"> |
| 203 |
<label><?php _e("Default Value",'acf'); ?></label> |
| 204 |
<p class="description"><?php _e("Enter each default value on a new line",'acf'); ?></p> |
| 205 |
</td> |
| 206 |
<td> |
| 207 |
<?php |
| 208 |
|
| 209 |
do_action('acf/create_field', array( |
| 210 |
'type' => 'textarea', |
| 211 |
'name' => 'fields['.$key.'][default_value]', |
| 212 |
'value' => $field['default_value'], |
| 213 |
)); |
| 214 |
|
| 215 |
?> |
| 216 |
</td> |
| 217 |
</tr> |
| 218 |
<tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 219 |
<td class="label"> |
| 220 |
<label><?php _e("Allow Null?",'acf'); ?></label> |
| 221 |
</td> |
| 222 |
<td> |
| 223 |
<?php |
| 224 |
do_action('acf/create_field', array( |
| 225 |
'type' => 'radio', |
| 226 |
'name' => 'fields['.$key.'][allow_null]', |
| 227 |
'value' => $field['allow_null'], |
| 228 |
'choices' => array( |
| 229 |
1 => __("Yes",'acf'), |
| 230 |
0 => __("No",'acf'), |
| 231 |
), |
| 232 |
'layout' => 'horizontal', |
| 233 |
)); |
| 234 |
?> |
| 235 |
</td> |
| 236 |
</tr> |
| 237 |
<tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 238 |
<td class="label"> |
| 239 |
<label><?php _e("Select multiple values?",'acf'); ?></label> |
| 240 |
</td> |
| 241 |
<td> |
| 242 |
<?php |
| 243 |
do_action('acf/create_field', array( |
| 244 |
'type' => 'radio', |
| 245 |
'name' => 'fields['.$key.'][multiple]', |
| 246 |
'value' => $field['multiple'], |
| 247 |
'choices' => array( |
| 248 |
1 => __("Yes",'acf'), |
| 249 |
0 => __("No",'acf'), |
| 250 |
), |
| 251 |
'layout' => 'horizontal', |
| 252 |
)); |
| 253 |
?> |
| 254 |
</td> |
| 255 |
</tr> |
| 256 |
<?php |
| 257 |
|
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
/* |
| 262 |
* format_value_for_api() |
| 263 |
* |
| 264 |
* This filter is appied to the $value after it is loaded from the db and before it is passed back to the api functions such as the_field |
| 265 |
* |
| 266 |
* @type filter |
| 267 |
* @since 3.6 |
| 268 |
* @date 23/01/13 |
| 269 |
* |
| 270 |
* @param $value - the value which was loaded from the database |
| 271 |
* @param $post_id - the $post_id from which the value was loaded |
| 272 |
* @param $field - the field array holding all the field options |
| 273 |
* |
| 274 |
* @return $value - the modified value |
| 275 |
*/ |
| 276 |
|
| 277 |
function format_value_for_api( $value, $post_id, $field ) |
| 278 |
{ |
| 279 |
if( $value == 'null' ) |
| 280 |
{ |
| 281 |
$value = false; |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
return $value; |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
/* |
| 290 |
* update_field() |
| 291 |
* |
| 292 |
* This filter is appied to the $field before it is saved to the database |
| 293 |
* |
| 294 |
* @type filter |
| 295 |
* @since 3.6 |
| 296 |
* @date 23/01/13 |
| 297 |
* |
| 298 |
* @param $field - the field array holding all the field options |
| 299 |
* @param $post_id - the field group ID (post_type = acf) |
| 300 |
* |
| 301 |
* @return $field - the modified field |
| 302 |
*/ |
| 303 |
|
| 304 |
function update_field( $field, $post_id ) |
| 305 |
{ |
| 306 |
// vars |
| 307 |
$field = array_merge($this->defaults, $field); |
| 308 |
|
| 309 |
|
| 310 |
// check if is array. Normal back end edit posts a textarea, but a user might use update_field from the front end |
| 311 |
if( is_array( $field['choices'] )) |
| 312 |
{ |
| 313 |
return $field; |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
// vars |
| 318 |
$new_choices = array(); |
| 319 |
|
| 320 |
|
| 321 |
// explode choices from each line |
| 322 |
if( $field['choices'] ) |
| 323 |
{ |
| 324 |
// stripslashes ("") |
| 325 |
$field['choices'] = stripslashes_deep($field['choices']); |
| 326 |
|
| 327 |
if(strpos($field['choices'], "\n") !== false) |
| 328 |
{ |
| 329 |
// found multiple lines, explode it |
| 330 |
$field['choices'] = explode("\n", $field['choices']); |
| 331 |
} |
| 332 |
else |
| 333 |
{ |
| 334 |
// no multiple lines! |
| 335 |
$field['choices'] = array($field['choices']); |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
// key => value |
| 340 |
foreach($field['choices'] as $choice) |
| 341 |
{ |
| 342 |
if(strpos($choice, ' : ') !== false) |
| 343 |
{ |
| 344 |
$choice = explode(' : ', $choice); |
| 345 |
$new_choices[trim($choice[0])] = trim($choice[1]); |
| 346 |
} |
| 347 |
else |
| 348 |
{ |
| 349 |
$new_choices[trim($choice)] = trim($choice); |
| 350 |
} |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
// update choices |
| 356 |
$field['choices'] = $new_choices; |
| 357 |
|
| 358 |
|
| 359 |
return $field; |
| 360 |
} |
| 361 |
|
| 362 |
} |
| 363 |
|
| 364 |
new acf_field_select(); |
| 365 |
|
| 366 |
?> |