X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Flayout_builder%2Fsrc%2FEventSubscriber%2FSetInlineBlockDependency.php;fp=web%2Fcore%2Fmodules%2Flayout_builder%2Fsrc%2FEventSubscriber%2FSetInlineBlockDependency.php;h=edc05f83ddf8798f89415aa46db6199dc24a8ae2;hp=0000000000000000000000000000000000000000;hb=0bf8d09d2542548982e81a441b1f16e75873a04f;hpb=74df008bdbb3a11eeea356744f39b802369bda3c diff --git a/web/core/modules/layout_builder/src/EventSubscriber/SetInlineBlockDependency.php b/web/core/modules/layout_builder/src/EventSubscriber/SetInlineBlockDependency.php new file mode 100644 index 000000000..edc05f83d --- /dev/null +++ b/web/core/modules/layout_builder/src/EventSubscriber/SetInlineBlockDependency.php @@ -0,0 +1,156 @@ +entityTypeManager = $entity_type_manager; + $this->database = $database; + $this->usage = $usage; + } + + /** + * {@inheritdoc} + */ + public static function getSubscribedEvents() { + return [ + BlockContentEvents::BLOCK_CONTENT_GET_DEPENDENCY => 'onGetDependency', + ]; + } + + /** + * Handles the BlockContentEvents::INLINE_BLOCK_GET_DEPENDENCY event. + * + * @param \Drupal\block_content\Event\BlockContentGetDependencyEvent $event + * The event. + */ + public function onGetDependency(BlockContentGetDependencyEvent $event) { + if ($dependency = $this->getInlineBlockDependency($event->getBlockContentEntity())) { + $event->setAccessDependency($dependency); + } + } + + /** + * Get the access dependency of an inline block. + * + * If the block is used in an entity that entity will be returned as the + * dependency. + * + * For revisionable entities the entity will only be returned if it is used in + * the latest revision of the entity. For inline blocks that are not used in + * the latest revision but are used in a previous revision the entity will not + * be returned because calling + * \Drupal\Core\Access\AccessibleInterface::access() will only check access on + * the latest revision. Therefore if the previous revision of the entity was + * returned as the dependency access would be granted to inline block + * regardless of whether the user has access to the revision in which the + * inline block was used. + * + * @param \Drupal\block_content\BlockContentInterface $block_content + * The block content entity. + * + * @return \Drupal\Core\Entity\EntityInterface|null + * Returns the layout dependency. + * + * @see \Drupal\block_content\BlockContentAccessControlHandler::checkAccess() + * @see \Drupal\layout_builder\EventSubscriber\BlockComponentRenderArray::onBuildRender() + */ + protected function getInlineBlockDependency(BlockContentInterface $block_content) { + $layout_entity_info = $this->usage->getUsage($block_content->id()); + if (empty($layout_entity_info)) { + // If the block does not have usage information then we cannot set a + // dependency. It may be used by another module besides layout builder. + return NULL; + } + /** @var \Drupal\layout_builder\InlineBlockUsage $usage */ + $layout_entity_storage = $this->entityTypeManager->getStorage($layout_entity_info->layout_entity_type); + $layout_entity = $layout_entity_storage->load($layout_entity_info->layout_entity_id); + if ($this->isLayoutCompatibleEntity($layout_entity)) { + if ($this->isBlockRevisionUsedInEntity($layout_entity, $block_content)) { + return $layout_entity; + } + + } + return NULL; + } + + /** + * Determines if a block content revision is used in an entity. + * + * @param \Drupal\Core\Entity\EntityInterface $layout_entity + * The layout entity. + * @param \Drupal\block_content\BlockContentInterface $block_content + * The block content revision. + * + * @return bool + * TRUE if the block content revision is used as an inline block in the + * layout entity. + */ + protected function isBlockRevisionUsedInEntity(EntityInterface $layout_entity, BlockContentInterface $block_content) { + $sections_blocks_revision_ids = $this->getInlineBlockRevisionIdsInSections($this->getEntitySections($layout_entity)); + return in_array($block_content->getRevisionId(), $sections_blocks_revision_ids); + } + +}