Security update to Drupal 8.4.6
[yaffs-website] / web / core / modules / block / tests / modules / block_test / src / Plugin / Block / TestAccessBlock.php
1 <?php
2
3 namespace Drupal\block_test\Plugin\Block;
4
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;
12
13 /**
14  * Provides a block to test access.
15  *
16  * @Block(
17  *   id = "test_access",
18  *   admin_label = @Translation("Test block access")
19  * )
20  */
21 class TestAccessBlock extends BlockBase implements ContainerFactoryPluginInterface {
22
23   /**
24    * Tests the test access block.
25    *
26    *
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
37    *   The state.
38    */
39   public function __construct(array $configuration, $plugin_id, $plugin_definition, StateInterface $state) {
40     parent::__construct($configuration, $plugin_id, $plugin_definition);
41
42     $this->state = $state;
43   }
44
45   /**
46    * {@inheritdoc}
47    */
48   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
49     return new static(
50       $configuration,
51       $plugin_id,
52       $plugin_definition,
53       $container->get('state')
54     );
55   }
56
57   /**
58    * {@inheritdoc}
59    */
60   protected function blockAccess(AccountInterface $account) {
61     return $this->state->get('test_block_access', FALSE) ? AccessResult::allowed()->setCacheMaxAge(0) : AccessResult::forbidden()->setCacheMaxAge(0);
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   public function build() {
68     return ['#markup' => 'Hello test world'];
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   public function getCacheMaxAge() {
75     return Cache::PERMANENT;
76   }
77
78 }