Score:0

Adding cache tags to custom block

kn flag

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 :

<?php

**
 * @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 :

parameters:
  http.response.debug_cacheability_headers: false
  twig.config:
    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

4uk4 avatar
cn flag
If you set `http.response.debug_cacheability_headers: true`, do you see 'taxonomy_term_list' in the response headers?
kn flag
Where should I check this ? inside my block by passing *$request* in `build()` ?
4uk4 avatar
cn flag
In the F12-tools of your browser or command line `curl --head www.example.com`.
kn flag
It's missing from the head
4uk4 avatar
cn flag
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.
ru flag
Be sure to do a full rendering (like `{{ content }}`) in your Twig template. If you do only partial rendering (like `{{ content.field_foo }}`), the cache tags will never bubble upwards and will never be included in the final page. If you can't to a full render, do something like `{% set dummy_trigger_cache = content|render %}` to trigger cache processing.
kn flag
@4uk4 why woudn't it be rendered correctly ?
4uk4 avatar
cn flag
For example, you would put the terms in an array and loop over this array in the template, this would be a problem in the block template. Use a separate template, if you can't build this in a recursive render array.
kn flag
So I have to print always `{{ content }}` as suggested @Hudri ?
4uk4 avatar
cn flag
Not always, but in general it's good practice to use the block template as a wrapper around content and render the inside in a custom template if necessary.
Score:1
cn flag

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

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.