Drupal - How do I invalidate cache of an embedded view?

Try to use buildRenderable(), that should ensure that the necessary cacheability metadata is added automatically.

$vars['content'] = $view->buildRenderable();

Hint: You might be interested in my Views Custom Cache Tag project. The node_list cache tag is invalidated every time any type of node is saved, even though your view only changes when an event changes. Using that module and a bit of custom code as shown on the project page, you have fewer cache invalidations and better performance.


Add the cache tag to the render array, not the object:

$vars['#cache']['tags'][] = 'node_list';

In preprocess you are working with render arrays, not objects. Look at this page of the documentation: https://www.drupal.org/developing/api/8/render/arrays/cacheability

Tags:

Caching

Views

8