ACF\Pro\Datastore

Localization::collect_field_datapublicACF 6.8.1

Recursively collects field definitions and values for the store.

Processes an array of fields, loading each field's value and adding it to the store data structure. For complex fields (repeater, group, flexible content), recurses into sub-fields to build nested values.

Method of the class: Localization{}

Hooks from the method

Returns

null. Nothing (null).

Usage

$Localization = new Localization();
$Localization->collect_field_data( $fields, $post_id, $field_group_key, $store_data );
$fields(array) (required)
Array of field arrays.
$post_id(int) (required)
The post ID to load values for.
$field_group_key(string) (required)
The parent field group's key.
$store_data(array) (required) (passed by reference — &)
Reference to the store data being built.

Changelog

Since 6.8.1 Introduced.

Localization::collect_field_data() code ACF 6.8.8

public function collect_field_data( $fields, $post_id, $field_group_key, &$store_data ) {
	foreach ( $fields as $field ) {
		$field = apply_filters( 'acf/prepare_field', $field );
		if ( ! $field ) {
			continue;
		}

		$field_def = array(
			'key'           => $field['key'],
			'name'          => $field['name'],
			'type'          => $field['type'],
			'label'         => acf_esc_html( $field['label'] ),
			'parent'        => $field['parent'],
			'fieldGroupKey' => $field_group_key,
		);

		if ( ! acf_field_type_supports( $field['type'], 'bindings', true ) ) {
			$field_def['supportsBindings'] = false;
		}

		if ( isset( $field['allow_in_bindings'] ) ) {
			$field_def['allowInBindings'] = (bool) $field['allow_in_bindings'];
		}

		// Repeaters with pagination enabled need unique handling in the datastore.
		if ( 'repeater' === $field['type'] ) {
			$field_def['pagination'] = ! empty( $field['pagination'] );
		}

		// Include sub_fields metadata for complex types.
		if ( ! empty( $field['sub_fields'] ) ) {
			$field_def['subFields'] = array();
			foreach ( $field['sub_fields'] as $sub_field ) {
				if ( apply_filters( 'acf/prepare_field', $sub_field ) ) {
					$field_def['subFields'][] = $sub_field['key'];
				}
			}
		}

		// Include layouts for flexible content.
		if ( ! empty( $field['layouts'] ) ) {
			$field_def['layouts'] = array();
			foreach ( $field['layouts'] as $layout ) {
				$layout_def = array(
					'key'   => $layout['key'],
					'name'  => $layout['name'],
					'label' => acf_esc_html( $layout['label'] ),
				);
				if ( ! empty( $layout['sub_fields'] ) ) {
					$layout_def['subFields'] = array();
					foreach ( $layout['sub_fields'] as $sub_field ) {
						if ( apply_filters( 'acf/prepare_field', $sub_field ) ) {
							$layout_def['subFields'][] = $sub_field['key'];
						}
					}
				}
				$field_def['layouts'][] = $layout_def;
			}
		}

		$store_data['fields'][ $field['key'] ] = $field_def;

		// Load the field value.
		//
		// Skip paginated repeaters: the value would be discarded by
		// reconcileWithDOM in JS (visible-page DOM rows overwrite it
		// on init), and acf_get_value() would cache the unsliced row
		// set here -- load_value()'s pagination slice is gated on
		// $is_rendering, which isn't set until pre_render_fields fires
		// for the metabox. The cached unsliced value would then be
		// reused by the render and show all rows on page 1.
		if ( 'repeater' === $field['type'] && ! empty( $field['pagination'] ) ) {
			$store_data['values'][ $field['key'] ] = array();
		} else {
			$value                                 = acf_get_value( $post_id, $field );
			$store_data['values'][ $field['key'] ] = $this->serialize_field_value( $field, $value, $post_id );
		}

		// Register sub-fields in the store for complex types.
		if ( ! empty( $field['sub_fields'] ) ) {
			$this->register_sub_fields( $field['sub_fields'], $field_group_key, $store_data );
		}

		// Register layout sub-fields for flexible content.
		if ( ! empty( $field['layouts'] ) ) {
			foreach ( $field['layouts'] as $layout ) {
				if ( ! empty( $layout['sub_fields'] ) ) {
					$this->register_sub_fields( $layout['sub_fields'], $field_group_key, $store_data );
				}
			}
		}
	}
}