Why Use the Corresponding Entity References Module?
Corresponding Entity References (CER) is a very useful module if you have two entity references that talk to each other. If you update one entity reference, CER does all the work to make sure the other entity reference reflects that update. It makes it easy to quickly associate entity references without having to edit two entities.
In this example, I will make two content types: Timeline and Timeline Event. I want to associate the two content types with each other. To do this, I will make a field on each content type that will be an entity reference field. Using CER, I will connect these two fields.
Thanks to CER, when I create a new Timeline Event, I just have to select the Timeline that I want associated and presto, the Timeline Event will show up on the Timeline. If I edit or delete the Timeline Event, then the changes will be reflected on the Timeline. If I edit the Timeline, the changes will be reflected in Timeline Event.
CER a great way to save time and make sure all your content is consistent.
Let’s get started!
First, the module depends on other modules. Make sure CTools, Entity API, and Elements modules are downloaded to your site.
The module is located here:
I downloaded and used version 7.x-3.x-dev.
First, I need to download the module and enable it. I can do this in my terminal by using drush and these two commands:
- drush dl cer-7.x-3.x-dev (or drush @[yoursite] dl cer-7.x-3.x-dev )
- drush en cer (or drush @[yoursite] en cer)
Now I need to make the fields on the two content types that I want to correspond to each other. I am going to connect the Timeline to the Timeline Event.
I need to add the field on the Timeline Event content type. I am naming this field Associated Timeline.
I need to make sure that Associated Timeline is a Field Type: Entity Reference. Now I navigate to Entity Selection and Target Bundles. Since Timeline Event is only going to talk to Timeline, I only need to select Timeline.
There are more setting options to choose from, such as the number of values and default value.
Now I need to go to the Timeline content type. This is the content type that I want to tie to Timeline Event.
I need to add a new field to Timeline. This field will also be an entity reference. I will name this field Associated Timeline Events.
For the field’s settings, I navigate to Entity Selection and Target Bundles. This time, I select Timeline Event.
I am going to set the number of values of Unlimited so I can associate more than one Timeline Event to a Timeline.
Now I have my two fields on my two content types, and both fields can target bundles from the content type they want to talk to. In other words, Timeline can choose from Timeline Events, and Timeline Events can choose from Timeline.
Let's set up the Corresponding Entity References.
To set up the Corresponding Entity References I will navigate to Configuration, select Content authoring, and then select Corresponding Entity References.
Then I click Add Preset.
I will select one of the options - Associated Timeline or Associated Timeline Event - I created for the Left Field and the other field for the Right Field. If I select bidirectional, it does not matter which field I place in the Left Field and Right Field. I also need to make sure the preset is Enabled.
Bidirectional means I can make the change on either content type and it will change the other content type.
If I do not choose bidirectional - then the Left Field will reflect changes, but the Right Field will not reflect these changes. In other words, if I set the Associated Timeline Event field as the Left Field and the the Associated Timeline field as the Right Field, the Timeline Event will change but the Timeline Event will not populate on the Timeline. Not sure why anyone would choose this, but hey, it's an option.
Once I hit save, I am redirected here. Good to go!
Now let's test it out.
First, I need to create my Timeline. The Timeline is called “A Short History of The Internet.” I want to create 3 separate Timeline Events and attached them to the Timeline.
I need to fill out the fields in the new Timeline. Since no Timeline Events exist yet, I cannot associate the two entity references yet.
Now I will create my Timeline Event. I need to associate the event with the timeline.
Now if I navigate back to the new Timeline, the new Timeline is magically populated and associated on the new Timeline Event node.
When I edit the Timeline node, the Associated Timeline Event field is now filled in!
Sweet, now I have Timeline Event that is associated to my Timeline. Let’s add a couple more events.
Let’s see the magic of the module in action. When I create a new Timeline Event, associate it with the Timeline then the timeline associated magically updates. On the content page, the Timeline will be marked as updated.
If you go to edit the Timeline, we can see the three new Timeline Events.
Displaying all the the fields
Great, but… I want to show all the fields of the content, not just the titles.
Now I need to go to the manage display of the timeline content type and set the Associated Timeline Events field Format to Rendered Entity. The “Rendered as” needs to match the view mode I want displayed for the Timeline Event.
Now all the Timeline Event fields are showing up on the Timeline.
Some errors/bugs/problems I have discovered and how to get around them.
- There are problems I ran into with pushing the settings up in Features. Do not include any CER presets in Features, instead recreate the CER preset on each environment. Also you might need to uninstall and reinstall the module per environment. If you encounter a problem where not all the CER fields show up (including Left Field, Right Field, Enabled, Bidirectional and Weight) reinstalling the module will fix it.
- I cannot automatically order the Entity Reference fields. They will be ordered in the way that they are created. I can manually switch their order by dragging them. If I want to automatically order the Entity Reference fields based on some entity field such as a date, a view will need to be created.
The CER module is a great way to make sure you only have to edit connecting content once. The module keeps your content consistent and clear, and that is always a great thing.
Now I present a cat sneezing.