TestAccessBlock.php in Zircon Profile 8
File
core/modules/block/tests/modules/block_test/src/Plugin/Block/TestAccessBlock.php
View source
<?php
namespace Drupal\block_test\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\State\StateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class TestAccessBlock extends BlockBase implements ContainerFactoryPluginInterface {
public function __construct(array $configuration, $plugin_id, $plugin_definition, StateInterface $state) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->state = $state;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('state'));
}
protected function blockAccess(AccountInterface $account) {
return $this->state
->get('test_block_access', FALSE) ? AccessResult::allowed()
->setCacheMaxAge(0) : AccessResult::forbidden()
->setCacheMaxAge(0);
}
public function build() {
return [
'#markup' => 'Hello test world',
];
}
public function getCacheMaxAge() {
return Cache::PERMANENT;
}
}