diff --git a/core/lib/Drupal/Core/Condition/ConditionAccessResolverTrait.php b/core/lib/Drupal/Core/Condition/ConditionAccessResolverTrait.php index 9f16d09..65990da 100644 --- a/core/lib/Drupal/Core/Condition/ConditionAccessResolverTrait.php +++ b/core/lib/Drupal/Core/Condition/ConditionAccessResolverTrait.php @@ -35,17 +35,17 @@ protected function resolveConditions($conditions, $condition_logic) { $pass = FALSE; } - // If a condition fails and all conditions were required, deny access. + // If a condition fails and all conditions were needed, deny access. if (!$pass && $condition_logic == 'and') { return FALSE; } - // If a condition passes and one condition was required, grant access. + // If a condition passes and only one condition was needed, grant access. elseif ($pass && $condition_logic == 'or') { return TRUE; } } - // If no conditions passed and one condition was required, deny access. + // If no conditions passed and one condition was needed, deny access. return $condition_logic == 'and'; } diff --git a/core/lib/Drupal/Core/Condition/ConditionPluginBag.php b/core/lib/Drupal/Core/Condition/ConditionPluginBag.php index acb0fce..394bebf 100644 --- a/core/lib/Drupal/Core/Condition/ConditionPluginBag.php +++ b/core/lib/Drupal/Core/Condition/ConditionPluginBag.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Condition; +use Drupal\Component\Plugin\Context\ContextInterface; use Drupal\Core\Plugin\DefaultPluginBag; /** @@ -15,6 +16,13 @@ class ConditionPluginBag extends DefaultPluginBag { /** + * An array of collected contexts for conditions. + * + * @var \Drupal\Component\Plugin\Context\ContextInterface[] + */ + protected $conditionContexts = array(); + + /** * {@inheritdoc} * * @return \Drupal\Core\Condition\ConditionInterface @@ -40,4 +48,29 @@ public function getConfiguration() { return $configuration; } + /** + * Sets the condition context for a given name. + * + * @param string $name + * The name of the context. + * @param \Drupal\Component\Plugin\Context\ContextInterface $context + * The context to add. + * + * @return $this + */ + public function addContext($name, ContextInterface $context) { + $this->conditionContexts[$name] = $context; + return $this; + } + + /** + * Gets the values for all defined contexts. + * + * @return \Drupal\Component\Plugin\Context\ContextInterface[] + * An array of set contexts, keyed by context name. + */ + public function getConditionContexts() { + return $this->conditionContexts; + } + } diff --git a/core/modules/block/src/BlockBase.php b/core/modules/block/src/BlockBase.php index 33ddd39..d1eb821 100644 --- a/core/modules/block/src/BlockBase.php +++ b/core/modules/block/src/BlockBase.php @@ -7,10 +7,8 @@ namespace Drupal\block; -use Drupal\block\Event\BlockContextEvent; +use Drupal\block\Event\BlockConditionContextEvent; use Drupal\block\Event\BlockEvents; -use Drupal\Component\Plugin\ConfigurablePluginInterface; -use Drupal\Component\Plugin\Context\ContextInterface; use Drupal\Component\Plugin\ContextAwarePluginInterface; use Drupal\Core\Condition\ConditionAccessResolverTrait; use Drupal\Core\Condition\ConditionPluginBag; @@ -51,13 +49,6 @@ protected $conditionPluginManager; /** - * An array of collected contexts for conditions. - * - * @var \Drupal\Component\Plugin\Context\ContextInterface[] - */ - protected $conditionContexts = array(); - - /** * {@inheritdoc} */ public function label() { @@ -164,7 +155,7 @@ public function access(AccountInterface $account) { $this->contextHandler()->applyContextMapping($condition, $contexts, $mappings[$condition_id]); } } - if ($this->resolveConditions($conditions, 'and', $this->getConditionContexts(), $mappings) === FALSE) { + if ($this->resolveConditions($conditions, 'and', $contexts, $mappings) === FALSE) { return FALSE; } return $this->blockAccess($account); @@ -177,18 +168,9 @@ public function access(AccountInterface $account) { * An array of set contexts, keyed by context name. */ protected function getConditionContexts() { - if (!$this->conditionContexts) { - $this->eventDispatcher()->dispatch(BlockEvents::BLOCK_CONTEXT, new BlockContextEvent($this)); - } - return $this->conditionContexts; - } - - /** - * {@inheritdoc} - */ - public function addContext($name, ContextInterface $context) { - $this->conditionContexts[$name] = $context; - return $this; + $conditions = $this->getVisibilityConditions(); + $this->eventDispatcher()->dispatch(BlockEvents::CONDITION_CONTEXT, new BlockConditionContextEvent($conditions)); + return $conditions->getConditionContexts(); } /** diff --git a/core/modules/block/src/BlockPluginInterface.php b/core/modules/block/src/BlockPluginInterface.php index 37ea357..64fd12f 100644 --- a/core/modules/block/src/BlockPluginInterface.php +++ b/core/modules/block/src/BlockPluginInterface.php @@ -172,16 +172,4 @@ public function getVisibilityCondition($instance_id); */ public function setVisibilityConfig($instance_id, array $configuration); - /** - * Sets the condition context for a given name. - * - * @param string $name - * The name of the context. - * @param \Drupal\Component\Plugin\Context\ContextInterface $context - * The context to add. - * - * @return $this - */ - public function addContext($name, ContextInterface $context); - } diff --git a/core/modules/block/src/Event/BlockConditionContextEvent.php b/core/modules/block/src/Event/BlockConditionContextEvent.php new file mode 100644 index 0000000..a290ae6 --- /dev/null +++ b/core/modules/block/src/Event/BlockConditionContextEvent.php @@ -0,0 +1,39 @@ +conditions = $conditions; + } + + /** + * @return \Drupal\block\BlockPluginInterface + */ + public function getConditions() { + return $this->conditions; + } + +} diff --git a/core/modules/block/src/Event/BlockContextEvent.php b/core/modules/block/src/Event/BlockContextEvent.php deleted file mode 100644 index 70a5f4f..0000000 --- a/core/modules/block/src/Event/BlockContextEvent.php +++ /dev/null @@ -1,39 +0,0 @@ -block = $block; - } - - /** - * @return \Drupal\block\BlockPluginInterface - */ - public function getBlock() { - return $this->block; - } - -} diff --git a/core/modules/block/src/Event/BlockEvents.php b/core/modules/block/src/Event/BlockEvents.php index 88dd588..070eb6c 100644 --- a/core/modules/block/src/Event/BlockEvents.php +++ b/core/modules/block/src/Event/BlockEvents.php @@ -13,11 +13,11 @@ final class BlockEvents { /** - * Name of the event when gathering context for a block plugin. + * Name of the event when gathering condition context for a block plugin. * * @see \Drupal\block\BlockBase::getConditionContexts() - * @see \Drupal\block\Event\BlockContextEvent + * @see \Drupal\block\Event\BlockConditionContextEvent */ - const BLOCK_CONTEXT = 'block.block_context'; + const CONDITION_CONTEXT = 'block.condition_context'; } diff --git a/core/modules/block/src/EventSubscriber/BlockContextSubscriberBase.php b/core/modules/block/src/EventSubscriber/BlockContextSubscriberBase.php index ef69f7d..683e83e 100644 --- a/core/modules/block/src/EventSubscriber/BlockContextSubscriberBase.php +++ b/core/modules/block/src/EventSubscriber/BlockContextSubscriberBase.php @@ -7,7 +7,7 @@ namespace Drupal\block\EventSubscriber; -use Drupal\block\Event\BlockContextEvent; +use Drupal\block\Event\BlockConditionContextEvent; use Drupal\block\Event\BlockEvents; use Drupal\Component\Plugin\Context\ContextInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; @@ -18,23 +18,23 @@ abstract class BlockContextSubscriberBase implements EventSubscriberInterface { /** - * @var \Drupal\block\BlockPluginInterface + * @var \Drupal\Core\Condition\ConditionPluginBag */ - protected $block; + protected $conditions; /** * {@inheritdoc} */ public static function getSubscribedEvents() { - $events[BlockEvents::BLOCK_CONTEXT][] = 'onBlockContext'; + $events[BlockEvents::CONDITION_CONTEXT][] = 'onBlockConditionContext'; return $events; } /** * Subscribes to the event and delegates to the subclass. */ - public function onBlockContext(BlockContextEvent $event) { - $this->block = $event->getBlock(); + public function onBlockConditionContext(BlockConditionContextEvent $event) { + $this->conditions = $event->getConditions(); $this->determineBlockContext(); } @@ -54,7 +54,7 @@ public function onBlockContext(BlockContextEvent $event) { * @return $this */ public function addContext($name, ContextInterface $context) { - $this->block->addContext($name, $context); + $this->conditions->addContext($name, $context); return $this; }