2

I've implemented CacheableDependencyInterface on my ViewsField plugin.

  /**
   * {@inheritdoc}
   */
  public function getCacheTags() {
    return ['arbitrary_tag'];
  }

I can see the tag on the HEADER and the View configuration object.

However, when that tag is invalidated, the View is not updated.

I've also tried using adding cache tags to the render method:

return [
  '#markup' => $message,
  '#cache' => [
    'tags' => ['my_list'],
  ],
];

Both to the Views field, but also tried directly on the View element using hook_views_pre_view

While debugging, I've made sure to try invalidate that specific tag:

Cache::invalidateTags(['my_list']);

While debugging further it seems like that the ResultRow values are still cached after clearing the tag.

1 Answer 1

2

Although there are Views plugins which use CacheableDependencyInterface, I'm not sure it is the right way for a field plugin, because a field renders content and a cache tag of rendered content belongs in the render array.

This was discussed in this issue for block plugins. Don't know if this here or in the other issues for blocks is a core issues, because you can see in both core modules the effort to collect plugin cacheable metadata, or Drupal assumes, that all render arrays fulfill the requirements outlined in the docs:

"It is of the utmost importance that you inform the Render API of the cacheability of a render array"

Anyways, an easy fix is to put the cache tag in the rendered output of the field.

4
  • I was a bit to fast on the Approve here, it doesn't actually seem to help to put the cache tags on the rendered output.
    – johndevman
    Mar 3, 2020 at 9:18
  • While debugging further, it looks like the ResultRow values are still the same, but the Views rendering gets rebuilt.
    – johndevman
    Mar 3, 2020 at 9:42
  • When I put your markup with the cache tag in a field plugin the cached result row contains the tag my_list.
    – 4uk4
    Mar 3, 2020 at 10:31
  • Thanks so much 4uk4! I struggled with a ViewsField plugin which was called for a node that has been deleted. Adding the CacheableDependencyInterface did not work, but repeating the cache tags in the render array did. Like: $build['#cache']['tags'] = ['node_list']; Feb 7 at 10:00

Your Answer

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

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