💾 Archived View for chirale.org › 2009-08-06_365.gmi captured on 2024-05-12 at 15:25:26. Gemini links have been rewritten to link to archived content

View Raw

More Information

-=-=-=-=-=-=-

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).

Creating Custom CCK Fields

Creating a Compound field

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, ), ); } /bin /boot /dev /etc /home /init /lib /lib32 /lib64 /libx32 /lost+found /media /mnt /opt /proc /root /run /sbin /srv /sys /tmp /usr /var Set the formatter **/ function my_funny_module_theme() { return array( 'my_funny_module_formatter_default' => array( 'arguments' => array('element' => NULL), 'function' => 'funny_display_table', ), ); } /bin /boot /dev /etc /home /init /lib /lib32 /lib64 /libx32 /lost+found /media /mnt /opt /proc /root /run /sbin /srv /sys /tmp /usr /var Here you format your table data as array **/ function my_funny_module_formatter_default($element) { $data = array( $element['#item']['firstname'], $element['#item']['lastname'], ); return $data; } /bin /boot /dev /etc /home /init /lib /lib32 /lib64 /libx32 /lost+found /media /mnt /opt /proc /root /run /sbin /srv /sys /tmp /usr /var This function will display a table even where data array is empty: all.sh django2gmi.sh processing README.md wp2gmi.sh You have to put an additional control statement to avoid this. all.sh django2gmi.sh processing README.md wp2gmi.sh $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) { /bin /boot /dev /etc /home /init /lib /lib32 /lib64 /libx32 /lost+found /media /mnt /opt /proc /root /run /sbin /srv /sys /tmp /usr /var Any row will contains **/ if(array_key_exists($i, $element)) { $rows[] = array( 'firstname' => $element[$i]['#item']['firstname'], 'lastname' =>$element[$i]['#item']['lastname'], ); $i++; } else { $end = TRUE; } } /bin /boot /dev /etc /home /init /lib /lib32 /lib64 /libx32 /lost+found /media /mnt /opt /proc /root /run /sbin /srv /sys /tmp /usr /var 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:

https://web.archive.org/web/20090806000000*/http://www.lullabot.com/articles/creating-custom-cck-fields

https://web.archive.org/web/20090806000000*/http://www.poplarware.com/cckfieldmodule.html

https://web.archive.org/web/20090806000000*/http://www.lullabot.com/articles/creating-custom-cck-fields

https://web.archive.org/web/20090806000000*/http://www.poplarware.com/cckfieldmodule.html

https://web.archive.org/web/20090806000000*/http://api.drupal.org/api/function/theme_table/6