acf_field__group::format_value_for_jsonld │ public │ ACF 6.8.0

Formats the field value for JSON-LD output.

Method of the class: acf_field__group{}

No Hooks.

Returns

mixed.

Usage

$acf_field__group = new acf_field__group();
$acf_field__group->format_value_for_jsonld( $value, $post_id, $field );
$value(mixed) (required)
The value of the field.
$post_id(int|string) (required)
The ID of the post.
$field(array) (required)
The field array.

Changelog

Since 6.8.0 Introduced.

acf_field__group::format_value_for_jsonld() code ACF 6.8.8

public function format_value_for_jsonld( $value, $post_id, $field ) {
	if ( empty( $value ) || ! is_array( $value ) || empty( $field['sub_fields'] ) ) {
		return null;
	}

	// Get output format with fallback.
	$output_format = $field['schema_output_format'] ?? '';
	if ( empty( $output_format ) ) {
		$property      = $field['schema_property'] ?? '';
		$output_format = \ACF\AI\GEO\Schema::get_default_output_format( $this->name, $property );
	}

	// Build the JSON-LD object.
	$result = array();

	// Add @type if we have a specific output format.
	if ( ! empty( $output_format ) ) {
		$result['@type'] = $output_format;
	}

	// Modify names for DB access.
	$field = $this->prepare_field_for_db( $field );

	// Process each sub field.
	foreach ( $field['sub_fields'] as $sub_field ) {
		// Get the sub field value.
		$sub_value = $value[ $sub_field['key'] ] ?? $value[ $sub_field['_name'] ] ?? null;

		if ( null === $sub_value ) {
			continue;
		}

		// Check if sub field has a schema property mapping.
		$schema_property = $sub_field['schema_property'] ?? '';

		if ( empty( $schema_property ) ) {
			// No schema property - skip this field for JSON-LD output.
			continue;
		}

		// Parse qualified property (e.g., "Thing.name" -> "name") to strip type prefix.
		$property_name = \ACF\AI\GEO\Schema::get_property_name( $schema_property );

		// Format the sub field value for JSON-LD.
		$formatted_value = \ACF\AI\GEO\GEO::format_field_value_for_jsonld( $sub_value, $sub_field );

		if ( null !== $formatted_value ) {
			$result[ $property_name ] = $formatted_value;
		}
	}

	// Only return if we have content beyond @type.
	if ( count( $result ) > 1 || ( count( $result ) === 1 && ! isset( $result['@type'] ) ) ) {
		return $result;
	}

	return null;
}