generated from pascalmartineau/wp-skeleton
feat: template replacement
Some checks failed
Deploy WordPress and Nuxt / deploy (push) Has been cancelled
Some checks failed
Deploy WordPress and Nuxt / deploy (push) Has been cancelled
This commit is contained in:
45
wp-content/themes/ccat/includes/core/sections.php
Normal file
45
wp-content/themes/ccat/includes/core/sections.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
// Helper: Process sections and replace templates with their content
|
||||
function ccat_process_sections( $sections ) {
|
||||
if ( ! is_array( $sections ) ) {
|
||||
return $sections;
|
||||
}
|
||||
$processed_sections = array();
|
||||
foreach ( $sections as $section ) {
|
||||
// Check if this is a template section
|
||||
if ( isset( $section['acf_fc_layout'] ) && $section['acf_fc_layout'] === 'template' ) {
|
||||
$template_id = $section['template'] ?? null;
|
||||
if ( $template_id ) {
|
||||
$template_sections = get_field( 'sections', $template_id );
|
||||
if ( is_array( $template_sections ) ) {
|
||||
$expanded_sections = ccat_process_sections( $template_sections );
|
||||
foreach ( $expanded_sections as $expanded_section ) {
|
||||
$processed_sections[] = $expanded_section;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$processed_sections[] = $section;
|
||||
}
|
||||
}
|
||||
return $processed_sections;
|
||||
}
|
||||
|
||||
// Hook into ACF get_field to process sections
|
||||
add_filter( 'acf/format_value/name=sections', 'ccat_format_sections_value' );
|
||||
function ccat_format_sections_value( $value ) {
|
||||
if ( ! is_array( $value ) ) {
|
||||
return $value;
|
||||
}
|
||||
return ccat_process_sections( $value );
|
||||
}
|
||||
|
||||
// Hook into GraphQL to process sections when queried
|
||||
add_filter( 'graphql_acf_get_field_value', 'ccat_graphql_process_sections', 10, 2 );
|
||||
function ccat_graphql_process_sections( $value, $acf_field ) {
|
||||
if ( isset( $acf_field['name'] ) && $acf_field['name'] === 'sections' && is_array( $value ) ) {
|
||||
return ccat_process_sections( $value );
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
@@ -83,10 +83,11 @@ function ccat_get_graphql_preview_data() {
|
||||
if ( empty( $layout_name = get_row_layout() ) ) {
|
||||
throw new Exception( "Erreur de prévisualisation (section invalide)" );
|
||||
}
|
||||
$result = array(
|
||||
$result = array(
|
||||
'fieldGroupName' => ccat_get_graphql_field_group_name( 'group_abstract_builder', $layout_name ),
|
||||
);
|
||||
foreach ( get_row( true ) as $key => $value ) {
|
||||
$format_value = $result['fieldGroupName'] !== 'GroupAbstractBuilderSectionsTemplateLayout';
|
||||
foreach ( get_row( $format_value ) ?: array() as $key => $value ) {
|
||||
if ( strpos( $key, 'acf_fc_layout' ) !== 0 && strpos( $key, '_' ) !== 0 ) {
|
||||
$result[ ccat_get_graphql_field_name( array( 'name' => $key ) ) ] = $value;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user