Issue #2342057 by dawehner, hussainweb, Wim Leers: Expand BlockPluginInterface to take into account $return_as_object
parent
8c30ce931a
commit
b8f63eddae
|
@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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('<front>');
|
||||
$this->assertNoText('Hello test world');
|
||||
|
||||
\Drupal::state()->set('test_block_access', TRUE);
|
||||
$this->drupalGet('<front>');
|
||||
$this->assertText('Hello test world');
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -0,0 +1,82 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\block_test\Plugin\Block\TestAccessBlock.
|
||||
*/
|
||||
|
||||
namespace Drupal\block_test\Plugin\Block;
|
||||
|
||||
use Drupal\Core\Block\BlockBase;
|
||||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
||||
use Drupal\Core\Session\AccountInterface;
|
||||
use Drupal\Core\State\StateInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Provides a block to test access.
|
||||
*
|
||||
* @Block(
|
||||
* id = "test_access",
|
||||
* admin_label = @Translation("Test block access")
|
||||
* )
|
||||
*/
|
||||
class TestAccessBlock extends BlockBase implements ContainerFactoryPluginInterface {
|
||||
|
||||
/**
|
||||
* Tests the test access block.
|
||||
*
|
||||
*
|
||||
* @param array $configuration
|
||||
* The plugin configuration, i.e. an array with configuration values keyed
|
||||
* by configuration option name. The special key 'context' may be used to
|
||||
* initialize the defined contexts by setting it to an array of context
|
||||
* values keyed by context names.
|
||||
* @param string $plugin_id
|
||||
* The plugin_id for the plugin instance.
|
||||
* @param mixed $plugin_definition
|
||||
* The plugin implementation definition.
|
||||
* @param \Drupal\Core\State\StateInterface $state
|
||||
* The state.
|
||||
*/
|
||||
public function __construct(array $configuration, $plugin_id, $plugin_definition, StateInterface $state) {
|
||||
parent::__construct($configuration, $plugin_id, $plugin_definition);
|
||||
|
||||
$this->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;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue