acf_render_field_wrap()ACF 5.0.0

acf_render_field_wrap

Render the wrapping element for a given field.

Хуки из функции

Возвращает

null. Ничего.

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

acf_render_field_wrap( $field, $element, $instruction );
$field(массив) (обязательный)
The field array.
$element(строка)
The wrapping element type.
По умолчанию: 'div'
$instruction(строка)
The instruction render position (label|field).
По умолчанию: 'label'

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

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

Код acf_render_field_wrap() ACF 5.10.2

function acf_render_field_wrap( $field, $element = 'div', $instruction = 'label' ) {

	// Ensure field is complete (adds all settings).
	$field = acf_validate_field( $field );

	// Prepare field for input (modifies settings).
	$field = acf_prepare_field( $field );

	// Allow filters to cancel render.
	if ( ! $field ) {
		return;
	}

	// Determine wrapping element.
	$elements = array(
		'div' => 'div',
		'tr'  => 'td',
		'td'  => 'div',
		'ul'  => 'li',
		'ol'  => 'li',
		'dl'  => 'dt',
	);

	if ( isset( $elements[ $element ] ) ) {
		$inner_element = $elements[ $element ];
	} else {
		$element = $inner_element = 'div';
	}

	// Generate wrapper attributes.
	$wrapper = array(
		'id'        => '',
		'class'     => 'acf-field',
		'width'     => '',
		'style'     => '',
		'data-name' => $field['_name'],
		'data-type' => $field['type'],
		'data-key'  => $field['key'],
	);

	// Add field type attributes.
	$wrapper['class'] .= " acf-field-{$field['type']}";

	// add field key attributes
	if ( $field['key'] ) {
		$wrapper['class'] .= " acf-field-{$field['key']}";
	}

	// Add required attributes.
	// Todo: Remove data-required
	if ( $field['required'] ) {
		$wrapper['class']        .= ' is-required';
		$wrapper['data-required'] = 1;
	}

	// Clean up class attribute.
	$wrapper['class'] = str_replace( '_', '-', $wrapper['class'] );
	$wrapper['class'] = str_replace( 'field-field-', 'field-', $wrapper['class'] );

	// Merge in field 'wrapper' setting without destroying class and style.
	if ( $field['wrapper'] ) {
		$wrapper = acf_merge_attributes( $wrapper, $field['wrapper'] );
	}

	// Extract wrapper width and generate style.
	// Todo: Move from $wrapper out into $field.
	$width = acf_extract_var( $wrapper, 'width' );
	if ( $width ) {
		$width = acf_numval( $width );
		if ( $element !== 'tr' && $element !== 'td' ) {
			$wrapper['data-width'] = $width;
			$wrapper['style']     .= " width:{$width}%;";
		}
	}

	// Clean up all attributes.
	$wrapper = array_map( 'trim', $wrapper );
	$wrapper = array_filter( $wrapper );

	/**
	 * Filters the $wrapper array before rendering.
	 *
	 * @date    21/1/19
	 * @since   5.7.10
	 *
	 * @param   array $wrapper The wrapper attributes array.
	 * @param   array $field The field array.
	 */
	$wrapper = apply_filters( 'acf/field_wrapper_attributes', $wrapper, $field );

	// Append conditional logic attributes.
	if ( ! empty( $field['conditional_logic'] ) ) {
		$wrapper['data-conditions'] = $field['conditional_logic'];
	}
	if ( ! empty( $field['conditions'] ) ) {
		$wrapper['data-conditions'] = $field['conditions'];
	}

	// Vars for render.
	$attributes_html = acf_esc_attr( $wrapper );

	// Render HTML
	echo "<$element $attributes_html>" . "\n";
	if ( $element !== 'td' ) {
		echo "<$inner_element class=\"acf-label\">" . "\n";
			acf_render_field_label( $field );
		if ( $instruction == 'label' ) {
			acf_render_field_instructions( $field );
		}
			echo "</$inner_element>" . "\n";
	}
		echo "<$inner_element class=\"acf-input\">" . "\n";
			acf_render_field( $field );
	if ( $instruction == 'field' ) {
		acf_render_field_instructions( $field );
	}
		echo "</$inner_element>" . "\n";
	echo "</$element>" . "\n";
}