ACF\Pro\Datastore

Localization::localize_field_store_dataprivateACF 6.8.1

Localizes field group definitions and values for the ACF @wordpress/data store.

Gathers all field groups visible on the current post, serializes their field definitions and current values, and passes them to JS via acf_localize_data(). This data initializes the 'acf/fields' store which powers JS-side block bindings and developer access.

Method of the class: Localization{}

No Hooks.

Returns

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->localize_field_store_data();

Changelog

Since 6.8.1 Introduced.

Localization::localize_field_store_data() code ACF 6.8.8

private function localize_field_store_data() {
	global $post;

	if ( ! $post ) {
		return;
	}

	$field_groups = acf_get_field_groups( array( 'post_id' => $post->ID ) );

	if ( empty( $field_groups ) ) {
		return;
	}

	$store_data = array(
		'context'     => array(
			'postId'   => $post->ID,
			'postType' => $post->post_type,
		),
		'fields'      => array(),
		'values'      => array(),
		'fieldGroups' => array(),
	);

	foreach ( $field_groups as $field_group ) {
		$store_data['fieldGroups'][] = array(
			'key'                   => $field_group['key'],
			'title'                 => acf_esc_html( acf_get_field_group_title( $field_group ) ),
			'position'              => $field_group['position'],
			'style'                 => $field_group['style'],
			'label_placement'       => $field_group['label_placement'],
			'instruction_placement' => $field_group['instruction_placement'],
			'edit_url'              => esc_url( acf_get_field_group_edit_link( $field_group['ID'] ) ),
		);

		$fields = acf_get_fields( $field_group );
		if ( $fields ) {
			$this->collect_field_data( $fields, $post->ID, $field_group['key'], $store_data );
		}
	}

	acf_localize_data( array( 'storeData' => $store_data ) );
}