I have
Content types:
- front_banner
- news
Entity Queues:
- front_banner -> only ever contains one front_banner node
- news -> contains multiple news nodes
Views:
- front_banner -> displays rendered nodes (default)
- news -> displays rendered nodes (teaser)
The front banner content type has a field in it that determines whether the text colour should be light or dark when display (sets a class in the twig template, works fine). The news content type also makes use of the front_banner's overlay style field like so:
function mymodule_preprocess_node(array &$vars) {
if ($vars['node']->bundle() == 'news'){
// Set the overlay class (dark/light).
$entity_subqueue = \Drupal::entityManager()->getStorage('entity_subqueue')->load('front_banner');
$items = $entity_subqueue->get('items')->getValue();
$front_node = node_load($items[0]['target_id']);
$vars['overlay_class'] = $front_node->get('field_overlay_style')->getString();
}
}
All of this works perfectly well, my problem occurs when I alter the front_banner node or entityqueue: the cache is obviously not set to refresh for news items when that occurs.
1. Which render cache do I need to rebuild?
Is it the view cache? or the node's render cache? or both?
2. How can I do it?
I have tried creating my own cache tag and flushing it like so:
function mymodule_entity_presave(EntityInterface $entity) {
$tags = ['entity_type:' . $entity->getEntityTypeId() . '.' . $entity->bundle()];
Cache::invalidateTags($tags);
}
function mymodule_node_view_alter(array &$build, Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display) {
if ($build['#node']->bundle() == 'news'){
//dpm($build);
$tags=['entity_type:node.front_page_banner', 'entity_subqueue:front_banner'];
$build['#node']->addCacheTags($tags);
}
}
(entity_subqueue:front_banner
tag is provided by the entityqueue module)
I cannot get the news node page itself to change the overlay style, nor can I get the view that displays news nodes to change the overlay style when the front_banner node is saved.
[Edit 1:] I looked into Views Custom Cache Tags - this does allow me to state which tags I want to affect the views cache - I tried putting the two relevant tags in my view to no effect. Perhaps this means it's the node cache that is giving me grief?
'entity_type:' . $entity->getEntityTypeId() . '.' . $entity->bundle()
renders down toentity_type:node.front_page_banner
, and thatentity_subqueue:front_banner
is provided by the entityqueue module. Can you elaborate if I'm missing something else?