acf_field_select::render_fieldpublicACF 3.6

Creates the HTML interface for the field.

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

Хуков нет.

Возвращает

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

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

$acf_field_select = new acf_field_select();
$acf_field_select->render_field( $field );
$field(массив) (обязательный)
An array holding all the field's data.

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

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

Код acf_field_select::render_field() ACF 6.4.2

public function render_field( $field ) {
	$value   = acf_get_array( $field['value'] );
	$choices = acf_get_array( $field['choices'] );

	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;
	}

	$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'],
	);

	if ( ! empty( $field['aria-label'] ) ) {
		$select['aria-label'] = $field['aria-label'];
	}

	if ( $field['multiple'] ) {
		$select['multiple'] = 'multiple';
		$select['size']     = 5;
		$select['name']    .= '[]';

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

	if ( ! empty( $field['create_options'] ) && $field['ui'] ) {
		$select['data-create_options'] = true;
	}

	// 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'];
	}
	if ( ! empty( $field['nonce'] ) ) {
		$select['data-nonce'] = $field['nonce'];
	}
	if ( $field['ajax'] && empty( $field['nonce'] ) && acf_is_field_key( $field['key'] ) ) {
		$select['data-nonce'] = wp_create_nonce( 'acf_field_' . $this->name . '_' . $field['key'] );
	}
	if ( ! empty( $field['hide_search'] ) ) {
		$select['data-minimum-results-for-search'] = '-1';
	}

	// 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'],
			)
		);
	}

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

	if ( ! empty( $field['create_options'] ) && $field['ui'] && is_array( $field['value'] ) ) {
		foreach ( $field['value'] as $value ) {
			// Already exists in choices.
			if ( isset( $field['choices'][ $value ] ) ) {
				continue;
			}

			$option = esc_attr( $value );

			$select['choices'][ $option ] = $option;
		}
	}

	acf_select_input( $select );
}