More defensive handling of empty $contexts['layout_builder.entity'] and broken blocks

Created on 11 March 2020, almost 5 years ago
Updated 13 October 2024, 2 months ago

Using page_manager with #2960739: Create a layout builder variant led us into some edge case situations which should perhaps be handled more defensive in onBuildRender(). Just wanted to let you know to decide how to handle these cases:

Current function looks like this:
Drupal\layout_builder_st\EventSubscriber\ComponentPluginTranslate->onBuildRender():

/**
   * Translates the plugin configuration if needed.
   *
   * @param \Drupal\layout_builder\Event\SectionComponentBuildRenderArrayEvent $event
   *   The section component render event.
   */
  public function onBuildRender(SectionComponentBuildRenderArrayEvent $event) {
    if (!$this->languageManager->isMultilingual()) {
      return;
    }
    $plugin = $event->getPlugin();
    $contexts = $event->getContexts();
    $component = $event->getComponent();
    if (!$plugin instanceof ConfigurableInterface && !isset($contexts['layout_builder.entity'])) {
      return;
    }

    // @todo Change to 'entity' in https://www.drupal.org/node/3018782.
    $entity = $contexts['layout_builder.entity']->getContextValue();
    $configuration = $plugin->getConfiguration();
    if ($event->inPreview()) {
      $section_storage = $this->routeMatch->getParameter('section_storage');
    }
    else {
      $section_storage = $this->getSectionStorageForEntity($entity);
    }

    if (static::isTranslation($section_storage)) {
      if ($translated_plugin_configuration = $section_storage->getTranslatedComponentConfiguration($component->getUuid())) {
        $translated_plugin_configuration += $configuration;
        $plugin->setConfiguration($translated_plugin_configuration);
      }
    }
  }

1. In a situation with a broken block (missing view) the variable

$plugin

was instanceof Drupal\Core\Block\Plugin\Block\Broken which resulted in a PHP error.

2. $contexts['layout_builder.entity'] is NULL in layout_builder which leads to null pointer in

$entity = $contexts['layout_builder.entity']->getContextValue();

I have no deeper understanding of the code in this area so I hope my bug report is helpful for this module and / or the core patch.

Thank you very much again.

🐛 Bug report
Status

Needs work

Version

1.0

Component

Code

Created by

🇩🇪Germany Anybody Porta Westfalica

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

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.

Production build 0.71.5 2024