Getting Started with References in Drupal

Joel Steidl

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!

In this example I am using Drupal 7 core and the Entity Reference module.

Creating the example

You can also follow along with the screencast if you like.

Create content types

  • Artist
    • Name (title field)
    • Biography (body field)
    • Photo (image field)
    • Albums (Entity Reference referencing the album content type)
  • Album
    • Title (title field)
    • Album Artwork (image field)
    • Year (text field)
    • Tracks (Entity Reference referencing the track content type)
  • Track
    • 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

Creating Content

We can now create artists, albums and tracks but the workflow isn't ideal.

Default workflow (cumbersome)

  1. Create all tracks for an album
  2. Create the album (reference the tracks created above)
  3. Create the artist (reference the album created above)

Repeat for each new album (or artist / album)

A more ideal workflow

Using a couple contributed modules, (references dialog and inline entity form), we can greatly improve this workflow.

  1. Create an artist, create albums and tracks directly from the artist edit screen!

References dialog

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.

  1. Enable references dialog
  2. 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.

  1. Enable inline entity form
  2. 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.

  1. Modify the view mode display for artist and album
  2. 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?


About the Author

Joel excels at making complicated problems simple by organizing and planning. Nothing brings him more joy than making sense out of a mess.

Read More