TestCacheOptionalBlock.php
Namespace
Drupal\block_test\Plugin\BlockFile
-
core/
modules/ block/ tests/ modules/ block_test/ src/ Plugin/ Block/ TestCacheOptionalBlock.php
View source
<?php
declare (strict_types=1);
namespace Drupal\block_test\Plugin\Block;
use Drupal\Core\Block\Attribute\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\CacheOptionalInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* Provides a block implementing CacheOptionalInterface to test its caching.
*/
class TestCacheOptionalBlock extends BlockBase implements CacheOptionalInterface {
/**
* {@inheritdoc}
*/
public function build() : array {
$content = \Drupal::keyValue('block_test')->get('content');
$build = [];
if (!empty($content)) {
$build['#markup'] = $content;
}
return $build;
}
/**
* {@inheritdoc}
*/
public function getCacheContexts() : array {
return \Drupal::state()->get('block_test.cache_contexts', parent::getCacheContexts());
}
/**
* {@inheritdoc}
*/
public function getCacheMaxAge() : int {
return \Drupal::state()->get('block_test.cache_max_age', parent::getCacheMaxAge());
}
}
Classes
| Title | Deprecated | Summary |
|---|---|---|
| TestCacheOptionalBlock | Provides a block implementing CacheOptionalInterface to test its caching. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.