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()
3 years ago

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

3 years ago
1
Changed the status to
In upcoming releases
3 years ago
1
Changed the status to
Completed
3 years ago