- π·πΊRussia Chi
Steps to reproduce are still very complected. If you are still capable to reproduce this, please write a test to prove the issue.
- πΊπΈUnited States devkinetic
I'm running into something similar, with some easier reproduction steps. In conjunction with the twig_tweak code, we are using Varnish.
1. Create a node type, for example "US State".
2. Create a node type, "news" with a reference field to "US State".
3. Add a "US State" node.
4. Add a "News" node, and set the reference to the "US State" created in step 3.
5. Add the view that shows "News" nodes, with a contextual filter for a NID of a state.
6. Add the following Twig to the node template for a "US State".{% set news_results = drupal_view_result('news', 'block_1', node.id)|length %} {% if news_results > 0 %} {{ drupal_view('news', 'block_1', node.id) }} {% endif %}
7. View the "US State" node. You will see the block rendered with the related News item.
8. Edit the "News" node and remove the reference to the "US State".
9. View the "US State" node again, you will no longer see the view, as there are no results.
10. Edit the "News" node again and add the reference to the "US State" again.
11. View the "US State" node again, you will not see the view, even though there is a result.I'm assuming that this is because the node template does not have the proper cache tag after step 9, since the view doesn't render.
In my case in particular, using
drupal_view_result
doesn't add cache tags. I have to first check for results as the view I am using shows a "view all" link, and it will render that always, as well as some extra HTML markup I want to only add when it makes sense.Full Example:
{% set news_results = drupal_view_result('news', 'block_1', node.id)|length %} {% if news_results > 0 %} <section class="news"> <div class="grid-container"> <div class="grid-row"> <div class="grid-col"> <div class="news-list--block"> <h3 class="news-list--title">{{ 'Recent News'|t }}</h3> {{ drupal_view('news', 'block_1', node.id) }} </div> </div> </div> </div> </section> {% endif %}
- π¦πΊAustralia gordon Melbourne
I continued to investigate this and I managed to solve the problem.
Because of the commit above I found that the caching is a bit more strict. I found that I needed to add context to the block.
Basically my offending block was was grabbing the node from the route and using that to render. So by adding context to the block so it knows that it is using the node then fixed my issues.
I found the resolution to the issue from #2980324 π Document how to use block contexts Fixed which solved the issue of the and then everything worked as required.
- Status changed to Closed: works as designed
about 1 year ago 8:40am 6 January 2024 - π·πΊRussia Chi
Added an example of rendering block with context to Cheat Sheet.
Closing this issue as there is nothing doable here.