Development

Entity Reference Referrer – Getting Entity Reference’s Parent/Referrer Entity ID and Type

Out-of-the-box, the Entity Reference module doesn’t provide a way to access the referrer entity’s fields from within the referenced entity. I have two ways of doing this. The first is to patch the entityreference module (not ideal unless this patch went out to the community via proper channels; perhaps I should get on that?). The second is to mimic entityreference’s “Rendered entity” field formatter but modify it to include the referrer information. I recommend the second method for now.

Here’s the result (in this example we have a paragraph item (Paragraph module) that has an Entity Reference field within it called field_p_sstory_item):

EA283D4FDD3DF9D04489D9FA4869E3C5 C6859F45446A4C8927D8824F75B8C9C3


Method 1

Patch entityreference.module to include the parent array you see screenshot above

Method 2

Create your own field formatter to include the parent array you see in the screenshot above

This is a basically a replication of the entityreference_entity_view formatter from entityreference.module, with one small change to include the parent entity’s id and entity_type.

Search for the word “referrer” in the code to see what I’ve added.


Usage

Now that you have the entity’s referrer you need to do something with it. Perhaps you’re trying to include a field from the referrer entity in the referenced entity. Here’s one way to do this using Display Suite.

Full Content view mode for the paragraph:

8C23764412885782A4F07D3DB78EEC8B

Callout single large view mode for the news_item (story) node:

C2EB87EEF12878492C7BE171FC21698D

 

Leave a Reply

Your email address will not be published.