I would like to move an ACF Flexible Content Field into Sage, specifically focusing on how to handle Relationship Fields or other loop fields inside a Flexible Content field.
Default ACF way:
<?php if ( have_rows( 'content_blocks' ) ): ?>
<?php while ( have_rows( 'content_blocks' ) ) : the_row(); ?>
<?php if ( get_row_layout() == 'text_block' ) : ?>
<?php the_sub_field( 'text_block_text' ); ?>
<?php elseif ( get_row_layout() == 'producers' ) : ?>
<?php the_sub_field( 'producer_title' ); ?>
<?php $featured_producers = get_sub_field( 'featured_producers' ); ?>
<?php if ( $featured_producers ): ?>
<?php foreach ( $featured_producers as $post ): ?>
<?php setup_postdata ( $post ); ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php $producers = get_sub_field( 'producers' ); ?>
<?php if ( $producers ): ?>
<?php foreach ( $producers as $post ): ?>
<?php setup_postdata ( $post ); ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php elseif ( get_row_layout() == 'stats' ) : ?>
<?php if ( have_rows( 'stats_items' ) ) : ?>
<?php while ( have_rows( 'stats_items' ) ) : the_row(); ?>
<?php $stats_items_image = get_sub_field( 'stats_items_image' ); ?>
<?php if ( $stats_items_image ) { ?>
<?php echo wp_get_attachment_image( $stats_items_image, 'full' ); ?>
<?php } ?>
<?php the_sub_field( 'stats_items_number' ); ?>
<?php the_sub_field( 'stats_items_text' ); ?>
<?php endwhile; ?>
<?php else : ?>
<?php // no rows found ?>
<?php endif; ?>
<?php endif; ?>
<?php endwhile; ?>
<?php else: ?>
<?php // no layouts found ?>
<?php endif; ?>
My Controller - app/Controllers/Page.php - for page.php as the ACF Flexible Content Field is default for every page. (Setup from this posts - Anybody using any page builders with sage?)
namespace App\Controllers;
use Sober\Controller\Controller;
class Page extends Controller
{
public function page_builder()
{
$page_builder = get_field('content_blocks');
$data = [];
foreach ($page_builder as $block) {
if ($block['acf_fc_layout'] == 'text_block') {
$this_block = (object) [
'block_type' => $block['acf_fc_layout'],
'text_block_text' => $block['text_block_text'],
];
array_push($data, $this_block);
} elseif ($block['acf_fc_layout'] == 'producers') {
$this_block = (object) [
'block_type' => $block['acf_fc_layout'],
'producer_title' => $block['producer_title'],
];
array_push($data, $this_block);
} elseif ($block['acf_fc_layout'] == 'stats') {
$this_block = (object) [
'block_type' => $block['acf_fc_layout'],
];
array_push($data, $this_block);
}
}
$data = (object) $data;
return $data;
}
}
My view:
@foreach ($page_builder as $block)
@if ($block->block_type == 'text_block')
@include('partials.page-builder.text-block')
@elseif ($block->block_type == 'producers')
@include('partials.page-builder.producers')
@elseif ($block->block_type == 'stats')
@include('partials.page-builder.stats')
@endif
@endforeach
The partials then access the data like - $block->text_block_text
What I need to do is include the logic for a Relationship Field or other loops inside these Objects, then figure out how to access that data.
$this_block = (object) [
'block_type' => $block['acf_fc_layout'],
**LOOP / REALTIONSHIP HERE**
];