Chris@17: entityTypeManager = $entity_type_manager; Chris@17: $this->database = $database; Chris@17: $this->usage = $usage; Chris@18: $this->sectionStorageManager = $section_storage_manager; Chris@17: } Chris@17: Chris@17: /** Chris@17: * {@inheritdoc} Chris@17: */ Chris@17: public static function getSubscribedEvents() { Chris@17: return [ Chris@17: BlockContentEvents::BLOCK_CONTENT_GET_DEPENDENCY => 'onGetDependency', Chris@17: ]; Chris@17: } Chris@17: Chris@17: /** Chris@17: * Handles the BlockContentEvents::INLINE_BLOCK_GET_DEPENDENCY event. Chris@17: * Chris@17: * @param \Drupal\block_content\Event\BlockContentGetDependencyEvent $event Chris@17: * The event. Chris@17: */ Chris@17: public function onGetDependency(BlockContentGetDependencyEvent $event) { Chris@17: if ($dependency = $this->getInlineBlockDependency($event->getBlockContentEntity())) { Chris@17: $event->setAccessDependency($dependency); Chris@17: } Chris@17: } Chris@17: Chris@17: /** Chris@17: * Get the access dependency of an inline block. Chris@17: * Chris@17: * If the block is used in an entity that entity will be returned as the Chris@17: * dependency. Chris@17: * Chris@17: * For revisionable entities the entity will only be returned if it is used in Chris@17: * the latest revision of the entity. For inline blocks that are not used in Chris@17: * the latest revision but are used in a previous revision the entity will not Chris@17: * be returned because calling Chris@17: * \Drupal\Core\Access\AccessibleInterface::access() will only check access on Chris@17: * the latest revision. Therefore if the previous revision of the entity was Chris@17: * returned as the dependency access would be granted to inline block Chris@17: * regardless of whether the user has access to the revision in which the Chris@17: * inline block was used. Chris@17: * Chris@17: * @param \Drupal\block_content\BlockContentInterface $block_content Chris@17: * The block content entity. Chris@17: * Chris@17: * @return \Drupal\Core\Entity\EntityInterface|null Chris@17: * Returns the layout dependency. Chris@17: * Chris@17: * @see \Drupal\block_content\BlockContentAccessControlHandler::checkAccess() Chris@17: * @see \Drupal\layout_builder\EventSubscriber\BlockComponentRenderArray::onBuildRender() Chris@17: */ Chris@17: protected function getInlineBlockDependency(BlockContentInterface $block_content) { Chris@17: $layout_entity_info = $this->usage->getUsage($block_content->id()); Chris@17: if (empty($layout_entity_info)) { Chris@17: // If the block does not have usage information then we cannot set a Chris@17: // dependency. It may be used by another module besides layout builder. Chris@17: return NULL; Chris@17: } Chris@17: $layout_entity_storage = $this->entityTypeManager->getStorage($layout_entity_info->layout_entity_type); Chris@17: $layout_entity = $layout_entity_storage->load($layout_entity_info->layout_entity_id); Chris@17: if ($this->isLayoutCompatibleEntity($layout_entity)) { Chris@17: if ($this->isBlockRevisionUsedInEntity($layout_entity, $block_content)) { Chris@17: return $layout_entity; Chris@17: } Chris@17: Chris@17: } Chris@17: return NULL; Chris@17: } Chris@17: Chris@17: /** Chris@17: * Determines if a block content revision is used in an entity. Chris@17: * Chris@17: * @param \Drupal\Core\Entity\EntityInterface $layout_entity Chris@17: * The layout entity. Chris@17: * @param \Drupal\block_content\BlockContentInterface $block_content Chris@17: * The block content revision. Chris@17: * Chris@17: * @return bool Chris@17: * TRUE if the block content revision is used as an inline block in the Chris@17: * layout entity. Chris@17: */ Chris@17: protected function isBlockRevisionUsedInEntity(EntityInterface $layout_entity, BlockContentInterface $block_content) { Chris@17: $sections_blocks_revision_ids = $this->getInlineBlockRevisionIdsInSections($this->getEntitySections($layout_entity)); Chris@17: return in_array($block_content->getRevisionId(), $sections_blocks_revision_ids); Chris@17: } Chris@17: Chris@17: }