Column Settings block idea

I’ve noticed this question asked quite a lot and wanted to share a solution I’ve been using recently. I created a custom block to pass settings to columns within a layout field. For example, controlling Flexbox attributes (could be background colour, etc…):

I then check for this block in the frontend using $column->blocks()->hasType('flex'). It’s working well so far. If I don’t want content editors to see or use these blocks, I hide them using CSS, which isn’t ideal but does the job.

3 Likes

A very good approach! I previously solved this using a structure field in the layout, but I think this variant is actually better, I rebuild this in my new pagebuilder. Thanks for the tip, was a really good inspiration :blush:

Do you also use something like $column->blocks()->findBy('type', 'columnSettings'); to access the contents of the block?

Maybe not the most elegant, but I do check for each column :

foreach ($layout->columns() as $column):
    $blocks = $column->blocks();
    if ($blocks->hasType('column-settings')):
        $settings = $blocks->findBy('type', 'column-settings');
        # Use your settings
    endif;
    # output blocks
endforeach;

Ah ok, yes I’ll do it almost the same way :smile:

$settings = $column->blocks()->findBy('type', 'column-settings');
if ( $column->blocks()->hasType('column-settings') ) {
  $width = $settings->container_width()->value(); // for example
}