A Minimal CCK Module in 5 Minutes

Posted Jun 13, 2008 // 4 comments
Irakli:

Content-Construction Kit (CCK) is a standard way, in Drupal, to add properties to the core content item: Node. There are many CCK Modules that provide a large variety of field types from e-mail fields to image and even video ones. Even so, sometimes standard toolset is just not enough and we need to write our own implementation. This short run-through of a tutorial will get you started. For more detailed documentation, you can refer to the offical CCK tutorial. The CCK content type that we are going to implement, in our sample cckmodule module, is as simple as it gets. It’s just a text-field that saves a value to the database. For a field like this, we need to describe two conceptual parts: field itself (abstract representation) and a widget – the visual element or a collection of elements that will be used on the screen to collect data from a user. The first method we implement describes the fields that this module will implement: function cckmodule_field_info() { return array( 'samplefield' => array('label' => 'Numeric Value)); } Next function describes additional information about the field – where will it be saved, what comparison operators can be applied to it when we use it in Views etc. The function has many more operators then we implement here, but we show only the minimal necessary set to get the job done. function cckmodule_field_settings ( $op, $field ) { switch ( $op ) { case 'database columns': $columns = array( //where and how will it be saved? 'value' => array( 'type' => 'int', 'not null' => TRUE, 'default' => 0, 'sortable' => TRUE) //Can be sorted! ); return $columns; break; case 'filters': return array( 'default' => array( 'name' => t('Default'), 'operator' => 'views_handler_operator_gtlt', //Can be compared on gt or lt ), ); } } The following method allows to perform some actions at different points of field’s life-cycle. We don’t need any of them, but still implement the function to satisfy the requirement and to demonstrate the usage. function cckmodule_field($op, &$node, $field, &$items, $teaser, $page) { switch ($op) { case 'load': break; case 'view': break; case 'validate': break; case 'insert': case 'update': break; } } Once we are done with describing the field, we move on to the widget: function cckmodule_widget_info() { return array( 'samplefield_widget' => array( 'label' => 'Samle Field Widget', 'field types' => array('samplefield',), ), ); } The form code for the widget: function cckmodule_widget($op, &$node, $field, &$items) { $label = $field['widget']['label']; $default = isset($field['widget']['default_value'][0]['value']) ? $field['widget']['default_value'][0]['value'] : 0; $type = $field['widget']['type']; $value = $node->$type; switch ($op) { case 'form': $form = array(); $form[$field['field_name']] = array('#tree' => TRUE); /** VISIBLE VERSION - FOR DEBUGGING ONLY $form[$field['field_name']][0]['value'] = array( '#type' => 'textfield', '#title' => $label, '#default_value' => isset($items[0]['value']) ? $items[0]['value'] : '', );**/ /** Invisible Version - Production use **/ $form[$field['field_name']][0]['value'] = array( '#type' => 'value', '#title' => $label, //'#default_value' => isset($items[0]['value']) ? $items[0]['value'] : '', ); return $form; case 'validate': break; case 'process form values': break; } } And last, but not necessarily least, a pair of functions that we can use to properly format our field: function cckmodule_field_formatter_info() { return array( 'default' => array( 'label' => t('Default'), 'field types' => array('samplefield'), ) ); } function cckmodule_field_formatter($field, $item, $formatter, $node) { $text = check_plain($item['value']); return $text; }

About Irakli

Irakli is Director of Product Development at Phase2 Technology. His main responsibility is development of packaged, turn-key solutions using open-source technologies and cutting-edge semantic APIs.

Irakli has been an avid open-source ...

more >

Read Irakli's Blog

Comments

by mlsamueson (not verified) on Tue, 07/01/2008 - 12:00

Drupal version?

Great down to the point article, but it’s not clear if this is for Drupal 5 or 6… or maybe I missed something?

Thanks!

by irakli on Tue, 07/01/2008 - 16:18

It's Drupal 5

Hello Michael,

good point. The code above is for Drupal 5.

CCK for Drupal6 is still in beta and not stable for production use. I have not tested this code against D6, yet, but my guess is that most of the code will probably still work. Code here uses CCK hooks, not the Drupal ones and CCK API in D6 is still pretty much the same, as far as I know.

We will try post a similar write-up, once CCK for D6 finalizes,

by Anonymous (not verified) on Fri, 12/26/2008 - 23:10

Now what...

Hey, i loved your article...

so i created my CCK module, per your tutorial.. and it exists as a field, which i can add to a content type, and place the block!

How do i actually produce content for the block i produced? And how do i add a field (like a textfield) for my CCK module when you edit a page?

-Josh

by Chris Cohen (not verified) on Mon, 10/19/2009 - 10:30

Code formatting

Can you put some formatting on the code please? As it stands, it just displays in a long line with no line breaks and no syntax highlighting.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • Allowed HTML tags: <a> <strong> <code> <p> <img> <ul> <ol> <li> <h2> <h3> <h4> <b> <u> <i>
  • You may insert videos with [video:URL]

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.