diff --git a/core/modules/layout_builder/src/EventSubscriber/PrepareLayout.php b/core/modules/layout_builder/src/EventSubscriber/PrepareLayout.php index 4d32995282..7ea4440414 100644 --- a/core/modules/layout_builder/src/EventSubscriber/PrepareLayout.php +++ b/core/modules/layout_builder/src/EventSubscriber/PrepareLayout.php @@ -59,6 +59,10 @@ public static function getSubscribedEvents(): array { public function onPrepareLayout(PrepareLayoutEvent $event) { $section_storage = $event->getSectionStorage(); + if ($this->layoutTempstoreRepository->hasUnsavedChanges($section_storage)) { + \Drupal::messenger()->addWarning($this->t('You have unsaved changes.')); + } + if (!$this->layoutTempstoreRepository->has($section_storage)) { // If the layout is an override that has not yet been overridden, copy the // sections from the corresponding default. diff --git a/core/modules/layout_builder/src/Form/LayoutBuilderEntityFormTrait.php b/core/modules/layout_builder/src/Form/LayoutBuilderEntityFormTrait.php index a20f4a2237..e48602c608 100644 --- a/core/modules/layout_builder/src/Form/LayoutBuilderEntityFormTrait.php +++ b/core/modules/layout_builder/src/Form/LayoutBuilderEntityFormTrait.php @@ -4,8 +4,8 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\StringTranslation\TranslatableMarkup; -use Drupal\layout_builder\SectionStorageInterface; use Drupal\Core\TempStore\Lock; +use Drupal\layout_builder\SectionStorageInterface; /** * Provides a trait for common methods used in Layout Builder entity forms. @@ -52,14 +52,6 @@ protected function buildMessageContainer(TranslatableMarkup $message, string $ty ]; } - /** - * Gets the messenger service. - * - * @return \Drupal\Core\Messenger\MessengerInterface - * The messenger service. - */ - abstract protected function messenger(); - /** * Checks if a lock is present and provides a situation specific message. * @@ -71,12 +63,9 @@ abstract protected function messenger(); * return a render array that will replace the Layout Builder UI form. * Otherwise, return null. */ - protected function getLockMessage(SectionStorageInterface $section_storage): array|NULL { - if ($lock = $this->layoutTempstoreRepository->getLock($section_storage)) { - if ($this->currentUser()->id() === $lock->getOwnerId()) { - $this->messenger()->addWarning($this->t('You have unsaved changes.')); - return NULL; - } + protected function getLockMessage(SectionStorageInterface $section_storage): ?array { + if (($lock = $this->layoutTempstoreRepository->getLock($section_storage)) + && $this->currentUser()->id() != $lock->getOwnerId()) { return $this->lockMessage($section_storage, $lock); } return NULL; diff --git a/core/modules/layout_builder/src/LayoutTempstoreRepository.php b/core/modules/layout_builder/src/LayoutTempstoreRepository.php index 7036f898d2..0567ea83c8 100644 --- a/core/modules/layout_builder/src/LayoutTempstoreRepository.php +++ b/core/modules/layout_builder/src/LayoutTempstoreRepository.php @@ -63,7 +63,11 @@ public function get(SectionStorageInterface $section_storage) { /** * {@inheritdoc} */ - public function getLock(SectionStorageInterface $section_storage): Lock|NULL { + public function getLock(SectionStorageInterface $section_storage): ?Lock { + if (!$this->hasUnsavedChanges($section_storage)) { + return NULL; + } + $key = $this->getKey($section_storage); return $this->getTempstore($section_storage)->getMetadata($key); }