name = 'radio';
$this->label = __("Radio Button",'acf');
$this->category = 'choice';
$this->defaults = array(
'layout' => 'vertical',
'choices' => array(),
'default_value' => '',
'other_choice' => 0,
'save_other_choice' => 0,
'allow_null' => 0,
'return_format' => 'value'
);
}
/*
* render_field()
*
* Create the HTML interface for your field
*
* @param $field (array) the $field being rendered
*
* @type action
* @since 3.6
* @date 23/01/13
*
* @param $field (array) the $field being edited
* @return n/a
*/
function render_field( $field ) {
// vars
$i = 0;
$e = '';
$ul = array(
'class' => 'acf-radio-list',
'data-allow_null' => $field['allow_null'],
'data-other_choice' => $field['other_choice']
);
// append to class
$ul['class'] .= ' ' . ($field['layout'] == 'horizontal' ? 'acf-hl' : 'acf-bl');
$ul['class'] .= ' ' . $field['class'];
// select value
$checked = '';
$value = strval($field['value']);
// selected choice
if( isset($field['choices'][ $value ]) ) {
$checked = $value;
// custom choice
} elseif( $field['other_choice'] && $value !== '' ) {
$checked = 'other';
// allow null
} elseif( $field['allow_null'] ) {
// do nothing
// select first input by default
} else {
$checked = key($field['choices']);
}
// ensure $checked is a string (could be an int)
$checked = strval($checked);
// other choice
if( $field['other_choice'] ) {
// vars
$input = array(
'type' => 'text',
'name' => $field['name'],
'value' => '',
'disabled' => 'disabled',
'class' => 'acf-disabled'
);
// select other choice if value is not a valid choice
if( $checked === 'other' ) {
unset($input['disabled']);
$input['value'] = $field['value'];
}
// allow custom 'other' choice to be defined
if( !isset($field['choices']['other']) ) {
$field['choices']['other'] = '';
}
// append other choice
$field['choices']['other'] .= '