I have a node type "Article" that contains a repeating Paragraphs field. One of the paragraph types let's the user embed a view using a entity reference field. When rendering an Article node containing a views reference I embed the selected view in
hook_preprocess_paragraph like this:
$view_id = $paragraph->field_view->target_id; $view = \Drupal\views\Views::getView($view_id); $view->setDisplay('embed_1'); $vars['content'] = $view->preview();
This is working fine except when I enable caching. For example one of my views lists nodes of the type "Event". When I add a new Event it does not show up in the listing until I clean the cache. I could force clean the cache when inserting / updating / deleting an "Event" but I would much rather use Drupal 8's cache tag system and only invalidate the cached content that contains event lists. The problem is I cannot get it to work.
So far I have tried adding cache tags to the node, the view, and the paragraph in
hook_preprocess_node like this:
$vars['paragraph']->addCacheTags(['node_list']); ... $view->storage->addCacheTags(['node_list']); ... $vars['node']->addCacheTags(['node_list']);
According to this page
node_list can be used for node listings to invalidate the cache whenever a node is updated. But it has no effect. Maybe the preprocess hooks are to late to add cache tags?
What am I missing?
After some more testing I found that if I edit an event, the changes can be seen in the listing and if I remove an event it disappears from the listing. The problem only occurs when adding new events.