I am working on a Drupal 8 modules which should push a http response header based on a value stored in an entity or a list of entities.

The module was originally done for D7 and now is in porting up to D8.

The main problem is that by using the Entity Cache in D8 any hooks are bypassed, which had been working in still in D7. So nor hook_entity_load or hook_entity_view are fired but only a single time before the entity is placed into the cache.

We might need to create an extra entity/node to a list and this should then eventually change the pages html-head section and the http response header.

So is there any chance to get informed (by a hook or event) for ALL entities be shown on a page (details or list/teaser)? Or can we store some information to a "cached" page (so a page that was assembled only out of cached entities) ?

  • There is probably two different cache involved. The loaded entity is likely cached (in the enttity cache). A entity_load hook is supposed to be used to add additional data to the entity, not react to its display. That would be why your hook_entity_load() impl. is not called. Then there is probably a rendered entity cache at play too. This one would explain why your hook_entity_view() hook implementation is not called. This one is weirder because of a rendered entity is cached, the hook would still be needed. Aug 25 '16 at 17:19

Although the questions is not so clear I will try to give you some directions. I think it is time to dive into the new Cache API (I haven't done it so far!)

First of all, to get the "information" for a cached page you need to deal with the contexts and the cache tags.

To invalidate the tags you may need to use function invalidateTags(array $tags) (here is an example of using this class).

On the other hand if you want to invalidate cache of each node while loading the node you can set this option to the node_load($nid = NULL, $reset = FALSE) function by using $reset = TRUE.

Your Answer

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