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