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 Answer 1

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.

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.