References

Fields have types, like List (text), Date, and Number (integer). There are some important types we haven’t talked about: references. References are fields that refer to other nodes.

BFF text field

Each sloth has a BFF. Just one BFF. They’re prickly about that. One and only one BFF.

Say you wanted to store that info. You could store the name of the BFF:

BFF

Entering data into the field:

BFF

A sloth’s page would look like this:

Sloth

Maybe you’d show that field in the sloth view:

BFF in view

Georgina
Georgina
Wait! That’s not right. Garnet’s BFF is Stephen, not Steven. Are they the same sloth?
Adela
Adela
Look at Steven’s BFF. It’s not spelled right.

Better

Let’s change things around. Let’s start with data input. Instead of typing the BFF:

BFF

How about selecting the BFF from a list:

BFF

Marcus
Marcus
Nice! You don’t type the BFF’s name, so you can’t misspell it.

Here’s Steven’s page:

Steven

Ray
Ray
Cool! A link to the BFF’s page, from Steven’s page.
Here’s the view:

View

Georgina
Georgina
Nice! No spelling errors, and the BFF field is a link.
Ahhh… much better.

Reference fields

References make this magic happen. A reference field refers to a node. It’s like a pointer to a node.

When you create a reference field, tell Drupal that is refers to Content. That means a node of a content type.

Create

Then tell Drupal which content types the field can refer to.

BFF content type selection

References across content types

In the example:

  • BFF is a field in the sloth content type
  • BFF references other sloths

Sloth-on-sloth referencing.

Usually, references are to fields with different content types. For example, you might have a Student content type, and a Course content type. Student could have a field called Enrolled, referencing Course nodes. Students referring to Courses. Different content types.

So, usually, a reference field refers to nodes of a different type. For example:

  • A Sales rep node can reference Customer nodes.
  • A Customer node can reference Product nodes.
  • A Product node can reference Manufacturer nodes.

Required, help, multivalued

Like other fields, reference fields can be required or not. Like other fields, reference fields have help text.

Reference fields can be multivalued. The BFF field was single valued. That is, each sloth has just one BFF. Often, reference fields are multivalued, as when a Sales rep node references more than one Customer node.

Exercise

Exercise: Make some reference fields
You should have Sloths and Martians in the same Drupal site, contenttypes. (If you don’t, make it so.)

Add some reference fields.

First, sloths buy somnolent from martians. It’s the substance that makes sloths so mellow. Each sloth can have more than one supplier. Add an appropriate field to the Sloth content type.

Second, martians follow sloths on social media. Each martian can follow many sloths. Add an appropriate field to the Martian content type.

Submit the URL of your site, along with login credentials for user 1.

(If you were logged in as a student, you could submit an exercise solution, and get some feedback.)

Summary

A reference field refers to a node. It’s like a pointer to a node. They reduce input errors, and make clickable links to other nodes.