Chris@0: eventDispatcher = $dispatcher; Chris@17: } Chris@17: Chris@17: /** Chris@17: * {@inheritdoc} Chris@17: */ Chris@17: public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) { Chris@17: return new static( Chris@17: $entity_type, Chris@17: $container->get('event_dispatcher') Chris@17: ); Chris@17: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) { Chris@0: if ($operation === 'view') { Chris@17: $access = AccessResult::allowedIf($entity->isPublished()) Chris@14: ->orIf(AccessResult::allowedIfHasPermission($account, 'administer blocks')); Chris@0: } Chris@17: else { Chris@17: $access = parent::checkAccess($entity, $operation, $account); Chris@17: } Chris@17: // Add the entity as a cacheable dependency because access will at least be Chris@17: // determined by whether the block is reusable. Chris@17: $access->addCacheableDependency($entity); Chris@17: /** @var \Drupal\block_content\BlockContentInterface $entity */ Chris@17: if ($entity->isReusable() === FALSE) { Chris@17: if (!$entity instanceof DependentAccessInterface) { Chris@17: throw new \LogicException("Non-reusable block entities must implement \Drupal\block_content\Access\DependentAccessInterface for access control."); Chris@17: } Chris@17: $dependency = $entity->getAccessDependency(); Chris@17: if (empty($dependency)) { Chris@17: // If an access dependency has not been set let modules set one. Chris@17: $event = new BlockContentGetDependencyEvent($entity); Chris@17: $this->eventDispatcher->dispatch(BlockContentEvents::BLOCK_CONTENT_GET_DEPENDENCY, $event); Chris@17: $dependency = $event->getAccessDependency(); Chris@17: if (empty($dependency)) { Chris@17: return AccessResult::forbidden("Non-reusable blocks must set an access dependency for access control."); Chris@17: } Chris@17: } Chris@17: /** @var \Drupal\Core\Entity\EntityInterface $dependency */ Chris@17: $access = $access->andIf($dependency->access($operation, $account, TRUE)); Chris@17: } Chris@17: return $access; Chris@0: } Chris@0: Chris@0: }