0

I have two content types. For clearer question I will work with the content types event and location.

From my location content type I can reference to and event.

Then on the event page I show the location that is connected to this event. I do this by creating a Views Block. This views block is showing content of the type location with a contextual filter to only show content with the Content ID from url.

This works fine. But there are events that don't have a location. On these events page I have a button saying add location. I do this with the following Twig Tweak:

{% if drupal_view_result('view-name', 'location-block') is empty %}
    <a href="/node/add/location">Add location</a>
{% else %}
    {{ drupal_view('view-name', 'location-block') }}
{% endif %}

This also works. But when I am on a event page and click the add location button I can add the location. When I go back to the same event page I was before I still see the add location button. Because the page is cached.

Is there a way to empty the cache of this event page after saving the location that is refering to the event? Or is there a other way to deal with this?

I tried if the problem was in the views cache but it wasn't turning it off for the block or removing only the views cache didn't work.

Thanks in advance

6
  • 1
    Sounds like you might need some extra cache tags or cache contexts for nodes in this case.
    – Kevin
    Jun 24, 2019 at 15:23
  • It sure does but I have no idea how to do this. Any suggestions?
    – Joost
    Jun 24, 2019 at 17:52
  • 1
    Better place the View in a block and put the button in the no results area.
    – 4uk4
    Jun 25, 2019 at 8:47
  • @4k4 this would have been a nice option but I need the nid of the events page I am looking at so to be precise the link should be something like: <a href="/node/add/location?nid={{nid}}">Add location</a> I couldn't find a way to do this with Views.
    – Joost
    Jun 26, 2019 at 11:46
  • @Joost, for the current node id try [current-page:url:unaliased:args:value:1] in a Views text area, see drupal.org/node/1360922
    – 4uk4
    Jun 26, 2019 at 12:04

2 Answers 2

1

You may specify any cache meta data directly in your Twig template. I suppose 'node_list' cache tag can help in this case.

{{ {'#cache': {'tags': ['node_list']}} }}

Another approach could be configuring views 'No results behavior' to show that link.

1
  • Did you test this? I don't see any document say that?
    – Jonh
    Jul 6, 2019 at 9:16
0

Since you're creating a reference to a location node, you'll know its node id upon saving the event.

At that point, you need to call \Drupal\Core\Cache\Cache::invalidateTags([$nid]) where $nid is the node id of the location.

There are many possibilities where this could be invoked, for example, in hook_entity_presave()

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.