Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Prior Art
In D7 we had needed https://www.drupal.org/project/entityreference_backreference for exposing backreferences in the entity API. Some other modules and discussions
- https://www.drupal.org/node/1176376
- https://www.drupal.org/project/cnr
- #1083902: Backreferencing (listing nodes that reference the target node)
- https://www.drupal.org/project/backreference
Use Case
Content type A has an entity reference field to Content type B. Getting to Content type A from B would be impossible without entityreference backreference module in other modules that build on the entity api, such as Search API or Rules...
Comments
Comment #1
giorgio79 CreditAttribution: giorgio79 commentedComment #2
giorgio79 CreditAttribution: giorgio79 commentedComment #3
giorgio79 CreditAttribution: giorgio79 commentedComment #4
XanoComment #7
dadangnh CreditAttribution: dadangnh commentedNice to have this
Comment #8
patoshi CreditAttribution: patoshi commentedHow would one access this in 8.3.x?
Comment #9
cosmicdreams CreditAttribution: cosmicdreams commentedWell, not impossible. If you use a preprocess function to input the entity_id of your current entity (Type B) into a look up function that find what Entities of type A have references of type B with the entity_id you have.
Or in code like this:
So, without a field I'm able to control things like this. Sometimes that's all I need. But what you're asking for is some way to have this control + control over the data like being able to modify what is being referenced + control field order / placement ?
Comment #10
deminy@cosmicdreams I'm wondering how the code piece in #9 could work in Drupal 8 cause there is no field called field_reference anywhere in Drupal core (version 8.2.5).
Comment #11
cosmicdreams CreditAttribution: cosmicdreams commented@deminy: This code was written with Drupal 8 in mind. And yes, there isn't one by default. I guess I should have said in my code comment that it assumes that you would create an entity reference field named field_reference.
Other things to notice:
Now if we wanted to have Drupal core provide back references by default there's probably a better way to do this than handling it in preprocess.
Comment #12
GeraldNDA CreditAttribution: GeraldNDA commentedI recently posted a sandbox and a project application for a reverse entity reference module. Any advice,code reviews and/or help getting this official would be appreciated.
Module: https://www.drupal.org/sandbox/geraldnda/2847085
Project Application: https://www.drupal.org/node/2847102
Comment #13
Grayside CreditAttribution: Grayside at Phase2 commentedIn case it's useful, here's some backreference code I've been using. https://gist.github.com/grayside/a7b8aba74ccf36ff984b0b9499b3a188
Comment #16
geek-merlinWe really should have that in core.
The sandbox referenced above is now a full project.
Let's review the code and pull it in!
https://www.drupal.org/project/reverse_entity_reference
Comment #17
dawehnerIf we do that it should be certainly a custom field you opt into , rather than what the module is doing, which is defining these back references automatically. There are back references, like terms, where you easily can destroy your site, due to the sheer amount of issues.