
Creating content types
As we build this site, we will build a range of content types for different functions. Although these different content types will have varied uses throughout the site, the basic process for creating content types remains consistent.
Tip
A content type and a node type mean the same thing. In most situations, a node is a piece of content.
For this example, we will create a content type for storing and sharing bookmarks.
Adding new content types requires the following steps:
- Create the content type.
- Add fields to the content type (this is optional: not all content types require additional fields).
- Assign a taxonomy to the content type (this is optional: not all content types will be organized using taxonomy).
- Assign permissions to the content type.
Of these four steps, only step one and two need to happen for all the new content types. As we will discuss, some content types do not require additional fields and some content types are not associated with a taxonomy.
Step 1 – creating the content type
To create a content type, follow these steps:
- Click on Structure | Content types or navigate to
admin/structure/types
. - Click on the Add content type link as shown in the following screenshot:
This brings you to the administrative screen to add a content type, accessible at admin/structure/types/add
. As shown in the following screenshot, this screen has six sections:
- Name and Description
- Submission form settings
- Publishing options
- Display settings
- Comment settings
- Menu settings
The Name and Description sections
As seen in the preceding screenshot, the Name and Description section contains two fields:
- Name: This field provides a human-readable name for the content type. The name for a content type should provide a general sense of what the content type will be used for. In this example, as we are creating a node type that will store bookmarks, we will name the content type
Bookmark
. - Description: This field holds a more detailed description of what the content type is used for. It field can hold HTML, so a description can contain, for example, links to external pages. Typically, a good description is brief. For the bookmark, we will use:
Add a bookmark that points to an external website
.
The Submission form settings page
You can view the different fields on the Submission form settings page as shown in the following screenshot:

The values in the various Submission form settings fields allow you to customize what appears to people as they add content on your site. The following screenshot shows where these values appear when people are adding content:

Compare this form with the previous Submission form settings screenshot to see the relationship between the two pages.
When creating new content types, the only value that requires changing is the Explanation or submission guidelines field. In this example and in many cases, we can use the same text that we used for the Description field: Add a bookmark that points to an external website
.
The Publishing options page
You can view the different fields on the Publishing options page as shown in the following screenshot:

The Publishing options section covers the basic rules for what happens to a piece of content when it is created and edited. Under the Default options section, you will want to select Published; without this, regular users will not be able to see your content.
Of the other options, the most commonly used is the Create new revision feature. Selecting this option allows you to create wiki-like functionality; each time a piece of content is edited and saved, it creates a revision and users with sufficient permissions can view and manage older revisions.
The other two flags (Promoted to front page and Sticky at top of lists) are useful if you are using Drupal's default home page or default organization. In most cases, however, we will be organizing our content using the Views module, and these flags will be of limited use.
The Display settings page
The Display settings section covers whether you want author and date information shown on posts, as shown in the following screenshot:

This setting is a matter of personal preference; we'll leave it enabled for now, so we can easily see who created the bookmark and when it was created:

The Comment settings page
You can view the different fields on the Comment settings page as shown in the following screenshot:

The most important setting in this section is the first one, Default comment setting for new content. If you want to allow comments, set this to Open. If you don't want to allow comments on this node type, select Hidden.
The second most important setting is Preview comment. Set this to Optional, as requiring users to preview their comments is an added step that can inhibit interaction on your site.
The remaining settings in this section are largely cosmetic, and the correct settings here tend to be a matter of taste. The settings shown in the preceding screenshot are sensible defaults that get you a nested comment thread, with earlier posts at the top of the thread.
The Menu settings page
You can view the different fields on the Menu settings page as shown in the following screenshot:

The Menu settings section allows the content type to be available to certain menus generated by Drupal. Users can add individual posts to menus on the site by using these settings. Since we won't be adding individual bookmarks to menus on this site, we will not make it available to any menus by unchecking all the boxes under Available menus. The menus will be covered in more detail in Chapter 14, Theming and User Interface Design.
Once you have set the defaults, click on the Save and add fields button to create your new content type.
Step 2 – adding fields
Once the content type has been created, we need to add fields. In Drupal 6, adding fields required the CCK module, but this functionality was added to Drupal's core in Drupal 7 with the field API. Because we are creating a bookmark and need to store a link, which is not one of Drupal's default field types, we do need to install the Link module.
The Link module can be downloaded from http://drupal.org/project/link.
Once you have downloaded and extracted the module, upload it into sites/all/modules
as described earlier in this chapter, and then click on the Modules link or navigate to admin/modules
to enable it.
Drupal's field API allows you to add fields to content types, and the project includes several submodules. In this section, we will enable the various field-related modules we will use throughout this book. In later chapters, we will install the additional modules that will further extend the functionality of fields via the field API.

