Multivalued fields

Fields so far

Content types have fields. For example, the content type Sloth has fields like name, weight, and birth date. Sloth is a type of thing. Weight is an attribute that all sloths have.

When you add a sloth, you create a new node of the content type Sloth. Drupal automatically creates a page for that node.

Sloths

Every field has characteristics besides field type. For example, every field has help text. Users see help text when they type data into fields.

Every field has a Required flag. If a field is required, it must have a value. For example, the Title field is required. Try to create a node without a title, and Drupal will complain.

Title required

Multivalued fields

Most fields have just one value. For example, a sloth only has one name, one weight, and one birth date. Does that always work?

Sloths are art nerds. They love visiting galleries. They stay up late into the night, arguing the merits of The Treachery of Images.

Say you want to record each sloth’s fave artist. Easy. You create a text field.

Creating field

You tell Drupal that the field can have only one value, that is, each sloth hast just one favorite artist:

One value

You enter data like this:

Entering data

The next day, you wake up covered in sloth feces. Turns out, sloths are serious art nerds. The idea of having just one fave artist is offensive. They don’t want to be shackled to such arbitrary constraints. Every sloth has many fave artists.

Fortunately, Drupal can handle that. When you were setting up the field, you told Drupal that it could have only one value:

One value

You can also do this:

Unlimited values

You also change the field’s label and help, changing to plural:

Label

How do you enter data? Here’s a sloth’s first fave:

Fave artist

To enter the next one, click on the Add button:

Fave artist

Then type the second name:

Fave artist

When someone looks at the node’s page, they see:

Fave artist

W00t! No more feces! You can add as many fave artists as each sloth wants.

Real world multivalued fields

Multivalued fields are common. For example:

  • One sales rep can have many customers.
  • One customer can buy many products.
  • One student can take many classes.
  • One class can have many students.
  • One sloth can throw feces at many presidential candidates.

Exercises

Exercise: Add multivalued fields to sloth
Add multivalued fields to the sloth content type.

First, favorite artist field (text, plain)

Second, favorite compass directions, North, East, West, and South. Make it a multivalued text list field.

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.)

Exercise: Add multivalued fields to martians
Add multivalued fields to the martian content type.

First, favorite teas. There are ten (make up their names). Make a Text (list) field, with a select. (Hint: to select more than one item in a dropdown, hold down CTRL and click on items.)

Second, dates that the martian missed work.

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

When you were setting up a field, you can tell Drupal that it can have more than one value.