Theme a multiple CCK field with a table

Sometimes CCK contrib modules cannot do exactly what you want. It’s time to build your custom CCK field!

Official documentation on CCK fields creation for Drupal 6 is incomplete and some passages are obscure. If there is a good howto you have to read before do any CCK customization, this is Creating Custom CCK Fields. This howto supposes you’ve read and understand it before continue. If you want to create a custom field, you can read the complete Creating a Compound field. A custom multiple compound field (with more than one field for element, e.g. an image and its description).

Read these howto well, you’ll spare time later

Well, you have followed the howto, you have your own compound field but now you have a problem. You want to display compound field data as cells in a table, and each field as row.

On following example, we have a name list made with a multiple compound field with “firstname” and “lastname” columns.

KarenS tell you that you’ve to use CONTENT_HANDLE_MODULE instead of CONTENT_HANDLE_CORE on hook_formatter_info() .

// The machine name of the formatter.
function my_funny_module_field_formatter_info() {
  return array(
    'default' => array(
      'label' => t('Default'),
      // An array of the field types this formatter
      // can be used on.
      'field types' => array('examplefield'),
      // CONTENT_HANDLE_CORE:   CCK will pass the formatter
      // a single value.
      // CONTENT_HANDLE_MODULE: CCK will pass the formatter
      // an array of all the values. None of CCK's core
      // formatters use multiple values, that is an option
      // available to other modules that want it.
      'multiple values' => CONTENT_HANDLE_MODULE,

/** Set the formatter **/
function my_funny_module_theme() {
  return array(
    'my_funny_module_formatter_default' => array(
      'arguments' => array('element' => NULL),
      'function' => 'funny_display_table',

/** Here you format your table data as array **/
function my_funny_module_formatter_default($element) {
  $data = array(
  return $data;

/** This function will display a table even where data array is empty:
 ** You have to put an additional control statement to avoid this.
 ** $element will have $data from formatter_default() above
function my_funny_module_display_table($element) {
  $header = array(
    t('First name'),
    t('Last name'),
  $i = 0;
  while (!$end) {
    /** Any row will contains **/
    if(array_key_exists($i, $element)) {
      $rows[] = array(
        'firstname' => $element[$i]['#item']['firstname'],
        'lastname' =>$element[$i]['#item']['lastname'],
    else {
      $end = TRUE;
  /** Theme a table with data from element and header **/
  return theme('table', $header, $rows);

Note: to format a table you have to change only “multiple values” on my_funny_module_field_formatter_info(): you can leave my_funny_module_widget_info() as is.

See also:

6 responses to “Theme a multiple CCK field with a table”

  1. This is really helpful. To finish it off, perhaps you could help with making this table sortable.

    I have tried, but whilst the headers change the data doesn’t!

  2. I’ve never tried it before, but it seems that headers on theme_table require an additional array element for sorting.

    $header = array(
    ‘data’ => t(‘First name’),
    ‘field’ => ‘fieldname_on_db’,
    ‘sort’ => ‘asc’,

    Maybe fieldname_on_db must be sortable on your implementation of hook_field_settings() (on $op == ‘database columns’).

    Try to take a look on poplar and lullabot howtos I’ve cited before and to theme_table function on Drupal API.

    Thanks for visiting and please post the solution if you find it!

  3. shouldn’t ‘function’ => ‘funny_display_table’,
    in function my_funny_module_theme() be the same as function my_funny_module_display_table() ??

    i haven’t tested either way..

  4. I do this with Views Attach, which may be insane, but I like the configurability.

    So what I do is create a view, and add a display of type “Node content” (that display option is only available when you have Views Attach module enabled). Then I add the current node nid as an argument, add the fields I want to show and then, of course, I set the style in “Basic settings” to “Table”.

    This was particularly useful to neatly display information stored in a CCK Multigroup.

%d bloggers like this: