class TestCacheOptionalBlock

Provides a block implementing CacheOptionalInterface to test its caching.

Attributes

#[Block(id: "test_cache_optional", admin_label: new TranslatableMarkup("Test block cache optional"))]

Hierarchy

Expanded class hierarchy of TestCacheOptionalBlock

File

core/modules/block/tests/modules/block_test/src/Plugin/Block/TestCacheOptionalBlock.php, line 15

Namespace

Drupal\block_test\Plugin\Block
View source
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());
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.