acf_field_color_picker::render_fieldpublicACF 3.6

Create the HTML interface for your field

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

Хуков нет.

Возвращает

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

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

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

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

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

Код acf_field_color_picker::render_field() ACF 6.8.8

<?php
public function render_field( $field ) {
	$text_input                             = acf_get_sub_array( $field, array( 'id', 'class', 'name', 'value' ) );
	$hidden_input                           = acf_get_sub_array( $field, array( 'name', 'value' ) );
	$text_input['data-alpha-skip-debounce'] = true;

	// Color picker alpha library requires a specific data attribute to exist.
	if ( $field['enable_opacity'] ) {
		$text_input['data-alpha-enabled'] = true;
	}

	// Handle color palette when the theme supports theme.json.
	if ( wp_theme_has_theme_json() ) {
		// If the field was set to use themejson.
		if ( $field['custom_palette_source'] === 'themejson' ) {
			$text_input['data-acf-palette-type'] = 'custom';

			// Get the palette (theme + custom).
			$global_settings = wp_get_global_settings();
			$palette         = $global_settings['color']['palette']['theme'] ?? array();

			// Extract only the color values.
			$color_values = array_map(
				fn( $c ) => $c['color'] ?? null,
				$palette
			);

			// Remove nulls (in case any entries are missing 'color')
			$color_values = array_filter( $color_values );

			$hex_string = implode( ',', $color_values );

			$text_input['data-acf-palette-colors'] = $hex_string;
		} elseif ( $field['custom_palette_source'] === 'custom' && ! empty( $field['palette_colors'] ) ) {
			// If the field was set to use a custom palette.
			$text_input['data-acf-palette-type']   = 'custom';
			$text_input['data-acf-palette-colors'] = $field['palette_colors'];
		} elseif ( $field['custom_palette_source'] === '' && ! empty( $field['palette_colors'] ) ) {
			// This state can happen if they switched from a classic theme to a themejson theme without resaving the field.
			$text_input['data-acf-palette-type']   = 'custom';
			$text_input['data-acf-palette-colors'] = $field['palette_colors'];
		} else {
			// Fallback to use the default color palette for the iris color picker.
			$text_input['data-acf-palette-type'] = 'default';
		}
	} else {
		// Handle color palette for themes that do not support themejson.
		if ( ! empty( $field['palette_colors'] ) ) {
			$text_input['data-acf-palette-type']   = 'custom';
			$text_input['data-acf-palette-colors'] = $field['palette_colors'];
		} else {
			// Fallback to use the default color palette for the iris color picker.
			$text_input['data-acf-palette-type'] = 'default';
		}
	}

	// html
	?>
<div class="acf-color-picker<?php echo esc_attr( ! $field['show_color_wheel'] ? ' acf-hide-color-picker-color-wheel' : '' ); ?>">
	<?php acf_hidden_input( $hidden_input ); ?>
	<?php acf_text_input( $text_input ); ?>
</div>
	<?php
}