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:
Entering data into the field:
A sloth’s page would look like this:
Maybe you’d show that field in the sloth view:
Better
Let’s change things around. Let’s start with data input. Instead of typing the BFF:
How about selecting the BFF from a list:
Here’s Steven’s page:
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.
Then tell Drupal which content types the field can refer to.
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
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.