diff --git a/core/modules/layout_builder/config/schema/layout_builder.schema.yml b/core/modules/layout_builder/config/schema/layout_builder.schema.yml index 862c90ecbe..c04383b9f0 100644 --- a/core/modules/layout_builder/config/schema/layout_builder.schema.yml +++ b/core/modules/layout_builder/config/schema/layout_builder.schema.yml @@ -60,6 +60,9 @@ inline_block: view_mode: type: string label: 'View mode' + block_id: + type: integer + label: 'Block ID' block_revision_id: type: integer label: 'Block revision ID' diff --git a/core/modules/layout_builder/layout_builder.services.yml b/core/modules/layout_builder/layout_builder.services.yml index bce1734e71..2f5149dfec 100644 --- a/core/modules/layout_builder/layout_builder.services.yml +++ b/core/modules/layout_builder/layout_builder.services.yml @@ -56,3 +56,8 @@ services: arguments: ['@layout_builder.tempstore_repository', '@messenger'] tags: - { name: event_subscriber } + layout_builder.workspace_safe_forms: + class: Drupal\layout_builder\EventSubscriber\WorkspaceSafeForms + arguments: ['@?workspaces.manager'] + tags: + - { name: event_subscriber } diff --git a/core/modules/layout_builder/src/EventSubscriber/WorkspaceSafeForms.php b/core/modules/layout_builder/src/EventSubscriber/WorkspaceSafeForms.php new file mode 100644 index 0000000000..bc9c1f3289 --- /dev/null +++ b/core/modules/layout_builder/src/EventSubscriber/WorkspaceSafeForms.php @@ -0,0 +1,103 @@ +workspaceManager = $workspace_manager; + } + + /** + * {@inheritdoc} + */ + public static function getSubscribedEvents() { + $events = []; + if (class_exists(WorkspaceEvents::class)) { + $events[WorkspaceEvents::WORKSPACE_SAFE_FORM][] = 'onWorkspaceSafeForm'; + } + return $events; + } + + /** + * Marks the safeness of layout_builder forms when they are safe. + * + * @param \Drupal\workspaces\Event\WorkspaceSafeFormEvent $event + */ + public function onWorkspaceSafeForm(WorkspaceSafeFormEvent $event) { + if (!in_array($event->getFormId(), static::FORM_IDS, TRUE)) { + return; + } + + if ($section_storage = $this->getSectionStorage($event->getFormState())) { + $context_definitions = $section_storage->getContextDefinitions(); + if (!empty($context_definitions['entity'])) { + /** @var \Drupal\Core\Entity\EntityInterface $entity */ + $entity = $section_storage->getContext('entity')->getContextValue(); + if ($entity && $this->workspaceManager->isEntityTypeSupported($entity->getEntityType())) { + $event->setSafe(TRUE); + $event->stopPropagation(); + } + } + } + } + + /** + * Retrieves the section storage from a form state object, if it exists. + * + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The form state object. + * + * @return \Drupal\layout_builder\SectionStorageInterface|null + * The section storage or NULL if it doesn't exist. + */ + protected function getSectionStorage(FormStateInterface $form_state) { + foreach ($form_state->getBuildInfo()['args'] as $argument) { + if ($argument instanceof SectionStorageInterface) { + return $argument; + } + } + } + +} diff --git a/core/modules/layout_builder/src/InlineBlockEntityOperations.php b/core/modules/layout_builder/src/InlineBlockEntityOperations.php index 9760243bf3..319e413b37 100644 --- a/core/modules/layout_builder/src/InlineBlockEntityOperations.php +++ b/core/modules/layout_builder/src/InlineBlockEntityOperations.php @@ -6,7 +6,6 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\RevisionableInterface; -use Drupal\layout_builder\Plugin\Block\InlineBlock; use Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -174,24 +173,6 @@ public function handlePreSave(EntityInterface $entity) { $this->removeUnusedForEntityOnSave($entity); } - /** - * Gets a block ID for an inline block plugin. - * - * @param \Drupal\layout_builder\Plugin\Block\InlineBlock $block_plugin - * The inline block plugin. - * - * @return int - * The block content ID or null none available. - */ - protected function getPluginBlockId(InlineBlock $block_plugin) { - $configuration = $block_plugin->getConfiguration(); - if (!empty($configuration['block_revision_id'])) { - $revision_ids = $this->getBlockIdsForRevisionIds([$configuration['block_revision_id']]); - return array_pop($revision_ids); - } - return NULL; - } - /** * Delete the inline blocks and the usage records. * @@ -255,7 +236,7 @@ protected function saveInlineBlockComponent(EntityInterface $entity, SectionComp $plugin->saveBlockContent($new_revision, $duplicate_blocks); $post_save_configuration = $plugin->getConfiguration(); if ($duplicate_blocks || (empty($pre_save_configuration['block_revision_id']) && !empty($post_save_configuration['block_revision_id']))) { - $this->usage->addUsage($this->getPluginBlockId($plugin), $entity); + $this->usage->addUsage($post_save_configuration['block_id'], $entity); } $component->setConfiguration($post_save_configuration); } diff --git a/core/modules/layout_builder/src/Plugin/Block/InlineBlock.php b/core/modules/layout_builder/src/Plugin/Block/InlineBlock.php index 9f347174d3..6846ccfeeb 100644 --- a/core/modules/layout_builder/src/Plugin/Block/InlineBlock.php +++ b/core/modules/layout_builder/src/Plugin/Block/InlineBlock.php @@ -289,6 +289,7 @@ public function saveBlockContent($new_revision = FALSE, $duplicate_block = FALSE $block->setNewRevision(); } $block->save(); + $this->configuration['block_id'] = $block->id(); $this->configuration['block_revision_id'] = $block->getRevisionId(); $this->configuration['block_serialized'] = NULL; }