I have a custom block which load some taxonomy terms.

When I made changes to my taxonomies, the block isn't loading the new data (due to the cache). I'm passing all my taxonomy tags to my #cache return, but nothing append when I reload my page. I still have to drush cr to see the changes (even tried with taxonomy_term_list, but didn't work either)

Here's a simplified version of my block :


 * @Block(
 *   id = "custom_block",
 *   admin_label = @Translation("custom block"),
 *   category = @Translation("Custom")
 * )
class CustomBlock extends BlockBase implements ContainerFactoryPluginInterface {

    // properties ...
    // constructor...
    // create() ...

    public function build() {
        // ...
        $tags = generate_tags_by_id($terms, self::CACHE_TAXONOMY);
        // ['taxonomy_term:34', 'taxonomy_term:40' ... ]
        return [
            /// ...
            '#cache' => [
                // 'tags' => //$tags,
                'tags' => ['taxonomy_term_list'],

Here's my container config :

  http.response.debug_cacheability_headers: false
    debug: false
    auto_reload: false
    cache: true

Also tried to hook on taxonomy update, launch a custom event which invalidate those caches, but not working.

I'm using drupal 9.5.7

  • 1
    If you set http.response.debug_cacheability_headers: true, do you see 'taxonomy_term_list' in the response headers?
    – 4uk4
    Apr 4, 2023 at 9:06
  • Where should I check this ? inside my block by passing $request in build() ?
    – lobafox
    Apr 4, 2023 at 9:17
  • In the F12-tools of your browser or command line curl --head www.example.com.
    – 4uk4
    Apr 4, 2023 at 9:32
  • It's missing from the head
    – lobafox
    Apr 4, 2023 at 9:48
  • Then your block doesn't render #cache. This can be a problem that $build is not a render array or the block template doesn't render it correctly.
    – 4uk4
    Apr 4, 2023 at 10:00

1 Answer 1


The issue seems to be that you don't build a render array and render it in the block.

If you want to theme a taxonomy term list then don't try to do this in the block.html.twig template. Use a custom template and put it in the block render array:

public function build() {
  // query $terms
  return [
    '#theme' => 'my_term_list',
    '#terms' => $terms,
    '#cache' => [
       'tags' => ['taxonomy_term_list'],

Then #cache will be recognized containing the list cache tag necessary if you query taxonomy terms in the database. The template can then include the cache tags of the single terms while rendering them.

Alternatively you can use a core #type or #theme, for example to render the terms in list tags. See Create unordered list in render array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.