
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.
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 privileges to the content type.
Of these four steps, only steps one and four need to happen for all 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
Click the Administer | Content management | Content types link, or navigate to admin/content/types
. Click the Add content type link, or the Add a new content type link, as shown in the following screenshot:

This brings you to the administrative screen to add a content type, accessible at admin/content/types/add
. As shown in the following screenshot, this screen has four sections:
- Identification
- Submission form settings
- Workflow settings
- Comment settings
As seen in the preceding screenshot, the Identification section contains three fields:
- The Name 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.
- The Type field holds the machine-readable name of the node type. Values entered into the Type field can only contain alphanumeric characters (a-z and 0-9) and underscores. Generally, the Type should relate to the Name; in this example, the Type is bookmark.
- The Description field holds a more detailed description of what the content type is used for. The description 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.
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 submission form settings screenshot just above 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 web site.
You can view the different fields on the Workflow settings page as shown in the following screenshot:

The Workflow settings cover the basic rules for what happens to a piece of content when it is created and edited. Under the Default options, 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 privileges can view and revert 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 Attachments settings allow you to specify whether users can attach files to content. In most cases, it makes sense to allow attachments. In this example, however, as we are saving bookmarks, attached files are unnecessary.
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. If you want to allow comments, set this to Read/Write. If you don't want to allow comments on this node type, select Disabled.
The second most important setting is Anonymous commenting (which is grayed out in the preceding screenshot, as anonymous users have not been given the right to comment). If you allow anonymous users to comment, you should require them to leave their contact information, and use one of the spam control modules. The Recaptcha module (http://drupal.org/project/recaptcha) is a decent first line of defense against spammers; if you have ongoing issues with spammers getting past recaptcha, Mollom (http://drupal.org/project/mollom) provides superb spam control.
The third 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.
Once you have set the defaults, click the Save Content Type button to create your new content type.
Step 2: Adding Fields
Once the content type has been created, we need to add fields. To add fields to content types, we need to install the Content Construction Kit, or CCK. Additionally, because we are creating a bookmark and need to store a link, we need to install the Link module.
CCK can be downloaded from http://drupal.org/project/cck, while the Link module can be downloaded from http://drupal.org/project/link.
Once you have downloaded and extracted the modules, upload them into sites/all/modules
as described earlier in this chapter, and then click the Administer | Site Building | Modules link, or navigate to admin/build/modules
to enable them.
Note
CCK allows you to add fields to content types, and the project download includes several sub-modules. In this section, we will enable the various CCK-related modules we will use throughout this book. In later chapters, we will install additional modules that will further extend the functionality of CCK.

Of all the modules shown in the preceding screenshot, only the Link module is not a part of the main CCK download.
Enable the modules as shown. Once you have enabled the modules, navigate to Administer | Content Management | Content Types link, or navigate to admin/content/types
.

Click the Manage fields link to get to the screen shown in the following screenshot:

As shown in the above screenshot, when adding your new field, you need to enter the following information:
- A Label; in this example, Link to source
- A Field name; in this example, link
- A Type of data; 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: Text fields for Title and URL.
After selecting the appropriate options, click the Save button to save your field and move on to the configuration screen.

In configuring 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.
Item 1 is the Help text. As the name implies, this text can be used to give instructions to the person filling out the form.
Item 2 makes the field Required; given that the purpose of this content type is to store bookmarks, the Link field is a required field.
Item 3 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 the Save field settings button.
This returns you to the Manage fields administrative screen. On this page, you can order your fields using drag and drop; click 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 the Administer | Content Management | Taxonomy link, or navigate to admin/content/taxonomy
. Click the Add vocabulary link shown in the following screenshot:

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

In the Identification section, you need to provide a Vocabulary name, a Description, and some Help text. 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. The help text is explanatory text for the end user.
In this section, you select what node types will be categorized by this taxonomy. In this example, we will select Bookmarks; as we progress through the book we will add several other content types into this list.
The Tags setting determines whether the list of terms will be added by individual users (known as freetagging), or whether the list of terms will be determined by a site administrator (a controlled vocabulary). Selecting Tags means that this will be a freetag vocabulary.
Multiple Select means that more than one term can be applied to a post. While this is always true for freetag vocabularies, it becomes more relevant for controlled vocabularies.
Required determines whether or not a user needs to select a term.
Once you have adjusted the settings, click the Save button to save your preferences.
Step 4: Assigning Privileges
The final step in preparing content types for use on your site is to assign privileges via user roles. To do this click the Administer | User Management | Roles link, or navigate to admin/user/roles
.
For this example, we will assign permissions to the teacher role.
To assign rights for teachers, click the edit permissions link to the right of the entry for teacher as shown in the following screenshot:

Then, on the Permission page, scroll down to the section titled node module. As pictured in the following screenshot, give the teacher role the rights to create bookmark content, delete own bookmark content, and edit own bookmark content.

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

Enter the required information, and then click the Save button to save your new bookmark.
Creating Content Types: A 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.