acf_field_radio::render_field()publicACF 3.6

Create the HTML interface for your field

Метод класса: acf_field_radio{}

Хуков нет.

Возвращает

null. Ничего (null).

Использование

$acf_field_radio = new acf_field_radio();
$acf_field_radio->render_field( $field );
$field (обязательный)
-

Список изменений

С версии 3.6 Введена.

Код acf_field_radio::render_field() ACF 6.0.4

function render_field( $field ) {

	// vars
	$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'];

	// Determine selected value.
	$value = (string) $field['value'];

	// 1. Selected choice.
	if ( isset( $field['choices'][ $value ] ) ) {
		$checked = (string) $value;

		// 2. Custom choice.
	} elseif ( $field['other_choice'] && $value !== '' ) {
		$checked = 'other';

		// 3. Empty choice.
	} elseif ( $field['allow_null'] ) {
		$checked = '';

		// 4. Default to first choice.
	} else {
		$checked = (string) key( $field['choices'] );
	}

	// other choice
	$other_input = false;
	if ( $field['other_choice'] ) {

		// Define other input attrs.
		$other_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( $other_input['disabled'] );
			$other_input['value'] = $field['value'];
		}

		// Ensure an 'other' choice is defined.
		if ( ! isset( $field['choices']['other'] ) ) {
			$field['choices']['other'] = '';
		}
	}

	// Bail early if no choices.
	if ( empty( $field['choices'] ) ) {
		return;
	}

	// Hiden input.
	$e .= acf_get_hidden_input( array( 'name' => $field['name'] ) );

	// Open <ul>.
	$e .= '<ul ' . acf_esc_attr( $ul ) . '>';

	// Loop through choices.
	foreach ( $field['choices'] as $value => $label ) {
		$is_selected = false;

		// Ensure value is a string.
		$value = (string) $value;

		// Define input attrs.
		$attrs = array(
			'type'  => 'radio',
			'id'    => sanitize_title( $field['id'] . '-' . $value ),
			'name'  => $field['name'],
			'value' => $value,
		);

		// Check if selected.
		if ( esc_attr( $value ) === esc_attr( $checked ) ) {
			$attrs['checked'] = 'checked';
			$is_selected      = true;
		}

		// Check if is disabled.
		if ( isset( $field['disabled'] ) && acf_in_array( $value, $field['disabled'] ) ) {
			$attrs['disabled'] = 'disabled';
		}

		// Additional HTML (the "Other" input).
		$additional_html = '';
		if ( $value === 'other' && $other_input ) {
			$additional_html = ' ' . acf_get_text_input( $other_input );
		}

		// append
		$e .= '<li><label' . ( $is_selected ? ' class="selected"' : '' ) . '><input ' . acf_esc_attr( $attrs ) . '/>' . acf_esc_html( $label ) . '</label>' . $additional_html . '</li>';
	}

	// Close <ul>.
	$e .= '</ul>';

	// Output HTML.
	echo $e;
}