The Sloth content type in the previous lesson had fields for name, weight, and birth date. The fields hold different types of data. Names are text. Weights are numbers. Birth dates are, well, dates.
Drupal has lots of field types, to store whatever data you need. Modules add even more, like map coordinates. In this lesson, we’ll look at some of the most common field types.
When you create a new field, you get to select the field type:
Drupal core gives you 23 field types. Let’s talk about the most common.
Text field types
Text is stuff you can type, like “Colorless green ideas are about 4.5 nanometers wide.” It includes letters (upper- and lowercase), digits, special characters (*-_=+,\). It also includes non-Latin characters, like ఊ, Ϫ, and ⑬. There are lots of funky things. This page lets you see more. Drop down the “go to other block” thing on that page, and explore.
Here are the text field types in Drupal:
Plain fields can’t have any HTML in them. You can’t have images, for example. Formatted fields can contain HTML. Images, links, tables, whatevs.
Here’s what a plain field called About looks like when you’re editing it.
Here’s About as a formatted field:
You’ve seen this before, of course. You can select the text format below the text area.
So that’s plain vs formatted. Another difference is whether a field is long, or not. Both of the text fields above are long, that is, they can have many lines of text. A regular text field is just a single line:
So there’s plain vs formatted. and normal vs long. There are two special text field types as well. One you’ve used already. It’s “Text (formatted, long, with summary)”. The Body field in the Basic page content type is one of these.
This field has two parts to it: the full value, and a summary. For example, here’s a Biography field:
Why does Drupal do this? It has to do with the way people use information. Say you want to find a sloth, but you’re not sure exactly what you want. How can Drupal help? Give you a list of sloths with a little information about each. You quickly scan the list, looking for a sloth that might fit the bill. When you find one, you can check out its details, to see if it might be the one you want.
An example. Let’s add a Biography field to the Sloth content type, and fill it in for all the sloths. Now, let’s make a node for Ed, a new sloth.
So we have two versions of the biography. A complete one, and a summary.
We add the summary version to a view (you’ll learn about views later), so it looks like this:
Here comes Edwina, looking for The One. She looks down the list, reading the summaries. Swirly hair, no, work-in-progress, no, dancing, no, smelly… hey! There’s a maybe! She clicks on the Ed’s name, and sees his full biography:
Wow! Smelly to the max! Definitely worth a swipe.
So, having separate summaries makes good business sense. Lets you show data in ways that help people make good decisions quickly.
- New South Wales
- South Australia
- Western Australia
You can tell Drupal just to allow these values for State.
When you create the field, you give Drupal a list of values, like this:
What does it look like when users enter state data? You can have a dropdown list (aka select list):
Also, it’s quicker to click on something than to type.
To summarize for text:
- Plain vs formatted
- Long vs normal
- Field with main and summary
- List of text values
You have options when you want to store numbers.
“Number (integer)” is for whole numbers, like 13 and -42.
“Number (float)” is for numbers like 2.7382 and -48.2, that have a varying number of decimal places. Floats can also contain whole numbers, like 15.0.
“Number (decimal)” is for things like prices, that have a fixed number of decimal places.
The list types (float and integer) are like the text list, when you have a few possible values for numbers.
You can figure out what “Telephone number” is for.
An example. Let’s add a field to store a sloth’s number of limbs. Most sloths have four, but some grow extra limbs, like slothy spiders. Eewww!
Start by telling Drupal the name of the field and the type (we’ll go over the clicky clicky procedures later; right now, it’s about the concepts).
It’s an integer field. That means whole numbers.
We fill in some options for the field:
The default is what Drupal assumes if the user doesn’t type anything. Most sloths have four limbs, so that’s a sensible default. Min and max are the smallest and largest values users are allowed to type. Try to tell Drupal that a sloth has -2 limbs, and you’ll get an error.
Prefix and suffix is text to put before and after the value. Common uses:
- 4 limbs
- 98 degrees F
- $9.95 ($ is a prefix)
In the Suffix option, “limb|limbs” means that Drupal will use the suffix “limb” when there is one, and “limbs” otherwise.
Date fields are kind of messy. First, you can tell Drupal whether you want to store date only (like for a birthday), or date and time (like when a meeting is scheduled).
Second, there is the date format. Many choices, like:
- Jun 30, 2016
On most of the planet, numeric dates are “day month, year,” like 30-6-2016. In the US, numeric dates are “month day, year,” like 6-30-2016. Makes no sense, but that’s the way it is. We’ll use US dates here, given the target audience for this course.
Drupal is good at internationalization, or I18N in geek speak. It can automatically switch date formats. So Bill in the US and Ben in Australia could be looking at data for the same node on the same Drupal IS, but see the dates in different formats.
We won’t be talking about I18N in this course. Just remember that if your company is global, Drupal is a good choice.
Drupal gives you a bazillion choices for date formats:
Actually, that first one, “Date only”, isn’t in the standard Drupal list. I added it myself. To add a date format, go to Configuration | Regional and language | Date and time formats. Click Add format, give a name, and a format string:
The format string is a bit geeky. M is short month, j is day without a leading 0 (that is, 8, not 08), and Y is year.
Special field types
Text, number, and date are the most common field types, but there are others. Like:
- Boolean, for when there are two options (yes or no, true or false, female or male)
- Email address
- Link, a link to a Web page
- Image, store an image in a field, like a photo of an employee
- File, store a file in a field, like a PDF of a tax form
Modules add others, like map coordinates, addresses, and videos.
Other field attributes
So, we have fields. Each field has a field type, like “Number (integer)” or “Text (list)”.
There are a few attributes that all fields have, no matter what their type.
If you mark a field as “Required,” that means that it has to have a value. For example, for the Sloth content type, you could mark the Weight field as required:
Drupal won’t let users create new Sloth nodes, if they leave weight empty.
Be careful with this. It sounds like a good idea to have lots of fields required, but in a real business, it can get in the way. Maybe a sloth comes in, and you want to enter its data. You go to weigh it, but the scale is broken that day. You can’t weigh it, so you don’t have anything for weight.
Would it be better for the user to:
- Leave the weight field empty
- Not enter data for the new sloth
For my money, leaving the field empty is the best idea. You can enter the rest of the sloth’s data, and have a record of it. You know that weight is missing, and can fill it in later, when the scale is fixed. For that to work, the weight field should not be required.
Think about what’s best for the business, when you decide whether to make fields required or not.
Help text shows up below fields, like this:
Is the weight in pounds, kilos, or something else? This would be a better help message:
Another good idea is to give an example of valid input. For a favorite band, you might have help like: Sloth’s favorite band, e.g., Pink Floyd.
OK, go to your
cowbooks site, or another one, and let’s create a sloth content type, and add fields.
Go to Structure | Content types, and click the Add content type button. Give it the name Sloths. There are tabs with options at the bottom of the page.
- Under Submission form settings, change Title to Name in Title field label.
- Under Display settings, turn off Display author and date information.
Now click the button Save and manage fields.
You’ll see that Drupal created a Body field for you automatically. Delete it; we don’t need it.
We want three fields:
- Name, e.g., Kitty
- Weight, e.g., 18.4
- Birth date, e.g., December 7, 1941
Name we already have. Each node has a title. We use that for name.
Let’s add weight. It’s a number, with a decimal part. Click the Add field button. You’ll see:
Choose Number (float) as the field type. Remember that float means “can have a decimal part.”
Click Save and continue.
The next screen:
Each sloth only has one weight. It can’t have two weights; what would that even mean? So leave it at one, and click Save. We’ll talk more about multivalued fields later.
The next screen asks for some more field settings. Type in a help message:
Click Save settings. You’ll see:
OK, we have a new content type with a title (that we’re using for name) and one field. Let’s give it a try. Content in the admin menu, then Add content. Click on Sloth. You’ll see a form like this:
Nice! There are our fields.
One more field to add: birth date. Structure | Content types | Sloth, Manage fields | Add field. Select Date as the field type, and give it the label Birth date:
Under field settings, choose Date only, and one value:
Add some help text, and save again.
The Content | Add content | Sloth:
Go ahead and add a couple of sloths.
Manage form display
In the admin menu, click Content. You’ll see the sloths you added:
Click on the title of one, Bill in my case. You’ll see the data you entered about the sloth:
Hmm. The data is there, but the format is strange. The birth date, especially.
Let’s fix that. In admin, Structure | Content types | Sloth. You’ll see four tabs:
They control different aspects of the content type:
- Edit: sets the name of the content type (Sloth) and some basic settings.
- Manage fields: lists the fields of the content type, in alphabetical order. Does not show what the fields look like.
- Manage form display: lets you control the form you use to enter sloth data.
- Manage display: lets you control how Drupal shows sloth data to users.
OK, we have this:
That’s how the data looks like to users. That’s controlled in Manage display. So let’s click that tab:
There are three rows. The first one, Links, controls the View, Edit, and Delete links:
We don’t need to change them, so let’s leave them alone.
The next row controls how the weight field looks. Click the gear icon on the right. Change Scale to one and Update:
Now click the gear to the right of Birth date. Choose the HTML date format and Update.
Now click Save. Don’t forget! Otherwise, your changes will be lost.
Have a look at a sloth again, and you’ll see the changes at work:
W00t! We created a content type, added some fields, and adjusted the display.
You want to build an IS to let people reserve meeting rooms. They’ll have a form they fill in, choosing the building, the room, etc.
You create a Meeting content type, and some fields:
- When starts
- When ends
- Meeting name
- Contact human
- Contact email
- Contact telephone
What field type should each field be? Be specific. Not just Text, what type of text field? Not just Number, what type of number?
Make a page with the answer on
contenttypes, or another of your Web sites. Add a table like this:
The format doesn’t have to match exactly.
Submit the URL of your page.
(If you were logged in as a student, you could submit an exercise solution, and get some feedback.)
Fields hold different types of data. Drupal has lots of field types, to store whatever data you need.
Text is stuff you can type. Plain text fields can’t have any HTML in them. Formatted fields can contain HTML.
Long text fields can have many lines of text. A regular text field is just one line of text.
There’s a text field type with a summary and a main section. You can also have a list of text values.
There are several number types: whole numbers, numbers with varying decimal places, fixed number of decimal places, number lists, and a telephone number type.
Dates can be date only, or date and time. There are many date format choices. You can add your own.
There are special field types, like email address, and image.
If you mark a field as “Required,” that means that it has to have a value.
Help text shows up below fields.