diff --git a/core/lib/Drupal/Core/Block/BlockBase.php b/core/lib/Drupal/Core/Block/BlockBase.php index ba1026b5564..68745432a3d 100644 --- a/core/lib/Drupal/Core/Block/BlockBase.php +++ b/core/lib/Drupal/Core/Block/BlockBase.php @@ -119,7 +119,7 @@ abstract class BlockBase extends ContextAwarePluginBase implements BlockPluginIn /** * {@inheritdoc} */ - public function access(AccountInterface $account) { + public function access(AccountInterface $account, $return_as_object = FALSE) { // @todo Remove self::blockAccess() and force individual plugins to return // their own AccessResult logic. Until that is done in // https://www.drupal.org/node/2375689 the access will be set uncacheable. @@ -129,7 +129,9 @@ abstract class BlockBase extends ContextAwarePluginBase implements BlockPluginIn else { $access = AccessResult::forbidden(); } - return $access->setCacheable(FALSE); + + $access->setCacheable(FALSE); + return $return_as_object ? $access : $access->isAllowed(); } /** diff --git a/core/lib/Drupal/Core/Block/BlockPluginInterface.php b/core/lib/Drupal/Core/Block/BlockPluginInterface.php index a42027e10c5..10ec23ef2c3 100644 --- a/core/lib/Drupal/Core/Block/BlockPluginInterface.php +++ b/core/lib/Drupal/Core/Block/BlockPluginInterface.php @@ -46,13 +46,19 @@ interface BlockPluginInterface extends ConfigurablePluginInterface, PluginFormIn * * @param \Drupal\Core\Session\AccountInterface $account * The user session for which to check access. + * @param bool $return_as_object + * (optional) Defaults to FALSE. * - * @return \Drupal\Core\Access\AccessResultInterface - * An access result object instantiated and configured by the block plugin. + * @return bool|\Drupal\Core\Access\AccessResultInterface + * The access result. Returns a boolean if $return_as_object is FALSE (this + * is the default) and otherwise an AccessResultInterface object. + * When a boolean is returned, the result of AccessInterface::isAllowed() is + * returned, i.e. TRUE means access is explicitly allowed, FALSE means + * access is either explicitly forbidden or "no opinion". * * @see \Drupal\block\BlockAccessControlHandler */ - public function access(AccountInterface $account); + public function access(AccountInterface $account, $return_as_object = FALSE); /** * Builds and returns the renderable array for this block plugin. diff --git a/core/modules/block/src/BlockAccessControlHandler.php b/core/modules/block/src/BlockAccessControlHandler.php index 6160f84bb01..6ec53f67eab 100644 --- a/core/modules/block/src/BlockAccessControlHandler.php +++ b/core/modules/block/src/BlockAccessControlHandler.php @@ -100,7 +100,7 @@ class BlockAccessControlHandler extends EntityAccessControlHandler implements En } if ($this->resolveConditions($conditions, 'and') !== FALSE) { // Delegate to the plugin. - $access = $entity->getPlugin()->access($account); + $access = $entity->getPlugin()->access($account, TRUE); } else { $access = AccessResult::forbidden(); diff --git a/core/modules/block/src/Tests/BlockTest.php b/core/modules/block/src/Tests/BlockTest.php index 53d1e5736aa..5c8d900dfd4 100644 --- a/core/modules/block/src/Tests/BlockTest.php +++ b/core/modules/block/src/Tests/BlockTest.php @@ -436,4 +436,18 @@ class BlockTest extends BlockTestBase { $this->assertIdentical(NULL, Block::load($block->id())); } + /** + * Tests the block access. + */ + public function testBlockAccess() { + $this->drupalPlaceBlock('test_access', ['region' => 'help']); + + $this->drupalGet(''); + $this->assertNoText('Hello test world'); + + \Drupal::state()->set('test_block_access', TRUE); + $this->drupalGet(''); + $this->assertText('Hello test world'); + } + } diff --git a/core/modules/block/src/Tests/BlockTestBase.php b/core/modules/block/src/Tests/BlockTestBase.php index 0aabffaa534..1f3cb74c2e5 100644 --- a/core/modules/block/src/Tests/BlockTestBase.php +++ b/core/modules/block/src/Tests/BlockTestBase.php @@ -19,7 +19,7 @@ abstract class BlockTestBase extends WebTestBase { * * @var array */ - public static $modules = array('block', 'filter', 'test_page_test', 'help'); + public static $modules = array('block', 'filter', 'test_page_test', 'help', 'block_test'); /** * A list of theme regions to test. diff --git a/core/modules/block/tests/modules/block_test/src/Plugin/Block/TestAccessBlock.php b/core/modules/block/tests/modules/block_test/src/Plugin/Block/TestAccessBlock.php new file mode 100644 index 00000000000..b3ed511e7b0 --- /dev/null +++ b/core/modules/block/tests/modules/block_test/src/Plugin/Block/TestAccessBlock.php @@ -0,0 +1,82 @@ +state = $state; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('state') + ); + } + + /** + * {@inheritdoc} + */ + protected function blockAccess(AccountInterface $account) { + return $this->state->get('test_block_access', FALSE); + } + + /** + * {@inheritdoc} + */ + public function build() { + return ['#markup' => 'Hello test world']; + } + + /** + * {@inheritdoc} + */ + public function isCacheable() { + return TRUE; + } + +} +