2

I have a content type called Project which can relate to n projects of the same content type. This is done with a field called related projects using the D8 core Entity References. A possible relationship could look like that:

            Project B
          /
Project A
          \
            Project C

Project templates should show all linked projects in both directions. Also would be great to edit the relations in both ways, when editing Project A but also when edition Project B.

This works fine for projects which have projects defined in the Related Projects field, like Project A in the example. But it doesn't work out for referenced projects like Project B and Project C. This is because of the directed nature of Entity References.

I try to use Corresponding Entity References module which "syncs the node reference between two node types which have a nodereference to each other" configuring it like that on /admin/config/content/cer:

  1. First field: related_projects
  2. Second field: related_projects
  3. Package: Node: Project

But Project B and Project C don't get updated when saving Project A. What am I doing wrong? Or is there another possible solution for syncing node references?

1
  • I've had similar issues with CER of late. It doesn't look like the D8 module is working. I had some success with it on a new install a few months ago, but when adding it to a site with content I get nothing… Commented May 30, 2018 at 11:18

1 Answer 1

1

You can display both directions in Parent-Child Entity references with Views. You don't need to edit the reference to note direction in any special way.

The Entity reference field is already showing Children information on Parent nodes (your Project A is showing Project B and Project C).

List Parent links on Child nodes using Views

To show Parent links on nodes that are referenced from that Parent (backwards relationship) create a block and display it on the Child nodes:

  1. Create Views of type Content Project. You'll probably have the Title field already there, leave it for now just for testing.
  2. Add a Contextual filter ID from Content Category and configure it:
    When the filter value is NOT available:
    Provide default value
    Content Id from URL
    When the filter value IS available or a default is provided
    Specify validation criteria
    Validator Content
    Project

    Now if you enter the node ID (either Project will do: A, B or C) into the Preview argument field, it will only show that node Title in the Views results. This is just to test that our contextual filter is working correctly, when we place it on Project pages.

  3. Add a new Relationship Content using field_project_reference (Relate each Content with a field_project_reference set to the content.) This is the backwards Relationship! Be careful not to include the forwards one that is called "Content referenced from field_project_reference"!

    Require this relationship because it makes sense, we want only the results if the node we're on actually has a Parent, but it won't matter much if you don't tick this.

  4. Edit the existing Title field and set it to use the Relationship from the previous step. The field using that Relationship will show the title of the Parent node, and not the one you're currently viewing, like it did up until now.

    Edit the label to say "Other Projects referencing this Project" or similar.

    Under No results behavior choose
    Hide if empty

    This will ensure that if the node you're currently viewing doesn't have any other nodes referencing it, nothing will be displayed. Otherwise, you'd get the Label and No results text (if you have it configured which you always should), or just the Label "Other Projects referencing this Project" with nothing listed there. With "Hide if empty" the block will not display anything which is more inline with displaying Fields on node pages.

  5. Now place this Block on Project nodes in Content region if you want it show close to the orignal Entity reference field, set the Block settings Content ID: Node from URL and restrict to show it on Project types only.

You can use these Views to display other information from references nodes, not only Titles.


In Drupal 7 the module Relation provided two-way references, but it seems in Drupal 8 you can recreate that without that module. If you need fields on your Relation/Entity reference or other features read the comments at the bottom: Relation for Drupal 8 Development.

2
  • Thanks for your extensive answer. I wanted to avoid doing it by views for the need of modifying the template. Also would be great to reflect the bidirectional relationship when editing a node so both sides can change the relations.
    – geraldo
    Commented Jun 2, 2018 at 12:45
  • Try reading the Issue I linked to on the Relation module, there is a suggestion how to recreate the Relation module features >"If you wanted to you could create entity type called Relation and then dynamic entity reference field called Endpoints to it and you'd have this relation module functionality in D8, no Relation module required." I'm not sure about the editing both side when editing one node though.
    – prkos
    Commented Jun 2, 2018 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.