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.
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.
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.
You tell Drupal that the field can have only one value, that is, each sloth hast just one favorite artist:
You enter data like this:
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:
You can also do this:
You also change the field’s label and help, changing to plural:
How do you enter data? Here’s a sloth’s first fave:
To enter the next one, click on the Add button:
Then type the second name:
When someone looks at the node’s page, they see:
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
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.)
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.