A Minimal CCK Module in 5 Minutes

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.

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; }

Irakli Nadareishvili