CCK is an extremely flexible framework for creating forms to enter content. Although the Drupal core provides the ability to create different content types (such as a Job Opening or Application), it does not yet provide a mechanism for adding fields to the newly created types. Until CCK fields become integrated into Drupal core (which is expected in future releases), installing CCK should be the first step in nearly any Drupal website.
Upon installing a new copy of Drupal, there are two content types provided: Story and Page. Both are fundamentally the same thing—a form that contains a Title field and a Body field. Any additional content types that are created will also contain a Title field and (optionally) a Body field; it takes an add-on module like CCK to add additional fields to a content type. Figure 3.2 depicts the Story content type as it appears prior to adding fields with CCK.
After enabling the CCK module, any number of custom fields may be added to any content type. Combined with Drupal core’s ability to create any number of custom content types, CCK lets you create any number of completely customized forms for adding content. Figure 3.3 shows the same form after adding a few custom fields, such as an additional text field, an image field, and a set of radio buttons.
After adding the fields, CCK can automatically handle saving information to the database, and after data is submitted, presenting this information in a variety of ways.
Functionally, CCK is set up into two end-user pieces. The first of these are fields, which allow a user to save data into your site. Fields represent the type of data that needs to be saved, such as integer, decimal, or text. When choosing a field to add to a content type, the first decision you need to make is what kind of data is being stored “behind the scenes” in the form. Will the information entered into the form be something basic like text or numbers, or something more special like a relationship to another node or user? The field types included in CCK “core” are displayed in Table 3.1. Other modules, such as Fivestar (http://drupal.org/project/fivestar), ImageField (http://drupal.org/project/imagefield), and Date (http://drupal.org/project/date), add more field types to CCK. These CCK field modules are covered later in the book, in Chapters 4, 7, and 9, respectively. A full list of available CCK field types is available at http://drupal.org/taxonomy/term/88.
Table 3.1. Built-in CCK field types
Field type | Common uses |
|---|---|
Integer | The most efficient way of storing a number. Use for product numbers, identifiers, or whenever you’ll have an exact number of something, like track numbers on an album or number of attendees at an event. |
Decimal | An efficient way of storing numbers to a certain decimal point. Useful for currency amounts. |
Float | The most accurate way of storing numbers that need a high level of precision, such as scientific measurements. |
Text | Can store any string of text. Useful for names and descriptions, and also for longer full-text content such as biographies. |
Node Reference | Can reference any node on the site in a field. Useful when one piece of content is related to another piece of content. |
User Reference | Can reference any user on the site in a field. Useful when associating a user with a certain piece of content, such as a coordinator or contact person for an event. |
Once the type of data is determined, then it’s time to think about how it should look in the form. In CCK lingo, the form elements are called widgets. Do you want a drop-down select list, or a group of radio buttons? Checkboxes or an autocomplete text field? Choose the widget that makes the most sense for the user entering the data. Note that the widgets available will vary based on the field type chosen.
The widget types included in CCK core are displayed in Table 3.2. As with fields, add-on modules often expose additional widget choices.
Table 3.2. Built-in CCK widget types
Complementing the configuration of field input, formatters allow you to adjust how the data will be output when it is displayed to the users of your site. For example, decimals could be displayed with or without commas to indicate thousands, such as in Figure 3.4,
Other modules may add additional formatters, giving you a plethora of ways of displaying information. The ImageCache module is an example of such a module; it allows the display of resized images.
Keep in mind that the formatters available depend on the type of the data, making it very important to set up a field as an integer, decimal, or float if you’ll be displaying numbers. CCK won’t let you change the data type after the field has been set up, so if you need to change the type of field from text to integer (or any other conversion), you’ll need to delete the field and add a new one with the desired type.
With the recipes in this book, you can take full advantage of the vast collection of community-contributed modules that make the Drupal web framework useful and unique. You'll get the information you need about how to combine modules in interesting ways (with a minimum of code-wrangling) to develop a variety of community-driven websites -- including a wiki, publishing workflow site, photo gallery, product review site, online store, user group site, and more.




Help







