acf_field_select::render_field()publicACF 3.6

Create the HTML interface for your field

{} Это метод класса: acf_field_select{}

Хуков нет.

Возвращает

null. Ничего.

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

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

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

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

Код acf_field_select::render_field() ACF 5.10.2

function render_field( $field ) {

	// convert
	$value   = acf_get_array( $field['value'] );
	$choices = acf_get_array( $field['choices'] );

	// placeholder
	if ( empty( $field['placeholder'] ) ) {
		$field['placeholder'] = _x( 'Select', 'verb', 'acf' );
	}

	// add empty value (allows '' to be selected)
	if ( empty( $value ) ) {
		$value = array( '' );
	}

	// prepend empty choice
	// - only for single selects
	// - have tried array_merge but this causes keys to re-index if is numeric (post ID's)
	if ( $field['allow_null'] && ! $field['multiple'] ) {
		$choices = array( '' => "- {$field['placeholder']} -" ) + $choices;
	}

	// clean up choices if using ajax
	if ( $field['ui'] && $field['ajax'] ) {
		$minimal = array();
		foreach ( $value as $key ) {
			if ( isset( $choices[ $key ] ) ) {
				$minimal[ $key ] = $choices[ $key ];
			}
		}
		$choices = $minimal;
	}

	// vars
	$select = array(
		'id'               => $field['id'],
		'class'            => $field['class'],
		'name'             => $field['name'],
		'data-ui'          => $field['ui'],
		'data-ajax'        => $field['ajax'],
		'data-multiple'    => $field['multiple'],
		'data-placeholder' => $field['placeholder'],
		'data-allow_null'  => $field['allow_null'],
	);

	// multiple
	if ( $field['multiple'] ) {

		$select['multiple'] = 'multiple';
		$select['size']     = 5;
		$select['name']    .= '[]';

		// Reduce size to single line if UI.
		if ( $field['ui'] ) {
			$select['size'] = 1;
		}
	}

	// special atts
	if ( ! empty( $field['readonly'] ) ) {
		$select['readonly'] = 'readonly';
	}
	if ( ! empty( $field['disabled'] ) ) {
		$select['disabled'] = 'disabled';
	}
	if ( ! empty( $field['ajax_action'] ) ) {
		$select['data-ajax_action'] = $field['ajax_action'];
	}

	// hidden input is needed to allow validation to see <select> element with no selected value
	if ( $field['multiple'] || $field['ui'] ) {
		acf_hidden_input(
			array(
				'id'   => $field['id'] . '-input',
				'name' => $field['name'],
			)
		);
	}

	// append
	$select['value']   = $value;
	$select['choices'] = $choices;

	// render
	acf_select_input( $select );

}