diff --git a/core/modules/layout_builder/src/EntityOperations.php b/core/modules/layout_builder/src/EntityOperations.php index 61f1d01870..b6466dfbdb 100644 --- a/core/modules/layout_builder/src/EntityOperations.php +++ b/core/modules/layout_builder/src/EntityOperations.php @@ -89,8 +89,13 @@ protected function removeUnusedForEntityOnSave(EntityInterface $entity) { return; } $original_sections = $this->getEntitySections($entity->original); - if ($removed_ids = array_diff($this->getInlineBlockIdsInSections($original_sections), $this->getInlineBlockIdsInSections($sections))) { - $this->deleteBlocksAndUsage($removed_ids); + $current_revision_ids = $this->getInBlockRevisionIdsInSection($sections); + // If there are any revisions in the original that aren't current there may + // some blocks that need to be removed. + if ($original_revision_ids = array_diff($this->getInBlockRevisionIdsInSection($original_sections), $current_revision_ids)) { + if ($removed_ids = array_diff($this->getBlockIdsForRevisionIds($original_revision_ids), $this->getBlockIdsForRevisionIds($current_revision_ids))) { + $this->deleteBlocksAndUsage($removed_ids); + } } } @@ -112,7 +117,7 @@ public function handleEntityDelete(EntityInterface $entity) { * @param \Drupal\Core\Entity\EntityInterface $entity * The entity. * - * @return array|\Drupal\layout_builder\Section[]|null + * @return \Drupal\layout_builder\Section[]|null * The entity layout sections if available. * * @internal @@ -143,7 +148,6 @@ public function handlePreSave(EntityInterface $entity) { return; } $duplicate_blocks = FALSE; - $this->removeUnusedForEntityOnSave($entity); if ($sections = $this->getEntitySections($entity)) { if ($entity instanceof FieldableEntityInterface && $entity->hasField('layout_builder__layout')) { @@ -183,27 +187,7 @@ public function handlePreSave(EntityInterface $entity) { $component->setConfiguration($plugin->getConfiguration()); } } - - } - - /** - * Gets the all Block Content IDs in Sections. - * - * @param \Drupal\layout_builder\Section[] $sections - * The layout sections. - * - * @return int[] - * The block ids. - */ - protected function getInlineBlockIdsInSections(array $sections) { - $block_ids = []; - $components = $this->getInlineBlockComponents($sections); - foreach ($components as $component) { - if ($id = $this->getPluginBlockId($component->getPlugin())) { - $block_ids[] = $id; - } - } - return $block_ids; + $this->removeUnusedForEntityOnSave($entity); } /** @@ -259,7 +243,7 @@ protected function getPluginBlockId(PluginInspectionInterface $plugin) { if (!empty($configuration['block_revision_id'])) { $query = $this->storage->getQuery(); $query->condition('revision_id', $configuration['block_revision_id']); - return array_pop($query->execute()); + return array_values($query->execute())[0]; } return NULL; } @@ -308,4 +292,44 @@ protected function isStorageAvailable() { return !empty($this->storage); } + /** + * Gets revision IDs for layout sections. + * + * @param \Drupal\layout_builder\Section[] $sections + * The layout sections. + * + * @return int[] + * The revision IDs. + */ + protected function getInBlockRevisionIdsInSection(array $sections) { + $revision_ids = []; + foreach ($this->getInlineBlockComponents($sections) as $component) { + $configuration = $component->getPlugin()->getConfiguration(); + if (!empty($configuration['block_revision_id'])) { + $revision_ids[] = $configuration['block_revision_id']; + } + } + return $revision_ids; + } + + /** + * Gets blocks IDs for an array of revision IDs. + * + * @param int[] $revision_ids + * The revision IDs. + * + * @return int[] + * The block IDs. + */ + protected function getBlockIdsForRevisionIds(array $revision_ids) { + if ($revision_ids) { + $query = $this->storage->getQuery(); + $query->condition('revision_id', $revision_ids, 'IN'); + $block_ids = $query->execute(); + return $block_ids; + } + return []; + + } + } diff --git a/core/modules/layout_builder/tests/src/FunctionalJavascript/InlineBlockContentBlockTest.php b/core/modules/layout_builder/tests/src/FunctionalJavascript/InlineBlockContentBlockTest.php index 8d6066ea13..04ee6e64ed 100644 --- a/core/modules/layout_builder/tests/src/FunctionalJavascript/InlineBlockContentBlockTest.php +++ b/core/modules/layout_builder/tests/src/FunctionalJavascript/InlineBlockContentBlockTest.php @@ -392,7 +392,6 @@ public function testDeletion() { // Remove block from default. $this->removeInlineBlockFromLayout(); $this->assertSaveLayout(); - $cron->run(); // Ensure the block in the default was deleted. $this->blockStorage->resetCache([$default_block_id]); $this->assertEmpty($this->blockStorage->load($default_block_id));