2

I am building a CRUD form in Drupal 8. Users get navigated to it from a list that shows the entities they are editing. After an edit or add they get redirected to the list again. The list is generated in a twig template that is being cached. It's a custom route/controller.

How do i invalidate/delete the cache on that specific list page/route after an edit/add of an entity?

The data in the list is build in custom action, it's no view, and as far as drupal knows, there are no nodes shown in the template. So no default cache functions are triggered.

1

AFAIK, cache invalidation should not depend on the page or route. When an entity (eg. node #5) is changed, it should in validate all caches tagged with "node:5". If the list of entities is correctly tagged with its cache dependencies, you should be fine.

I realize this answer is rather theoretical and not very specific. Hopefully other answers will be more detailed.

  • So Drupal invalidates the cache based on what node or user is being shown. But on my list pages, drupal doesn't know what node or user is being shown. – jivanrij Mar 8 '16 at 15:15
  • 1
    What kind of page is it? Is it a view (as generated by the Views module) or custom code? The Views module should add the appropriate cache tags automatically. If it's a custom module, it is your (the developer's) task to add the cache tags. A node's cache tags can be retrieved using $node->getCacheTags(). You can get the entity type's "list cache tag" (which is cleared when new entities are added) using the EntityType::getListCacheTags() method on an entity type object. – marcvangend Mar 8 '16 at 15:52
  • Thanks! Great read. But i found no solution. I took a look at the cache tables and found entry's in the dynamic_page table. Removing these solved my problem. Then i checked the tags on these entry's, they had the tag:rendered. Now on each add & edit action, i do Cache::invalidateTags(array('rendered'));. But later on i will have more custom pages with this tag that don't need new cache files in these situations. Is there a way to add a specific tag to a route/url? I can't find anything related to it. Only some documentation about the cache context that comes close to it, but that's not suffice – jivanrij Mar 8 '16 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.