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

  1. Enable Layout Builder and Layout Library.
  2. Create a content type with Layout Builder enabled (for example: themag_landing_page).
  3. Create and save a layout library entry for that content type
    (for example: mg_home_page_6 under
    /admin/structure/types/manage/themag_landing_page/layout-library).
  4. Edit the saved layout library item so that there are unsaved changes.
  5. 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 NULL context 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().

Comments

georg3 created an issue. See original summary.

quietone’s picture

Version: 11.3.x-dev » main
Issue tags: -layout_builder, -layout_library, -bug +Needs title update

Hi, 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