TestAccessBlock.php

Same filename and directory in other branches
  1. 9 core/modules/block/tests/modules/block_test/src/Plugin/Block/TestAccessBlock.php
  2. 8.9.x core/modules/block/tests/modules/block_test/src/Plugin/Block/TestAccessBlock.php
  3. 10 core/modules/block/tests/modules/block_test/src/Plugin/Block/TestAccessBlock.php

Namespace

Drupal\block_test\Plugin\Block

File

core/modules/block/tests/modules/block_test/src/Plugin/Block/TestAccessBlock.php

View source
<?php

declare (strict_types=1);
namespace Drupal\block_test\Plugin\Block;

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\Attribute\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\KeyValueStore\KeyValueStoreInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a block to test 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\KeyValueStore\KeyValueStoreInterface $keyValue
   *   The key value store.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, protected KeyValueStoreInterface $keyValue) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
  }
  
  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container->get('keyvalue')
      ->get('block_test'));
  }
  
  /**
   * {@inheritdoc}
   */
  protected function blockAccess(AccountInterface $account) {
    return $this->keyValue
      ->get('access', FALSE) ? AccessResult::allowed()->setCacheMaxAge(0) : AccessResult::forbidden()->setCacheMaxAge(0);
  }
  
  /**
   * {@inheritdoc}
   */
  public function build() {
    return [
      '#markup' => 'Hello test world',
    ];
  }
  
  /**
   * {@inheritdoc}
   */
  public function getCacheMaxAge() {
    return Cache::PERMANENT;
  }

}

Classes

Title Deprecated Summary
TestAccessBlock Provides a block to test access.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.