Summary:
The below error occurs after a user removes all sections from a layout and saves while the Quickedit module is enabled.
The website encountered an unexpected error. Please try again later.
Error: Call to a member function getRegionNames() on null in Drupal\layout_builder\QuickEditIntegration->entityViewAlter() (line 134 of core/modules/layout_builder/src/QuickEditIntegration.php).
Additionally, this error will appear in the log:
Notice: Undefined index: #layout in Drupal\layout_builder\QuickEditIntegration->entityViewAlter() (line 133 of core/modules/layout_builder/src/QuickEditIntegration.php).
Steps to reproduce:
- Install Drupal 8.7.3
- Enable Layout Builder
- Under Manage Display of any content type, enable both "Use Layout Builder" and "Allow each content item to have its layout customized."
- Create a Layout enabled node
- Click the "Layout" tab of this new node and remove all sections
- Click "Save layout"
Proposed fix:
In QuickEditIntegration.php wrap lines 132 to 144 in an if statement so that it looks like this:
if (!empty($section)) {
/** @var \Drupal\Core\Layout\LayoutDefinition $layout */
$layout = $section['#layout'];
$regions = $layout->getRegionNames();
foreach ($regions as $region) {
if (isset($section[$region])) {
foreach ($section[$region] as $uuid => $component) {
if (isset($component['#plugin_id']) && $this->supportQuickEditOnComponent($component, $entity)) {
$plugin_ids_to_update[$component['#plugin_id']][$delta][$region][$uuid] = $uuid;
}
}
}
}
}
Comments
Comment #2
jeremyr commentedComment #3
tim.plunkettNice find. We have test coverage for removing all sections, but quickedit wasn't installed.
Also, the render system provides a bespoke isEmpty method that handles #cache info, switching to that.
Comment #5
omkar06 commentedComment #6
omkar06 commentedTested Patch provided in #3. After applying a patch, I am able to view the page even I remove all sections from the layout of the page. Before applying a patch, I was able to reproduce an issue.
Comment #7
omkar06 commentedComment #8
catchCommitted a2d8c0a and pushed to 8.8.x. Thanks!
Comment #11
paulkerrigan commentedNotice: Undefined index: #layout in Drupal\layout_builder\QuickEditIntegration->entityViewAlter() (line 133 of core/modules/layout_builder/src/QuickEditIntegration.php).The notice above can be avoided by adding a further check inside the if-statement, to make sure
$section['#layout']is not empty.As the code relies on the availability of the LayoutDefinition object, it appears that there is no need to run the loop if it's unset. Please correct me if I'm wrong. Thanks.