CCK Overview and Structure
Overview
CCK is comprised of 2 things:
- fields: These refer to the data that's being stored. For example, Text or Number.
- widgets: These refer to how the input widget an end user uses to enter the data looks. For example, Text field or Check boxes.
Data Model
CCK's data model consists of 3 base tables:
- node_type_content: A table containing the CCK node type defninitions: name, label, description, help, and the label given to the node's Title field.
- node_field: A table containing the various field definitions: field name, type, and various settings.
- node_field_instance: An individual instance of a field applied to a content type, along with its widget settings. For example, field_description in the content_blog type and field_description in the content_product type.
In addition, each new content type creates its own table, called
node_content_X, where
X is the name of the content type. This table contains a node ID (nid) and revision ID (vid) field, along with the values of any extra fields, if the field is specific to that content type.
If, however, a field is shared among more than one content type, a table called
node_data_field_X, where
X is the name of the field, is also created to track the field's settings between node types.
Hooks
Note: More detailed hook information is available from the file
field.php in the CCK module.
Fields
- hook_field_info(): Here you declare the label for your field type(s). This will show up when you click "add field" on a CCK content type.
- hook_field_settings($op, $field): Handles displaying, validating, and saving field settings forms. In addition, manages how they're stored in the database and provides Views integration.
- hook_field($op, &$node, $field, &$node_field, $teaser, $page): Define the behavior of a field type.
- hook_field_formatter_info(): Declare information about a formatter.
- hook_field_formatter($field, $item, $formatter, $node): Prepare an individual item for viewing in a browser.
Widgets
- hook_widget_info(): Here you declare any widgets associated with your field type. These will show up below the fields when you click "add field" on a CCK content type in 4.7.x and 5.x. Fields show up in the first drop-down and widgets in the second drop down when adding content types in 6.x.
- hook_widget_settings($op, $widget): Handle the parameters for a widget.
- hook_widget: Define the behavior of a widget.
Tips
- While developing CCK, you may notice oddities such as settings you made not appearing and other such things. Always remember to clear your cache!
Comments
The multiple values I've found very useful, and I've used it in several places already.
An example in my case is "People attending Events":
Many people can attend an event, and many events can be attended by one person.
In the database, as mentioned before, a new table is created which has foreign keys for both person and event. This means the many-to-many relationship can exist without having to have a 'list' in a field (thus keeping it 3rd Normal Form).
What I'm saying is, the multiple values thing is essential (at least for me :)
Thanks for the documentation. I didn't realise this was here until now - figured it all out from poking around the database...
The effect of the "Multiple values" field setting
The node_data_field_X table is also created when the field is of type "Multiple values". This happens even if the field is not shared among content types.
When the field is multiple-valued, the node_data_field_X table contains an additional column: delta.