Empty layout sections get rendered

Created on 2 July 2019, almost 6 years ago
Updated 25 January 2023, over 2 years ago

Problem/Motivation

Empty sections create unnecessary markup. Also, if you've styled the sections with a background/padding you'll get some unwanted bars.

Proposed resolution

Remove empty sections while preserving their cache metadata.

Remaining tasks

User interface changes

N/A

API changes

N/A

Data model changes

N/A

Release notes snippet

๐Ÿ› Bug report
Status

Needs work

Version

10.1 โœจ

Component
Layout builderย  โ†’

Last updated 1 day ago

Created by

๐Ÿ‡ฉ๐Ÿ‡ชGermany Corn696 Flensburg

Live updates comments and jobs are added and updated live.
  • Blocks-Layouts

    Blocks and Layouts Initiative. See the #2811175 Add layouts to Drupal issue.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • ๐Ÿ‡ช๐Ÿ‡ธSpain javier_rey

    Adding a patch for the 10.1.x branch.

  • Status changed to Needs review over 2 years ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States tim.plunkett Philadelphia

    Fixed a couple more tests

  • Status changed to Needs work over 2 years ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Thank you for this! See this often with clients who arenโ€™t great with layout builder.

    Still seem to be some test failures however. Definitely keeping this on the radar

  • Status changed to Needs review about 2 years ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    @tim.plunkett added a condition so the sections do build in layout builder side.

    Thoughts?

  • Status changed to Needs work about 2 years ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Thanks. Also seemed to cause some additional test failures that I'll look at.

  • Status changed to Needs review about 2 years ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    So this additional checks seems to be the best way to cover all scenarios.

    When adding a section _layout_builder isn't there.

    Tried _admin_route but that caused test failures also.

  • Assigned to tim.plunkett
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States tim.plunkett Philadelphia

    Fixing merge (using rebase) to allow me to review better

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    @tim.plunkett wonder if you had a chance to review?

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States tim.plunkett Philadelphia

    I'll have to re-review, the change to checking for `layout_builder_tempstore` is tricky

  • Status changed to Needs work almost 2 years ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Tried this out on a local project and I use drush search-api-index

    But I get this error

    > [error] Error while trying to render item entity:node/48:en with view mode full for search index tetra_tech: Call to a member function getOption() on null in Drupal\Core\Layout\LayoutDefault->build() (line 70 of /var/www/html/webroot/core/lib/Drupal/Core/Layout/LayoutDefault.php).

    So seems all scenarios aren't caught.

  • ๐Ÿ‡ช๐Ÿ‡จEcuador LeonelEnriquez98

    Adding a patch for the 10.2.x branch

  • ๐Ÿ‡ช๐Ÿ‡ธSpain tonibarbera

    Rerolled the patch for the 10.3.1 branch

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia krishna mishra

    I get same error for media on solr indexing:
    [error] Error while trying to render item entity:media/XX:en with view mode search_index for search index INDEX-ABC: Call to a member function getOption() on null in Drupal\Core\Layout\LayoutDefault->build() (line 70 of web/core/lib/Drupal/Core/Layout/LayoutDefault.php).

  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands bbrala Netherlands
  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands bbrala Netherlands
  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands bbrala Netherlands

    Sorry for the noise, tool a random issue to check something. Please ignore.

  • ๐Ÿ‡ง๐Ÿ‡ดBolivia xjkwak

    I encountered the same issue described in #29 when applying the patch from #28. The problem occurs because \Drupal::routeMatch()->getRouteObject() returns null in non-route contexts (e.g., during Search API indexing via cron), which leads to a fatal error when getOption() is called.

    To fix this, I updated the patch to use the null-safe operator, ensuring the code does not break when the route object is unavailable:

    $temp_store = FALSE;
    if (!empty(\Drupal::routeMatch()?->getRouteObject()?->getOption('parameters')) &&
        isset(\Drupal::routeMatch()?->getRouteObject()?->getOption('parameters')['section_storage'])) {
      $temp_store = \Drupal::routeMatch()->getRouteObject()->getOption('parameters')['section_storage']['layout_builder_tempstore'];
    }
    

    This change ensures compatibility with CLI and cron environments. I've also attached an updated patch that includes this fix. It's my first contribution to Drupal :)

Production build 0.71.5 2024