get('plugin.manager.condition'), $container->get('context.handler') ); } /** * Constructs the block access control handler instance * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type * The entity type definition. * @param \Drupal\Core\Executable\ExecutableManagerInterface $manager * The ConditionManager for checking visibility of blocks. * @param \Drupal\Core\Plugin\Context\ContextHandlerInterface $context_handler * The ContextHandler for applying contexts to conditions properly. */ public function __construct(EntityTypeInterface $entity_type, ExecutableManagerInterface $manager, ContextHandlerInterface $context_handler) { parent::__construct($entity_type); $this->manager = $manager; $this->contextHandler = $context_handler; } /** * {@inheritdoc} */ protected function checkAccess(EntityInterface $entity, $operation, $langcode, AccountInterface $account) { /** @var \Drupal\block\BlockInterface $entity */ if ($operation != 'view') { return parent::checkAccess($entity, $operation, $langcode, $account); } // Don't grant access to disabled blocks. if (!$entity->status()) { return AccessResult::forbidden()->cacheUntilEntityChanges($entity); } else { $contexts = $entity->getContexts(); $conditions = []; foreach ($entity->getVisibilityConditions() as $condition_id => $condition) { if ($condition instanceof ContextAwarePluginInterface) { try { $this->contextHandler->applyContextMapping($condition, $contexts); } catch (ContextException $e) { return AccessResult::forbidden()->setCacheMaxAge(0); } } $conditions[$condition_id] = $condition; } if ($this->resolveConditions($conditions, 'and') !== FALSE) { // Delegate to the plugin. $access = $entity->getPlugin()->access($account, TRUE); } else { $access = AccessResult::forbidden(); } // This should not be hardcoded to an uncacheable access check result, but // in order to fix that, we need condition plugins to return cache contexts, // otherwise it will be impossible to determine by which cache contexts the // result should be varied. // @todo Change this to use $access->cacheUntilEntityChanges($entity) once // https://www.drupal.org/node/2375695 is resolved. return $access->setCacheMaxAge(0); } } }