diff --git a/core/lib/Drupal/Core/Condition/ConditionPluginBase.php b/core/lib/Drupal/Core/Condition/ConditionPluginBase.php index ed28d69..ece4e20 100644 --- a/core/lib/Drupal/Core/Condition/ConditionPluginBase.php +++ b/core/lib/Drupal/Core/Condition/ConditionPluginBase.php @@ -9,6 +9,7 @@ use Drupal\Core\Executable\ExecutablePluginBase; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Plugin\ContextAwarePluginAssignmentTrait; /** * Provides a basis for fulfilling contexts for condition plugins. @@ -21,6 +22,8 @@ */ abstract class ConditionPluginBase extends ExecutablePluginBase implements ConditionInterface { + use ContextAwarePluginAssignmentTrait; + /** * {@inheritdoc} */ @@ -41,6 +44,9 @@ public function isNegated() { * {@inheritdoc} */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + $temporary = $form_state->getTemporary(); + $contexts = isset($temporary['gathered_contexts']) ? $temporary['gathered_contexts'] : []; + $form['context_mapping'] = $this->addContextAssignmentElement($this, $contexts); $form['negate'] = array( '#type' => 'checkbox', '#title' => $this->t('Negate the condition'), diff --git a/core/lib/Drupal/Core/Plugin/Context/ContextHandler.php b/core/lib/Drupal/Core/Plugin/Context/ContextHandler.php index 820e9fe..212cec8 100644 --- a/core/lib/Drupal/Core/Plugin/Context/ContextHandler.php +++ b/core/lib/Drupal/Core/Plugin/Context/ContextHandler.php @@ -73,6 +73,7 @@ public function getMatchingContexts(array $contexts, ContextDefinitionInterface * {@inheritdoc} */ public function applyContextMapping(ContextAwarePluginInterface $plugin, $contexts, $mappings = array()) { + $mappings += $plugin->getContextMapping(); $plugin_contexts = $plugin->getContextDefinitions(); // Loop through each context and set it on the plugin if it matches one of // the contexts expected by the plugin. diff --git a/core/modules/block/src/BlockAccessControlHandler.php b/core/modules/block/src/BlockAccessControlHandler.php index b97fa39..b815c41 100644 --- a/core/modules/block/src/BlockAccessControlHandler.php +++ b/core/modules/block/src/BlockAccessControlHandler.php @@ -90,7 +90,7 @@ protected function checkAccess(EntityInterface $entity, $operation, $langcode, A foreach ($entity->getVisibilityConditions() as $condition_id => $condition) { if ($condition instanceof ContextAwarePluginInterface) { try { - $this->contextHandler->applyContextMapping($condition, $contexts, $condition->getContextMapping()); + $this->contextHandler->applyContextMapping($condition, $contexts); } catch (ContextException $e) { $access = AccessResult::forbidden(); diff --git a/core/modules/block/src/BlockForm.php b/core/modules/block/src/BlockForm.php index 079f605..33dbcca 100644 --- a/core/modules/block/src/BlockForm.php +++ b/core/modules/block/src/BlockForm.php @@ -16,7 +16,6 @@ use Drupal\Core\Form\FormState; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Language\LanguageManagerInterface; -use Drupal\Core\Plugin\ContextAwarePluginAssignmentTrait; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -25,8 +24,6 @@ */ class BlockForm extends EntityForm { - use ContextAwarePluginAssignmentTrait; - /** * The block entity. * @@ -206,9 +203,6 @@ protected function buildVisibilityInterface(array $form, FormStateInterface $for // @todo Allow list of conditions to be configured in // https://drupal.org/node/2284687. $visibility = $this->entity->getVisibility(); - $temporary = $form_state->getTemporary(); - $contexts = isset($temporary['gathered_contexts']) ? $temporary['gathered_contexts'] : []; - foreach ($this->manager->getDefinitions() as $condition_id => $defintion) { // Don't display the current theme condition. if ($condition_id == 'current_theme') { @@ -224,9 +218,6 @@ protected function buildVisibilityInterface(array $form, FormStateInterface $for $condition = $this->manager->createInstance($condition_id, isset($visibility[$condition_id]) ? $visibility[$condition_id] : []); $form_state->set(['conditions', $condition_id], $condition); $condition_form = $condition->buildConfigurationForm([], $form_state); - if ($condition instanceof ContextAwarePluginInterface) { - $condition_form['context_mapping'] = $this->addContextAssignmentElement($condition, $contexts); - } $condition_form['#type'] = 'details'; $condition_form['#title'] = $condition->getPluginDefinition()['label']; $condition_form['#group'] = 'visibility_tabs'; diff --git a/core/modules/block/src/BlockRepository.php b/core/modules/block/src/BlockRepository.php index 9a4071d..e3279b8 100644 --- a/core/modules/block/src/BlockRepository.php +++ b/core/modules/block/src/BlockRepository.php @@ -81,7 +81,7 @@ public function getVisibleBlocksPerRegion(array $contexts) { $block->setAvailableContexts($contexts); $block_plugin = $block->getPlugin(); if ($block_plugin instanceof ContextAwarePluginInterface) { - $this->contextHandler->applyContextMapping($block_plugin, $contexts, $block_plugin->getContextMapping()); + $this->contextHandler->applyContextMapping($block_plugin, $contexts); } if ($block->access('view')) { diff --git a/core/tests/Drupal/Tests/Core/Plugin/ContextHandlerTest.php b/core/tests/Drupal/Tests/Core/Plugin/ContextHandlerTest.php index 0122f49..b586c02 100644 --- a/core/tests/Drupal/Tests/Core/Plugin/ContextHandlerTest.php +++ b/core/tests/Drupal/Tests/Core/Plugin/ContextHandlerTest.php @@ -244,6 +244,9 @@ public function testApplyContextMapping() { $plugin = $this->getMock('Drupal\Component\Plugin\ContextAwarePluginInterface'); $plugin->expects($this->once()) + ->method('getContextMapping') + ->willReturn([]); + $plugin->expects($this->once()) ->method('getContextDefinitions') ->will($this->returnValue(array('hit' => 'hit'))); $plugin->expects($this->once()) @@ -267,6 +270,9 @@ public function testApplyContextMappingConfigurable() { $plugin = $this->getMock('Drupal\Tests\Core\Plugin\TestConfigurableContextAwarePluginInterface'); $plugin->expects($this->once()) + ->method('getContextMapping') + ->willReturn([]); + $plugin->expects($this->once()) ->method('getContextDefinitions') ->will($this->returnValue(array('hit' => 'hit'))); $plugin->expects($this->never()) @@ -290,6 +296,9 @@ public function testApplyContextMappingConfigurableAssigned() { $plugin = $this->getMock('Drupal\Tests\Core\Plugin\TestConfigurableContextAwarePluginInterface'); $plugin->expects($this->once()) + ->method('getContextMapping') + ->willReturn([]); + $plugin->expects($this->once()) ->method('getContextDefinitions') ->will($this->returnValue(array('hit' => 'hit'))); $plugin->expects($this->once()) @@ -316,6 +325,9 @@ public function testApplyContextMappingConfigurableAssignedMiss() { $plugin = $this->getMock('Drupal\Tests\Core\Plugin\TestConfigurableContextAwarePluginInterface'); $plugin->expects($this->once()) + ->method('getContextMapping') + ->willReturn([]); + $plugin->expects($this->once()) ->method('getContextDefinitions') ->will($this->returnValue(array('hit' => 'hit'))); $plugin->expects($this->never())