Of all the modules shown in the preceding screenshot, only the Link module is not a part of the core fields. Enable the modules as shown. Once you have enabled the modules, navigate to Structure | Content types or navigate to admin/structure/types
.

Then, click on the MANAGE FIELDS link to get to the screen shown in the following screenshot:

As shown in the previous screenshot, when adding your new field, you need to enter the following information:
- LABEL, in this example, Link to source
- FIELD TYPE, in this example, Link
Then, depending on the type of data to be stored in the field, you will be presented with some widget options for the form element used to edit the data. While some field types have many different widgets, the Link field only offers one option: Link.
After selecting the appropriate options, click on the Save button to save your field and move on to the configuration screen:

During configuration of the Link field, in most cases, the default settings will work. For the bookmark, we make three changes, marked 1 to 3 in the preceding screenshot.
The first marked option is Required; given that the purpose of this content type is to store bookmarks, the Link field is a required field.
The second marked option Help text. As the name implies, this text can be used to give instructions to the person filling out the form.
The third marked option removes the title from the Link field. Given that the node already has a title, requiring a title for the link as well would be redundant.
Once the settings have been adjusted, click on the Save settings button. This returns you to the Manage fields administrative screen. On this page, you can order your fields by dragging-and-dropping; click on the Save button to record any changes.
Step 3 – assigning taxonomies
Once you have created a node type, you need to decide whether or not you will use taxonomy to organize or categorize the posts made with that content type. For bookmarks, we want users to be able to use tags to categorize their links.
To add a taxonomy, click on Structure | Taxonomy link or navigate to admin/structure/taxonomy
. Click on the Add vocabulary link shown in the following screenshot:

Clicking on the Add Vocabulary link brings you to the screen shown in the following screenshot:

Here you need to provide a vocabulary name and a description. The name should be somewhat intuitive, and as this vocabulary will be used to describe posts, we will call it Keywords
. The description of the vocabulary is more for administrative purposes, as it is not displayed anywhere on the site by default. Click on Save to create the vocabulary.
After saving, you will be redirected to the Taxonomy page. To edit the vocabulary's other options, you must add it to a content type.
Adding a taxonomy to a content type
Navigate to Structure | Content types | MANAGE FIELDS or admin/structure/types/manage/bookmark/fields
to add the Keywords vocabulary to the Bookmark content type:

Add a new field called Keywords
, and select Term reference as the field type. Since we want this to be a vocabulary where the options are not controlled by a site administrator, choose Autocomplete term widget (tagging) as the widget type.

Click on Save to edit the field's options.
On the next screen you will see options for the taxonomy on the Bookmark content type. Most of the default options are fine for our purposes, except for the two numbered fields in the following screenshot:

In the first marked item, you can add an explanatory text for the end user.
In the second marked item, you can set how many terms can be added to the post. Since we don't want to impose a limit on our users, we will select Unlimited.
Once you have adjusted the settings, click on the Save settings button to save your preferences.
Step 4 – assigning permissions
The final step in preparing content types for use on your site is to assign permissions via user roles. To do this click on People | Permissions | Roles link or navigate to admin/people/permissions/roles
.
For this example, we will assign permissions to the teacher role.
To assign rights for teachers, click on the edit permissions link that is on the right-hand side of the entry for teacher, as shown in the following screenshot:

Then, on the Permission page, scroll down to the section titled Node. As pictured in the following screenshot, give the teacher role the rights to administer content, access the content overview page, view published content, create bookmark content, edit own bookmark content, and delete own bookmark content.

Click on the Save permissions button to save your settings.
The result
Click on the Create content link or navigate to node/add
. Click on the Bookmark link, which brings you to node/add/bookmark
.

Enter the required information and then click on the Save button to save your new bookmark.
Creating content types – summary
Creating content types has four steps:
- Create the content type.
- Add fields (optional).
- Assign taxonomy (optional).
- Assign permissions.
These steps will apply to all new content types created on the site. In some cases, new content types will not require additional fields or taxonomy; however, these steps will guide you through the general process of creating new content types.