Sorry, we don't support your browser.  Install a modern browser
This post is closed.

Layout field: `hasBlocks` or `isEmpty` method#200

I currently use a custom function which accepts a $layout object as parameter. This function checks to see if the layout has any, non-hidden blocks. This way it is very easy to avoid rendering a layout row with no blocks (or only hidden blocks).

function layoutHasBlocks ($layout) {
  foreach ($layout->columns() as $column) {
    // if has blocks
    if ($column->blocks()->isNotEmpty()) {
      foreach ($column->blocks() as $block) {
        // if has a visible block
        if (!$block->isHidden()) {
          return true;
        }
      }
    }
  }
}

Ideally this would be a method of the $layout object, so could be easily used as:

$layout->hasBlocks()
a year ago

I do the same but I had no idea you can hide blocks until now. lol Thanks for posting the code

a year ago
1
Changed the status to
In upcoming releases
10 months ago
1
Changed the status to
Completed
10 months ago