Issue #2635238 by dsnopek, tim.plunkett: Contexts not mapped in time to use them in BlockInterface::access()
parent
6a09a57c74
commit
be80816a56
|
@ -127,7 +127,22 @@ class BlockAccessControlHandler extends EntityAccessControlHandler implements En
|
|||
}
|
||||
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 @@ class BlockUiTest extends WebTestBase {
|
|||
|
||||
$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 @@ class BlockUiTest extends WebTestBase {
|
|||
$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 @@ class TestContextAwareBlock extends BlockBase {
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function blockAccess(AccountInterface $account) {
|
||||
if ($this->getContextValue('user') instanceof UserInterface) {
|
||||
drupal_set_message('User context found.');
|
||||
}
|
||||
|
||||
return parent::blockAccess($account);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue