Drupal 6 Content Construction Kit (CCK)

Exclusive offer: get 50% off this eBook here
Building Powerful and Robust Websites with Drupal 6

Building Powerful and Robust Websites with Drupal 6 — Save 50%

Build your own professional blog, forum, portal or community website with Drupal 6

$26.99    $13.50
by David Mercer | February 2009 | Content Management Drupal Open Source

It is likely that at some stage, you will want to upgrade at least some content from plain text to something that looks a little out of the ordinary. In this article by David Mercer, we will have a look at the CCK module provided by Drupal which is used to build custom content types that can be tailored to suit your needs. In effect, it gives you control over which fields are presented to a user whenever they post content using custom content types. The term field refers to a given piece of content within a node. Conversely, a node is a collection of fields.

In addition to the basic field types provided by the CCK module, you should also keep an eye out for contribs that extend CCK functionality to provide a huge range of useful field enhancements. Everything from Brazilian ID numbers to validated email fields, voting widgets and Amazon ASINS have been made available in the past.

Views module provide administrators with the means to modify how Drupal displays lists of content, and CCK exposes its fields to the Views module, making them perfect partners when it comes to creating custom content and then displaying that content in a highly configurable manner. At the time of writing, Views is not available for Drupal 6 (although the module is being actively developed and should hopefully be ready by the time you read this) so it is left as an exercise to download and install it, and create at least one new View, utilizing fields created in the following sections.

Installing CCK

CCK is available, so go ahead and download the latest version and extract the file to your modules folder. CCK adds its own section to the Modules page under Site building:

Building Powerful and Robust Websites with Drupal 6

There are a number of interdependent sections for this module, but all of them rely on the first option, Content, so go ahead and enable this first. We are going to look over all the features provided by CCK, by default, in this section. So go ahead and enable those modules that rely only on Content. With that done, enable the remaining options so that you end up with everything working, like this:

Building Powerful and Robust Websites with Drupal 6

Notice that some of the options are disabled to prevent us from inadvertently disabling an option that is required by something else. If, for example, you wish to disable Text, then disable Node Reference and User Reference first.

Working with CCK

With all the options enabled, we can now go ahead and create a new content type. Actually, it is possible to create new content types without the use of CCK, it's just that the new content types will look pretty much like the standard content types already available, because there are no really interesting fields to add.

Head over to Content types under Content management and select the Add content type field to bring up the following page:

Building Powerful and Robust Websites with Drupal 6

The identification section is pretty straightforward. You can go ahead and fill in whatever new content settings are appropriate. Of special interest is the Submission form settings below this that allows you to decide whether the default Title and Body fields should be changed or even retained (in the case of the Body field):

Building Powerful and Robust Websites with Drupal 6

In the case of the Endangered Species content type, it doesn't really make sense to have a species Title, rather a Common name makes more sense. Leaving the Body field label blank will cause this field to be omitted completely in the event that it is not suitable for the type of content you have in mind.

You may have noticed that there are several additional tabs to the right of Add content type tab that provide additional functionality. These options are discussed a little later on in this section. So for now, go ahead and fill out the Name, Type, and Description fields and click Save content type to add this to the default list:

Building Powerful and Robust Websites with Drupal 6

We are now ready to begin customizing this new type utilizing whatever options are available—depending on what is or is not enabled. It is possible to customize any type that is available on Drupal, including the default ones like Blog entry or Poll, but to begin with it is best to leave these alone.

To begin working on the new content type, click on edit in the Endangered Species row. We can now look at the various aspects of working with content types, beginning with…

Adding Fields

Select the Add field tab to bring up the following page:

Building Powerful and Robust Websites with Drupal 6

This dialog allows you to specify the new field's machine readable name and then select what type of input it is going to be.

Presently, only the Create new field section is displayed on this page, because we have yet to add new fields. Once there is at least one custom field available, this page will have an additional section allowing existing fields to be added directly from any content type (you can come back here once there are a few saved fields):

Building Powerful and Robust Websites with Drupal 6

Regardless, the Create new field list presently comprises of the following options:

  • Node Reference – Allows the poster to reference another node using its ID value
  • Integer, Decimal, Float – Allows posters to store numbers in various formats
  • Text – Allows posters to enter content
  • User Reference – Allows posters to reference other users.

Remember that this list is subject to change, depending on whether you disable various components of the default package, for example, Node Reference or User Reference, or include additional modules that add field types such as Date or Fivestar.

Each value type comes with a set of options for how that data should be entered. Looking at the Integer type, we can see that users can be prompted for an integer with a Text Field, Select list, Check boxes, and radio buttons—in this case, the Select list is going to be used.

Be careful about how information is stored—it is important to be efficient. For example, don't store information as text when there is only a certain number of options available, instead, store them as a number and provide the right input type to display the various options appropriately.

To demonstrate this point, consider that at the moment, the numbers_in_wild field is set as an integer with the Select list input type. We are not going to provide a select list of every possible integer, but we are going to represent a range of numbers with an integer. For example, the value 1 will correspond to the range 1-10, 2 will correspond to 11-100, and so on.

With the new field created, the configuration page for this field (Click on configure in the Operations column of the Manage fields page) now displays the current settings available. To begin with, the options in the Endangered Species settings are not of much interest as we have not specified what data this field will hold. To do this, scroll down the page to the Global settings section. From here, you can decide on how the data will be presented to the user and whether or not the field itself will be compulsory or not:

Along with the Allowed values list used to input key-value pairs, there are a few other settings that may be of use, depending on what data the field should capture. Minimum and Maximum values along with Suffix and Prefix values allow for some minor input validation, as well as some useful display properties like currency denominations or physical units of measurement.

Building Powerful and Robust Websites with Drupal 6 Build your own professional blog, forum, portal or community website with Drupal 6
Published: April 2008
eBook Price: $26.99
Book Price: $44.99
See more
Select your format and quantity:

The Global settings section is type-specific. If, for example, you wanted to save a field in text format, you will find that the following options are presented instead of the Prefix and Suffix options:

In our case, there is a specified list of eight values that denote the range of animal numbers in the wild. Clicking on Save field settings should now display the field type in a drag-and-drop representation of the content type, as it will appear when users attempt to use it:

Building Powerful and Robust Websites with Drupal 6

It's easy enough to move things around to place them exactly as required on the page—although you must remember to click Save to implement the changes.

To get back to the field's configuration page, click on configure. Now we can implement some of the Endangered Species settings that were skipped the first time round.

Building Powerful and Robust Websites with Drupal 6

In this case, an informative, human readable name has been provided along with a default value for the select list. Initially, no default value options could be presented because there were none specified in the Global settings section. At present, we have no groups so the Display in group option is left as is. It is possible to use PHP to determine what the default value should be, by clicking on the Php code link and entering it into the space provided—unless you have an exceedingly pressing reason to use this, stay clear.

Go ahead and save the changes by clicking on Save field settings, and head on over to the Create content section and create a new Endangered Species post:

We now have, in addition to the default fields provided, a perfectly good method of specifying the number of animals in the wild by selecting the appropriate values from the select list. Furthermore, the data stored in the database is only an integer and not the entire range; so, this is a pretty efficient way of doing things.

Be aware that representing information with an integer, while efficient, means that if you do decide to change your data, then all legacy content will reflect these changes (because the data stored is only a representation and not the actual text information being displayed).

What if, in addition to the estimated numbers of the species, a field for entering the scientific name for the animal in genus species format is required. It may also be nice to have the number of breeding pairs in captivity, the total number of captive animals, the number of wild breeding pairs, and any other statistical information people might want about the species.

The species name is not really a statistic, so ideally, it would be presented separately from the stats about the animal. CCK provides a way on intuitively grouping information like this through the use of groups…

Adding Groups

To create a group, click on the Add Group tab and fill out the form according to how it should be presented—options that are required should always be open, but it may be better to present less important options in collapsed form, so that users can quickly scroll over them if they are not going to fill them out:

This screenshot shows the Statistics group that will hold the vital statistics of a given endangered species. Once you have filled out the form, click Add to include it in the content type. The group will now be present on the drag-and-drop list of the Manage fields tab, and is presented with configure and remove options, as shown here:

With the group added to the content type, we can now go ahead and create the other fields that will constitute the input data for this group. This includes Estimated Numbers in the Wild, so click on its configure link and modify the Display in group section of the Endangered Species settings like so:

Click Save field settings, and the field is now contained within the Statistics group:

After creating a few more fields, including the Species name, and adding them to the Statistics group, we now have something like this:

You can re-order fields within a group, in the same way fields and groups are re-ordered within the content type. Once everything is in the right place, go back to the Create content and look at the new post page:

The Species (anyone know what Acinonyx means?) field is presented above the Statistics group with each field in the correct order, as specified in the Manage fields page. So the Manage fields page allows us to control how the fields are presented on the content creation page, but this still leaves us with the task of presenting the captured data correctly in its various forms—be it in search results, teasers or full pages.

Displaying Fields

In order to determine how input data should be displayed on screen, click on the Display fields tab next to Manage fields, when editing a content type. This brings up two tabs that cater for General and Advanced settings, respectively:

The General tab allows the Teaser and Full node fields and their labels to be displayed according to the settings made here. In the same way, the Advanced tab deals with the display of this content type in RSS items, the search index, and search results. Each field and group has a Label option to determine where the label is displayed, relative to the data it contains. For fields, the three options are Above, Inline or Hidden. Groups only have two options, Above and HiddenInline would not make much sense for a group.

Setting the Species Label to Above, as shown in the previous screenshot, displays its field content like this:

Setting it to Inline gives:

And finally, setting it to Hidden gives:

As an exercise, try out the different options available for each of the fields (and group) in the Full node section. For example, changing the option to Hidden here, produces this result:

It's important to realize that different fields have different options, depending on what underlying type they are. An example of this is the Node reference type that can display the Title(Link), Title(no link), Full node, Teaser, and Hidden. As mentioned earlier, the Node reference type allows posters to reference another node, and in this case, I added a Similar Animals field to the Endangered Species content type.

Building Powerful and Robust Websites with Drupal 6 Build your own professional blog, forum, portal or community website with Drupal 6
Published: April 2008
eBook Price: $26.99
Book Price: $44.99
See more
Select your format and quantity:

Selecting the Title(Link) option in the Full node column for this type and ensuring that a few nodes have been referenced in a new post, like so (incidentally, this Node reference field was created with the Autocomplete option that allows posters to begin typing in the name of a post and select which of the available options are presented in the drop-down list—the actual node number is automatically added when a choice is made):

...displays the following when the content is viewed:

As requested, the user is presented with the titles of two other Endangered Species posts in the form of links that will take the user to their content. This is a really nice feature for providing connected and relevant content. Just as easily, we could have specified Full node for the Similar Animals Node reference field in the Full node column:

...in which case, the entire nodes would be displayed instead of the title links in any content that referenced them:

How you choose to present content is really up to your own preferences and what is most expedient and useful for users in the context of how they utilize your site. It is left as an exercise to go through the settings available under the Advanced tab and to make use of all the default field types in the process. Remember that you need to run the cron script before any new content will show up in search results (just in case you want to play around with the Search Result column and view the results).

Export & Import

Earlier in this section, we saw that there were additional tabs added to the Content type page under Content management. The first one, Fields, provides a list of all the fields added to the various content types and doesn't warrant much explanation here. The other two, Export and Import provide a powerful facility to effectively copy and paste content types, whole, or piece by piece.

For argument's sake, let's assume that the Endangered Species content type is close but not quite the same as an Extinct Species content type that is going to be added. Instead of recreating the new content type from scratch, we can duplicate the Endangered Species type and modify it during the Export process, before importing it as the new type.

Click on the Export tab and select the Endangered Species content type, and click Submit. This brings up a list of field and group definitions that should be included in the export:

For somewhat macabre reasons, we no longer need to include any of the fields previously held in the Statistics group. Although, the Statistics group itself can be used to house other stats about the extinct species. Accordingly, only the Species and Similar Animals fields need to be exported to the new content type.

Clicking on Submit, brings up the Export data text area, containing the information that will be used by the Import feature to build the new content type. Copy this code and then click on the Import tab and paste it into the space provided:

In this instance, the Content type is left as the default option, <Create>, because we do not want to import these group and field definitions into any of the current content types. If you did want to add a field or two into one of the existing content types, you would select that Content type from the drop-down list and add the new field definition (obtained from your own site or someone else's site—wherever) into the Import data section.

The export definition contains the content type value. In this case, we can't directly import the new definition because the species content type already exists, so Drupal would report a failure to import the type because is already there. To get around this, edit the first few values of the Import data as follows:

 $content[type] = array (
'name' => 'Extinct Species',
'type' => 'extinct',
'description' => '',
'title_label' => 'Common name',
'body_label' => '',

Now we are effectively importing a completely new type of content (called extinct) that happens to be based on the existing species content type. Clicking Submit will add the new type to your list and you can now view it as expected.

You might find that there are field definitions in other content types (yours or someone else's) that could be imported instead of being recreated when modifying content types. In this case, go through the process again, selecting the relevant content type to export from, and then, choose the field to export, copy the export code across to the Import section, and add the field to the relevant content type.

Adding Contributed Fields

One of the great things about CCK is that anyone can build and add custom field types that can be incorporated into your own content types. Let's take a quick look at an example of this. Head on over to the Drupal site and download both the Voting API and Fivestar modules—the Fivestar module provides a custom field type that we can use to add rating facilities to content, but it requires the Voting API module to be enabled first.

Go ahead and extract both modules ensuring to first enable Voting API and then Fivestar. With that done, head on over to the Content types page under Content management and click edit for the content type that you would like users to be able to rate—for argument's sake, I will go with Endangered species. Click on the Add field tab, enter a field name (something like field_rating), and scroll down the page till you get to the Fivestar Rating option:

Choose an option and click Create field to add it to the content type, and then, ensure that you set the configuration options to something appropriate for you—each option is fairly well explained. Bear in mind that the Node ID field is the most important because it determines exactly, the node that receives the value of the rating. In this case, it's time to dust off your PHP skills and use it to dynamically select the appropriate node ID—admittedly, this isn't ideal but it does serve to illustrate the process for adding a contributed field, regardless of what it is.

As an addendum, Fivestar actually provides a nice rating widget that can be enabled for a content type—on it's Edit page, in the list of settings categories, towards the bottom of the page:

While not shown in the previous screenshot, there is also a Fivestar comment rating widget that can disabled, optional or required. Like the Direct rating widget, there is a preview available on the right-hand side of the page. With your choices made, click Save settings and then go post a piece of content for the type that now has the Fivestar rating enabled.

For example, a post of the Endangered species type, with Fivestar enabled, looks like this:

Whenever a user makes a vote, the widget registers it for that specific node. How these votes are tallied, displayed, and so on, can all be configured from the Fivestar and Voting API pages under Site configuration.

Don't be surprised if you click the widget only to find the vote is not registered immediately. It is possible to set the widget to update straight away, but more than likely you can leave this to update with the cron run using the Voting API section in Site configuration. By playing around with Fivestar settings, the rating widget can end up looking like this:

In this case, Fivestar actually provides dual functionality, in that it is possible to provide a voting widget for users to rate a given piece of content, as well as a contributed field type that is utilized by posters of content—this could be useful if, for example, a danger level rating should be supplied with each Endangered species post.

Of course, each contributed field will come with its own entourage of settings and configuration parameters, so be careful to ensure that you are entirely clear on how the contrib functions work before making it available to the site's content posters.

Summary

CCK is another powerful feature of Drupal that can provide a vast array of new and interesting content types to cater for virtually any type of content. As time goes by, more and more CCK related contributions will become available, and you are urged to keep checking the available modules. Unfortunately, at the time of writing, the Views contrib was not available to work in conjunction with CCK but please take the time to check it out when it does arrive.

About the Author :


David Mercer

David Mercer was born in August 1976 in Harare, Zimbabwe. Having always had a strong interest in science, David came into regular contact with computers at university where he graduated "cum laude" with majors in applied math and math.

His technical books are now sold worldwide and have been translated into French, German, Polish, Greek, Spanish and many more. His book on Drupal 6 was reviewed on Slashdot and went on to become a best seller. Mercer's books are recommended reading at higher learning institutes like MIT, and he has a write-up on Wikipedia.

David divides his time between consulting for companies and organizations in a wide variety of industries, helping them to develop and implement cutting edge systems. He also contributes to interesting web-based projects like design-a-webpage (http://www.design-a-webpage.com) that allows people to create beautiful, effective landing pages in minutes - absolutely no code or Web experience required.

He also maintains a blog, training and support site for his readers at http://www:siteprebuilder.com. Site prebuilder provides quizzes and exercises that accompany his books, and serves as his primary online presence and contact point.

When he isn't working (which isn't that often) he enjoys playing guitar (generally on stage and unrehearsed) and getting involved in outdoor activities ranging from touch rugby and golf to water skiing and snowboarding.

Books From Packt

Drupal 6 Social Networking
Drupal 6 Social Networking

Drupal 6 Themes
Drupal 6 Themes

Building Websites with Joomla! 1.5
Building Websites with Joomla! 1.5

Joomla! Web Security
Joomla! Web Security

WordPress Plugin Development (Beginner's Guide)
WordPress Plugin Development (Beginner's Guide)

MODx Web Development
MODx Web Development

Drupal 6 Site Builder Solutions
Drupal 6 Site Builder Solutions

Learning Drupal 6 Module Development
Learning Drupal 6 Module Development

Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software