diff --git a/core/modules/block/src/BlockAccessControlHandler.php b/core/modules/block/src/BlockAccessControlHandler.php index 92b54a4ae57bb8e9497625e8225ff82d43efc968..4529647d6d7faa095d4309ceb46ef2865fa97386 100644 --- a/core/modules/block/src/BlockAccessControlHandler.php +++ b/core/modules/block/src/BlockAccessControlHandler.php @@ -127,7 +127,22 @@ protected function checkAccess(EntityInterface $entity, $operation, AccountInter } elseif ($this->resolveConditions($conditions, 'and') !== FALSE) { // Delegate to the plugin. - $access = $entity->getPlugin()->access($account, TRUE); + $block_plugin = $entity->getPlugin(); + try { + if ($block_plugin instanceof ContextAwarePluginInterface) { + $contexts = $this->contextRepository->getRuntimeContexts(array_values($block_plugin->getContextMapping())); + $this->contextHandler->applyContextMapping($block_plugin, $contexts); + } + $access = $block_plugin->access($account, TRUE); + } + catch (ContextException $e) { + // Setting access to forbidden if any context is missing for the same + // reasons as with conditions (described in the comment above). + // @todo Avoid setting max-age 0 for some or all cases, for example by + // treating available contexts without value differently in + // https://www.drupal.org/node/2521956. + $access = AccessResult::forbidden()->setCacheMaxAge(0); + } } else { $access = AccessResult::forbidden(); diff --git a/core/modules/block/src/Tests/BlockUiTest.php b/core/modules/block/src/Tests/BlockUiTest.php index 4d0f03a1e1bb4b63d4788b72aec703759ca429fc..6b5566790780f22db0bdf0aa70cb20c48b3b20e5 100644 --- a/core/modules/block/src/Tests/BlockUiTest.php +++ b/core/modules/block/src/Tests/BlockUiTest.php @@ -241,6 +241,7 @@ public function testContextAwareBlocks() { $this->drupalGet(''); $this->assertText('Test context-aware block'); + $this->assertText('User context found.'); $this->assertRaw($expected_text); // Test context mapping allows empty selection for optional contexts. @@ -251,6 +252,7 @@ public function testContextAwareBlocks() { $this->drupalPostForm(NULL, $edit, 'Save block'); $this->drupalGet(''); $this->assertText('No context mapping selected.'); + $this->assertNoText('User context found.'); } /** diff --git a/core/modules/block/tests/modules/block_test/src/Plugin/Block/TestContextAwareBlock.php b/core/modules/block/tests/modules/block_test/src/Plugin/Block/TestContextAwareBlock.php index 013dbae75b8a7e3f1861ed084b8353c5c8d5414e..dcb79bdb443c2f5f52e61a94ccedaeb01f898bc1 100644 --- a/core/modules/block/tests/modules/block_test/src/Plugin/Block/TestContextAwareBlock.php +++ b/core/modules/block/tests/modules/block_test/src/Plugin/Block/TestContextAwareBlock.php @@ -8,6 +8,8 @@ namespace Drupal\block_test\Plugin\Block; use Drupal\Core\Block\BlockBase; +use Drupal\Core\Session\AccountInterface; +use Drupal\user\UserInterface; /** * Provides a context-aware block. @@ -35,4 +37,15 @@ public function build() { ); } + /** + * {@inheritdoc} + */ + protected function blockAccess(AccountInterface $account) { + if ($this->getContextValue('user') instanceof UserInterface) { + drupal_set_message('User context found.'); + } + + return parent::blockAccess($account); + } + }