2

I have a content type called basic_page. This content type has a field called field_elements which is an entity reference field. The display settings for this field is set to 'Render Entity' - in other words, when a basic_page node is being rendered, it will also render the referenced entity. All good - no problem.

My question is, I would like to pass run-time information (based on basic_page node) to the referenced entity that should be used in mytheme_preprocess_node when the referenced entity is being 'pre-processed`.

The problem is, by the time hook_preprocess_node is called for the referenced entity - I have no information of the basic_page node that 'contains' the referenced entity.

I was hoping that in hook_preprocess_field I could check if it is field_element and then set some value in the &$variables param I am getting in hook_preprocess_field that will persist to exist up to the point where hook_preprocess_node is called?

1

This works as designed. The referenced node can have multiple parents, but is only processed once and then cached.

A slighly different approach would be to use the context to check inside the referenced node, which parent node is displayed as page content.

Load the basic node with a route match in preprocess of the referenced node. See this question from today on blocks, which can be used in the node preprocess hook in the same way:

How to access field values from a custom block

You would also have to adjust the cache settings of the referenced node, see the comment in the linked answer.

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.