Function to format CCK fields into HTML tables
Sun, 26 Jul, 2009
I'm posting a function I found very useful when theming CCK powered node-type templates. It was written by David Thomas (dekita on #drupal-support) and like many of his ideas, this is a great one:
/**
* return cck fields formatted as table
*/
function phptemplate_content_format_table($node){
// get fields
$fields = content_fields(NULL, $node->type);
$header = array('', '');
$rows = array();
// loop through and check if field is set in node
foreach($fields as $fieldname => $field){
$row = array();
if(! empty($node->{$fieldname}[0]['value'])){
// make row of label and value
$row[] = array('data'=>$field['widget']['label'], 'class'=>'field-label');
// handle multiple values
foreach($node->{$fieldname} as $key => $value){
if(isset($node->{$fieldname}[$key]['view'])){
$row[] = array('data'=>$node->{$fieldname}[$key]['view'], 'class'=>'field-item');
}else{
$row[] = array('data'=>content_format($fieldname, $node->{$fieldname}[$key]), 'class'=>'field-item');
}
}
$rows[] = $row;
}
}
return theme('table', $header, $rows, array('class'=>'cck-table'));
//var_dump($fields);
}
That's all there's to it!
