Slated to be part of Drupal 8 core, Entity Reference is a field type module you should be familiar with now. For experienced Drupal users, the reference field is probably not a new concept. Regardless of your Drupal skill level, I hope to give a practical example of how the Entity Reference module can be utilized and introduce a few other modules that greatly improve its usability.
The Entity Reference module provides a new field type that allows entities (nodes, taxonomy terms, etc.) to create a reference (or connection) to each other. The need for references usually comes from needing to create content with complex data points (or fields). My example is for Aten Records (our company's fake record label). Aten Records needs to catalog all of their artists, albums and tracks. Imagine trying to store every album and track for an artist in a single content type (artist for example). It would be challenging because of the data we need to collect for each album and track. It becomes much easier to imagine three content types (artist, album & track), but we need a way to connect the three content types together. Entity References can help!
Creating the example
You can also follow along with the screencast if you like.
Create content types
- Name (title field)
- Biography (body field)
- Photo (image field)
- Albums (Entity Reference referencing the album content type)
- Title (title field)
- Album Artwork (image field)
- Year (text field)
- Tracks (Entity Reference referencing the track content type)
- Title (title field)
- MP3 (file)
- Length (text)
- Writer (text)
Review entity reference field settings
- Make sure it is pointing to the correct entity type (node in our example)
- Make sure it can reference the correct content types
- Make sure it can reference a unlimited number of options
We can now create artists, albums and tracks but the workflow isn't ideal.
Default workflow (cumbersome)
- Create all tracks for an album
- Create the album (reference the tracks created above)
- Create the artist (reference the album created above)
Repeat for each new album (or artist / album)
A more ideal workflow
- Create an artist, create albums and tracks directly from the artist edit screen!
References dialog launches a new content create form in a modal window on the page where you are currently editing content. I've chosen this for album, so new albums can quickly be created without leaving the artist edit screen.
- Enable references dialog
- Modify albums field settings on the artist content type
Inline entity form
Inline entity form embeds the form of the referencing entity directly on the page. I've chosen this for tracks so we don't have modals in modals, because we already have an album modal window open at this point.
- Enable inline entity form
- Modify tracks field settings on the album content type
Displaying referenced content
I will only briefly touch on this topic since it could be a whole other blog post.
- Modify the view mode display for artist and album
- For each entity referenced field, choose "show rendered node"
The sky is really the limit with how you use references in a project. Imagine a social site with profiles referencing work experience, a soccer club website with teams referencing players, or a hiking website with trails referencing other nearby trails. What other tips do you have for folks just getting started with Entity References? What other modules improve usability of Entity References?