3 namespace Drupal\block_test\Plugin\Block;
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Block\BlockBase;
7 use Drupal\Core\Cache\Cache;
8 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
9 use Drupal\Core\Session\AccountInterface;
10 use Drupal\Core\State\StateInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * Provides a block to test access.
18 * admin_label = @Translation("Test block access")
21 class TestAccessBlock extends BlockBase implements ContainerFactoryPluginInterface {
24 * Tests the test access block.
27 * @param array $configuration
28 * The plugin configuration, i.e. an array with configuration values keyed
29 * by configuration option name. The special key 'context' may be used to
30 * initialize the defined contexts by setting it to an array of context
31 * values keyed by context names.
32 * @param string $plugin_id
33 * The plugin_id for the plugin instance.
34 * @param mixed $plugin_definition
35 * The plugin implementation definition.
36 * @param \Drupal\Core\State\StateInterface $state
39 public function __construct(array $configuration, $plugin_id, $plugin_definition, StateInterface $state) {
40 parent::__construct($configuration, $plugin_id, $plugin_definition);
42 $this->state = $state;
48 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
53 $container->get('state')
60 protected function blockAccess(AccountInterface $account) {
61 return $this->state->get('test_block_access', FALSE) ? AccessResult::allowed()->setCacheMaxAge(0) : AccessResult::forbidden()->setCacheMaxAge(0);
67 public function build() {
68 return ['#markup' => 'Hello test world'];
74 public function getCacheMaxAge() {
75 return Cache::PERMANENT;