Problem/Motivation
When using Layout Library with Layout Builder in Drupal 11, clicking
“Discard changes” on a saved layout library item can cause a fatal error:
Call to a member function label() on null in DiscardLayoutChangesForm::getDescription().
The form expects an entity context, but SectionStorage::getContextValue('entity')
can return NULL without throwing a ContextException, and this case is not handled.
Steps to reproduce
- Enable Layout Builder and Layout Library.
- Create a content type with Layout Builder enabled (for example:
themag_landing_page). - Create and save a layout library entry for that content type
(for example:mg_home_page_6under
/admin/structure/types/manage/themag_landing_page/layout-library). - Edit the saved layout library item so that there are unsaved changes.
- Click the “Discard changes” link, which hits a URL like:
/admin/structure/types/manage/themag_landing_page/layout-library/mg_home_page_6/discard-changes.
Proposed resolution
Make DiscardLayoutChangesForm::getDescription() robust when the entity context is
missing or NULL. Instead of always calling $entity->label(), first check that
$entity is not NULL, and fall back to the generic message if there is no entity.
public function getDescription() { try { $entity = $this->sectionStorage->getContextValue('entity'); if ($entity && $entity->label()) { return $this->t('Any unsaved changes to the layout for %label will be discarded. This action cannot be undone.', [ '%label' => $entity->label(), ]); } } catch (\Drupal\Component\Plugin\Exception\ContextException $e) { // Fall through to the generic message below. } // If the entity context is not available or NULL, return a generic message. return $this->t('Any unsaved changes to the layout will be discarded. This action cannot be undone.'); }
Remaining tasks
- Add automated test coverage for the discard changes route when no entity context is available.
- Verify the fix on a real site using Layout Library and Layout Builder.
User interface changes
- No visual UI changes. Only prevents a fatal error and ensures the confirmation page always renders with either the entity label or the generic message.
Introduced terminology
- None.
API changes
- None. This only hardens an existing form method against
NULLcontext values.
Data model changes
- None.
Release notes snippet
Fixed a fatal error when discarding unsaved changes from the Layout Builder discard form in some Layout Library scenarios, by handling missing entity context in DiscardLayoutChangesForm::getDescription().
Issue fork drupal-3604077
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 3604077-layout-library-discard
changes, plain diff MR !16196
Comments
Comment #2
quietone commentedHi, Issues for Drupal core should be targeted to the 'main' branch, our primary development branch. Changes are made on the main branch first, and are then back ported as needed according to the Core change policies. The version the problem was discovered on should be stated in the issue summary Problem/Motivation section. Thanks.
Changing tags per Issue tags field and Issue tags -- special tags
Comment #5
danielvezaI was debating if this is an issue with Layout Library or Core, but the report is correct that
getContextValuecan return NULL. So we should handle that. I've added a fix & test.Comment #6
smustgrave commentedSince I can't run test-only jobs (super annoying) ran locally
DiscardLayoutChangesFormTest
RevertOverridesFormTest
Change itself looks fine to me, good defensive code.
Agree with putting the fix into core but maybe an investigation ticket into Layout Library? May be a bug over there too triggering this. Haven't looked though.
Comment #10
larowlanCommitted and pushed a57059c9dcd to main and 0b2b2c3b843 to 11.x. Thanks!
Didn't backport to 11.4.x because there are string changes