TestCacheBlock.php in Drupal 8
File
core/modules/block/tests/modules/block_test/src/Plugin/Block/TestCacheBlock.php
View source
<?php
namespace Drupal\block_test\Plugin\Block;
use Drupal\Core\Block\BlockBase;
class TestCacheBlock extends BlockBase {
public function build() {
$content = \Drupal::state()
->get('block_test.content');
$build = [];
if (!empty($content)) {
$build['#markup'] = $content;
}
return $build;
}
public function getCacheContexts() {
return \Drupal::state()
->get('block_test.cache_contexts', []);
}
public function getCacheMaxAge() {
return \Drupal::state()
->get('block_test.cache_max_age', parent::getCacheMaxAge());
}
}