Skip to content
Snippets Groups Projects
Commit be80816a authored by catch's avatar catch
Browse files

Issue #2635238 by dsnopek, tim.plunkett: Contexts not mapped in time to use...

Issue #2635238 by dsnopek, tim.plunkett: Contexts not mapped in time to use them in BlockInterface::access()
parent 6a09a57c
Branches
Tags
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
......@@ -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();
......
......@@ -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.');
}
/**
......
......@@ -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);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment