From 68acbae0126371862ac11be0cb05857efcc61d7a Mon Sep 17 00:00:00 2001 From: Pascal Martineau Date: Wed, 24 Sep 2025 11:50:06 -0400 Subject: [PATCH] wip: preview using GraphQL value --- wp-content/themes/ccat/layouts/preview.php | 265 ++++++++++++++++++++- 1 file changed, 264 insertions(+), 1 deletion(-) diff --git a/wp-content/themes/ccat/layouts/preview.php b/wp-content/themes/ccat/layouts/preview.php index 84b7d79..ad4fabc 100644 --- a/wp-content/themes/ccat/layouts/preview.php +++ b/wp-content/themes/ccat/layouts/preview.php @@ -1,7 +1,270 @@ $value ) { + if ( strpos( $key, 'acf_fc_layout' ) === 0 || strpos( $key, '_' ) === 0 ) { + continue; + } + $graphql_key = get_graphql_field_name( array( 'name' => $key ) ); + $formatted_data[ $graphql_key ] = $value; + } + return $formatted_data; + } + + $active_loop = $acf_loop['active']; + $field_config = $active_loop['field']; + + // Find the current layout configuration + $layout_config = null; + if ( isset( $field_config['layouts'] ) ) { + foreach ( $field_config['layouts'] as $layout ) { + if ( $layout['name'] === $layout_name ) { + $layout_config = $layout; + break; + } + } + } + + if ( ! $layout_config || ! isset( $layout_config['sub_fields'] ) ) { + // Fallback if layout config not found + $raw_data = get_row( true ); + $formatted_data = array(); + foreach ( $raw_data as $key => $value ) { + if ( strpos( $key, 'acf_fc_layout' ) === 0 || strpos( $key, '_' ) === 0 ) { + continue; + } + $graphql_key = get_graphql_field_name( array( 'name' => $key ) ); + $formatted_data[ $graphql_key ] = $value; + } + return $formatted_data; + } + + // Resolve fields using the actual layout configuration with proper field access + return resolve_layout_fields( $layout_config['sub_fields'] ); +} + +// Resolve fields within a flexible content layout context +function resolve_layout_fields( $field_configs ) { + $resolved_data = array(); + + foreach ( $field_configs as $field_config ) { + $graphql_field_name = get_graphql_field_name( $field_config ); + $field_value = null; + + // Handle different field types with their proper access methods + switch ( $field_config['type'] ) { + case 'repeater': + // For layout_settings repeater (which acts like settings) + if ( $field_config['name'] === 'layout_settings' ) { + // layout_settings is a single-row repeater, get the first (and usually only) row + if ( have_rows( 'layout_settings' ) ) { + the_row(); + $field_value = resolve_settings_fields( $field_config['sub_fields'] ); + wp_reset_postdata(); // Reset after the_row() + } + } else { + // Regular repeater field + $repeater_data = array(); + if ( have_rows( $field_config['name'] ) ) { + while ( have_rows( $field_config['name'] ) ) { + the_row(); + $repeater_data[] = resolve_settings_fields( $field_config['sub_fields'] ); + } + wp_reset_postdata(); + } + $field_value = $repeater_data; + } + break; + + case 'group': + // Regular group field + $group_value = get_sub_field( $field_config['name'] ); + if ( is_array( $group_value ) && isset( $field_config['sub_fields'] ) ) { + $field_value = resolve_group_fields( $field_config['sub_fields'], $group_value ); + } else { + $field_value = $group_value; + } + break; + + default: + // Regular field + $field_value = get_sub_field( $field_config['name'] ); + break; + } + + $resolved_data[ $graphql_field_name ] = $field_value; + } + + return $resolved_data; +} + +// Resolve fields within repeater/settings context (using get_sub_field) +function resolve_settings_fields( $sub_fields ) { + $resolved_settings = array(); + + foreach ( $sub_fields as $sub_field ) { + $sub_field_graphql_name = get_graphql_field_name( $sub_field ); + $sub_field_value = get_sub_field( $sub_field['name'] ); + + // Handle nested structures within settings + if ( isset( $sub_field['type'] ) && $sub_field_value !== null ) { + switch ( $sub_field['type'] ) { + case 'group': + if ( isset( $sub_field['sub_fields'] ) && is_array( $sub_field_value ) ) { + $sub_field_value = resolve_group_fields( $sub_field['sub_fields'], $sub_field_value ); + } + break; + + case 'repeater': + // Nested repeater within settings - this would be complex, handle as needed + // For now, leave as-is + break; + } + } + + $resolved_settings[ $sub_field_graphql_name ] = $sub_field_value; + } + + return $resolved_settings; +} + $preview_url = ( defined( 'NUXT_PUBLIC_SITE_URL' ) ? NUXT_PUBLIC_SITE_URL : 'https://cultureat.ca' ) . '/api/acf-preview'; $data = array( - 'layout' => get_row_layout(), + 'fieldGroupName' => get_row_layout(), + 'data' => get_row_graphql_formatted(), ); $result = wp_remote_post( $preview_url, array( 'body' => $data ) ); if ( is_wp_error( $result ) ) {