Customizing Content in Drupal 7
Drupal suffers from the “blind men describing an elephant” syndrome. In that Indian fable, one man touches the trunk and declares that an elephant is like a tree branch; another touches the tail and says an elephant is like a rope; a third touches an ear and says the elephant is like a great leaf; and so forth. The thing observed is seen by its parts, according to the needs of the individual.
Since Drupal’s audience has historically been software developers, that’s translated into strong support for text and add-on programming. Two areas where it’s not been as capable are in creating content types with custom fields, and in supporting graphics and text styling within nodes.
Fortunately, both areas are getting better. This chapter tells you how to create more flexible content using tools that are available today. However, a constant stream of new solutions passes through the library of contributed modules at drupal.org/project/modules. For details on how to find and use them, see Chapter 9, “Extending Drupal with Modules.”
Defining Custom Types of Content
Now we come to one of Drupal’s most useful features: custom content types. Through them you can move far beyond built-in types and into a world bound only by your imagination. For example, an “employee” content type could include fields for position and salary, and a “catalog page” content type would have fields for prices, colors, and sizes.
Nearly all Drupal sites today have custom content types. But amazingly, Drupal succeeded for years without this feature. It was first available in 2005 through a contributed (non-core) module called Flexinode, which was replaced by Content Construction Kit (CCK) in 2006. Now, Drupal 7 incorporates most of CCK in its core.
Although this section teaches how to create new content types, the instructions are essentially the same if you want to edit an existing content type, such as an article or basic page. If you do so, be aware that changes to an existing content type will primarily affect only newly created nodes of that content type. If you add an age field to an existing content type, for example, and require that the age field contain a value, then existing nodes without a value in that field will be unaffected—at least until you go to edit the node. At that point, you’d be prompted to fill in the required field.
To create a new content type:
- Click Structure in the Toolbar and then click “Content types.”
- Click “Add content type” to go to the content type edit form .
Most of this form is familiar to you, because it looks somewhat like the one you use to create and edit nodes, which you learned about in Chapter 2, “Establishing Your Drupal Site.”
But there’s an important difference. Changes you make on a node edit form affect only a single node; changes you make on the content type edit form affect every node of that content type. You’re creating a template for future nodes.
- Complete the two fields that give the content type its identity:
- The Name field is where you type the “human-readable” name of the content type. Drupal automatically creates a “machine name” that converts spaces to underscores, and upper-case characters to lowercase characters. (Drupal uses this machine name internally.) However, you can choose to change the machine name by clicking the Edit link in this area .
- The optional Description field is where you add explanatory text that appears on the “Add new content” page to help content editors select the right content type.
- Complete the “Submission form settings” section, which controls the appearance of several parts of the node edit form for
this content type:
- The “Title field label” field comes with the default value of Title. I like to change it to something more descriptive, such as Model name . You can’t leave this field blank.
- The “Preview before submitting” setting determines whether node creators see a Preview button at the bottom of the node edit form. If Disabled is selected, they see only the Save button; if Optional is selected, they see both the Save and Preview buttons; and if Required is selected, they see only the Preview button and are allowed to save a node only after previewing it.
- The “Explanation or submission guidelines” field lets you add a note that appears at the top of the node edit form for the benefit of node creators and editors .
- Click the “Publishing options” tab to set default values for that part of the node edit form. To learn about these settings, see Chapter 3, “Creating and Managing Content.”
- Click the “Display settings” tab to choose whether a node shows its author and date of creation , taken from the “Authoring information” section of the node edit form. Exactly how this information displays is defined by programming in the active theme.
- Click the “Comment settings” tab to control how visitors can “talk back” in response to nodes:
- The “Default comment setting for new content” pop-up menu lets you set whether comments will be allowed at all on nodes of
this content type. (You can change this setting on individual nodes, however.)
Open means comments are allowed.
Closed prevents further comments from being added, but displays any that were already there.
Hidden means all existing comments will be hidden, and no more will be allowed.
- Threading affects whether text is indented so that conversations are easier to follow . When not selected, all comments appear at the same indentation level. This “flat” format is graphically clean but (sometimes) makes it hard to follow individual conversations, which are known as “threads” .
- The “Comments per page” setting defines how many comments appear before the reader has to click links at the bottom of the page to see more. A low value makes comment pages display more quickly, because there’s less information for Drupal to put together and for the server to push through the Internet. I generally don’t find the delay too onerous, so I usually change it to the maximum value (300).
- The “Allow comment title” check box determines whether commenters can write their own subject lines. When unselected, or if a commenter fails to provide a title, Drupal automatically creates one using the first few words of the comment. For example, for a comment that reads “I agree very strongly with your post,” Drupal will generate the (somewhat awkward) subject line “I agree very strongly with.”
- The “Show reply form on the same page as comments” check box determines whether visitors see a comment form directly below the node’s content or have to click an “Add new comment” link to see it. Your choice makes a difference in how many comments you get; it’s surprising how many people won’t bother to click! To encourage more comments, leave this check box selected.
- “Preview comment” options work very much like those for a node’s “Preview post” options. When you select Required, commenters are forced to read through what they wrote before being allowed to post. In practice, selecting Required means that some commenters will fail to realize that they have to preview their posts before saving them and will simply lose their comments by going to another screen. On the other hand, some administrators believe the Required selection cuts down on frivolous, misformatted, and hot-headed comments.
- The “Default comment setting for new content” pop-up menu lets you set whether comments will be allowed at all on nodes of this content type. (You can change this setting on individual nodes, however.)
- Click the “Menu settings” tab to control where you can put menu links to nodes of this content type when you create or edit nodes. See the section “To create a menu item that links to a node” in Chapter 3 to understand how your choice here affects users. You can always add links from any menu by directly editing it: For details, see Chapter 6, “Improving Access to Content.”
- When you’ve made all necessary changes, click “Save content type.”
To change a field’s display and input options:
- In the Toolbar, click Structure and then click “Content types.”
- Click the “manage fields” link next to the content type you want to affect. Besides being a place to add fields, on this screen
you can also:
- Delete a field by clicking its “delete” link, if it’s a type of field that can be deleted. However, you can’t delete fields that modules create or the node’s title field (shown in by the name we gave it earlier, “Model name”).
- Edit the field’s basic criteria by clicking the “edit” link. We discuss the many options on the resulting page in the next step.
- Change the order of fields by clicking and dragging their icons. Note that you have to click Save after reordering fields: You’ll lose all reordering changes if you click other links on this page before clicking Save.
- Change a field’s type, for example from List to Long text, by clicking the link that shows the current type. For the Body field in , that’s “Long text and summary.” (It’s rare that you’ll want to do this.)
- Change a field’s “widget”—that is, the selection tool for entering data in the field. Examples of widgets included in Drupal include check boxes, radio buttons, pop-up menus, and text fields; contributed modules may add other widgets (such as a date selection calendar). In , the widget is currently “Text area with a summary.”
- Click the “edit” link to go to the field settings form.
Options on this form vary considerably, depending on the field’s type and widget. (Our example is of the Integer field type.) In fact, some field types and widgets break these options into more than one screen, in which case you’ll simply have to fill out the first screen, then click “Save field settings” before moving on to the second.
The screen shown in is for a newly created field that’s intended to hold an integer. (You’ll learn how to add fields in the next section, “To add fields to a custom content type.”) The form’s top part is for changing the field’s basic information, including:
- The Label, which you entered when you first created the field.
- The “Required field” check box, which determines whether to force node editors to enter a value in this field.
- The “Help text,” which will appear near the field as a guide for node editors.
- The Minimum, Maximum, Prefix, and Suffix allow you to limit the range of values the user can enter, or to change how the field appears when displayed. For example, you could put a dollar sign before the value so a “5” appears as “$5.”
There are many other options, depending on the field type and widget selected. For example, the “Long text and summary” field type includes a “Summary input” check box that lets you permit (or forbid) entry of a summary section. For more about summaries, see the “Gaining More Control of Individual Nodes” section in Chapter 3.
- Fill out the “Default value” parts of the field edit form. Whatever data you enter here will appear prefilled in the field when someone later creates a node that contains it.
- Complete the “Number of values” pop-up menu that allows you to decide how many values you can have in a multiple value field.
Multi-value fields can be a hard concept to grasp, and it’s best explained with a few examples. Such a field might be used to show:
- The names of gold, silver, and bronze medalists in a content type for Olympic sports events. (In that case, you’d set the “Number of values” pop-up menu to 3.)
- The instruments that a musician plays in an “orchestra member” content type (with the pop-up value set to Unlimited).
- When you’ve finished modifying the field’s settings, click “Save settings.”
To add fields to a custom content type:
- Click Structure in the Toolbar and then click “Content types.”
- Click the “manage fields” link next to the content type you want to change.
- Put the human-readable name in the Label area and the machine-readable name in the “Field name” area. Select a field type,
which determines the kind of data this field can contain.
Generally speaking, Drupal’s field types fall into five categories:
- Text, including Text, Long text, and Long text and summary
- Numbers, including Integer, Float, Decimal, and Boolean
- Selection lists
- References, for example Term reference. (Downloadable modules permit references to other entities, such as nodes and users.)
Other field types may become available as you add modules, as Chapter 9 describes.
- Once you’ve indicated the type of data this field will contain, the widget pop-up menu becomes available and reflects the widgets for the selected field type. Choose the one you want and then click Save.
- The “Field settings” screen displays, where you specify details that are specific to the field type you chose. This section is broken into two screens: The first affects this field in every content type where it appears, while the second is specific to this content type. Complete the field settings form as was described in the section, “To change a field’s display and input options” earlier in this chapter. When you’ve provided all required details, click “Save field settings.” You will return to the list of fields for this content type.
- Put fields in the order you want by clicking and dragging their icons. Click Save when finished.