Our Target Module: What We Want
Before we begin developing a module, here’s a brief overview of what we want to accomplish.
The module we will write in this article is the Philosophy Quotes module (philquotes will be our machine-readable name). The goal of this module will be to create a block that displays pithy philosophical quotes.
We will implement the following features:
- Quotes should be stored along with other basic content, making it possible to add, modify, and delete this content in exactly the same way that we create other articles.
- Since our existing themes aren’t aware of this quotes module, it must provide some default styling.
We will progress through the creation of this module by first generating a new “quote” content type, and then building a theme-aware module.
Creating a Custom Content Type
As Drupal evolved, it incorporated an increasingly sophisticated method for defining content. Central to this system is the idea of the content type. A content type is a definition, stored in Drupal’s database, of how a particular class of content should be displayed and what functionality it ought to support.
Out of the box, Drupal has two defined content types: Page and Story. Pages are intended to contain content that is static, like an “About Us” or “Contact Us” page. Stories, on the other hand, are intended to contain more transient content—news items, blog postings, and so on.
Creating new pages or stories is as simple as clicking on the Create Content link in the default menu.
Obviously, not all content will be classified as either a page or a story, and many sites will need specialized content types to adequately represent a specific class of content. Descriptions of events, products, component descriptions, and so on might all be better accomplished with specialized content types.
Our module is going to display brief quotes. These quotes shouldn’t be treated like either articles or pages. For example, we wouldn’t want a new quote to be displayed along with site news in the center column of our front page.
Thus, our quotes module needs a custom content type. This content type will be very simple. It will have two parts: the text of the quote and the origin of the quote.
For example, here’s a famous quote:
The life of man [is] solitary, poor, nasty, brutish, and short.—Thomas Hobbes.
The text of this quote is “The life of man [is] solitary, poor, nasty, brutish, and short”, and the origin in this example is Thomas Hobbes. We could have been more specific and included the title of the work (Leviathan) or even the exact page reference, edition, and so on. But all this information, in our simple example, would be treated as the quote’s origin.
Given the simplicity of our content type, we can simply use the built-in Drupal content type tool to create the new type.
To generate even more sophisticated content types, we could install the CCK (Content Creation Kit) module, and perhaps some of the CCK extension modules. CCK provides a robust set of tools for defining custom fields, data types, and features.
But here our needs are simple, so we won’t need any additional modules or even any custom code to create this new content type.
Using the Administration Interface to Create a Content Type
The process of creating our custom content type is as simple as logging into Drupal and filling out a form.
The content type tool is in Administer | Content management | Content types. There are a couple of tabs at the top of the page:
Clicking the Add content type tab will load the form used to create our new content type.
On this form, we need to complete the Name and Type fields—the first with a human-friendly name, and the second with a computer-readable name. Description is often helpful.
In addition to these fields, there are a few other form fields under the Submission form settings and Workflow settings that we need to change.
In the Submission form settings section, we will change the labels to match the terminology we have been using. Instead of Title and Body, our sections will be Origin and Text.
Changing labels is a superficial change. While it changes the text that is displayed to users, the underlying data model will still refer to these fields as title and body. We will see this later in the article.
In the Workflow settings section, we need to make sure that only Published is checked. By default, Promoted to front page is selected. That should be disabled unless you want new quotes to show up as content in the main section of the front page.
Once the form is complete, pressing the Save content type button will create the new content type.
That’s all there is to it. The Create content menu should now have the option of creating a new quote:
As we continue, we will create a module that displays content of type quote in a block.
Before moving on, we want a few pieces of content. Otherwise, our module would have no data to display.
Here’s the list of quotes (as displayed on Administer | Content management | Content) that will constitute our pool of quotations for our module.