Creating Custom CCK Fields

You can create custom CCK fields, widgets, and formatters for any situation, but it can be hard to see how to do it. I finally found time to create an 'Example' module that creates a simple field, formatter, and widget, with lots of embedded documentation about what belongs where. You need to create three files, an .info file, an .install file, and the module itself. The code below creates a very simple textfield, but it can be used as a starting point for any custom module. I'm also attaching a .zip file with the contents of this custom module.

The .info File

; $Id$name = Example fielddescription = Defines an example field type.dependencies[] = contentpackage = CCKcore = 6.x

The .install File

<?php
// $Id$
// Notify CCK when this module is enabled, disabled, installed,
// and uninstalled so CCK can do any necessary preparation or cleanup.
/**
* @file
* Implementation of hook_install().
*/
function example_install() {
drupal_load('module', 'content');
content_notify('install', 'example');
}

/**
* Implementation of hook_uninstall().
*/
function example_uninstall() {
drupal_load('module', 'content');
content_notify('uninstall', 'example');
}

/**
* Implementation of hook_enable().
*
* Notify content module when this module is enabled.
*/
function example_enable() {
drupal_load('module', 'content');
content_notify('enable', 'example');
}

/**
* Implementation of hook_disable().
*
* Notify content module when this module is disabled.
*/

read more