FooBlock.php in Twig Tweak 3.1.x
File
tests/twig_tweak_test/src/Plugin/Block/FooBlock.php
View source
<?php
namespace Drupal\twig_tweak_test\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Session\AccountInterface;
final class FooBlock extends BlockBase {
public function defaultConfiguration() : array {
return [
'content' => 'Foo',
];
}
protected function blockAccess(AccountInterface $account) : AccessResult {
$result = AccessResult::allowedIf($account
->getAccountName() == 'User 1');
$result
->addCacheTags([
'tag_from_' . __FUNCTION__,
]);
$result
->setCacheMaxAge(35);
$result
->cachePerUser();
return $result;
}
public function build() : array {
return [
'#markup' => $this
->getConfiguration()['content'],
'#attributes' => [
'id' => 'foo',
],
'#cache' => [
'contexts' => [
'url',
],
'tags' => [
'tag_from_' . __FUNCTION__,
],
],
];
}
}