Files
Pascal Martineau 3119f29218
Some checks failed
Deploy WordPress and Nuxt / deploy (push) Has been cancelled
feat: template replacement
2025-09-25 11:33:02 -04:00

46 lines
1.4 KiB
PHP

<?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;
}