CacheableMetadataCalculationTest.php in Drupal 10
File
core/modules/views/tests/modules/views_test_cacheable_metadata_calculation/src/Plugin/views/access/CacheableMetadataCalculationTest.php
View source
<?php
namespace Drupal\views_test_cacheable_metadata_calculation\Plugin\views\access;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\State\StateInterface;
use Drupal\views\Plugin\views\access\AccessPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\Route;
class CacheableMetadataCalculationTest extends AccessPluginBase implements CacheableDependencyInterface {
protected $state;
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'));
}
public function access(AccountInterface $account) {
return TRUE;
}
public function alterRouteDefinition(Route $route) {
}
public function getCacheContexts() {
$this
->cacheableMetadataHasBeenAccessed();
return [];
}
public function getCacheTags() {
$this
->cacheableMetadataHasBeenAccessed();
return [];
}
public function getCacheMaxAge() {
$this
->cacheableMetadataHasBeenAccessed();
return Cache::PERMANENT;
}
protected function cacheableMetadataHasBeenAccessed() {
$this->state
->set('views_test_cacheable_metadata_has_been_accessed', TRUE);
}
}