background image
HomeRecent PostsDrupalSearchTagsRSSContactAboutAccount

Getting the CCK field labels

Eric.London's picture

Hardcoding something in your module or theme can be just as bad as hacking the core. If the Drupal admin interface allows you the change and configure something, your modules and themes must be scalable enough to account for these changes. This code snippet (example) shows you how you can lookup the CCK field labels, so you'll never have to hard code a label in your theme/module again.

<?php
// create a container for the field labels
$fieldLabels = array();

// load node
$node = node_load('MYCCKNODEID');

// loop through node properties
foreach ($node as $k => $v) {
 
// ensure this property is a field
 
if (substr($k,0,6) == 'field_') {
   
// use the CCK function to get the field data for this field
   
$fieldData = content_fields($k, $node->type);

   
// add the label to the array
   
$fieldLabels[$k] = $fieldData['widget']['label'];
  }
}

// for debug, you could now show all the fields with their labels:
echo "<pre>";
print_r($fieldLabels);
echo
"</pre>";
?>

I hope the irony of talking

I hope the irony of talking about hard coding as a sin with a code snippet that uses

// ensure this property is a field
if (substr($k,0,6) == 'field_') {

makes other people chortle as much as I did. Oh, Drupal.

Exactly what I needed

Thanks!

Previous Comment

Hi Eric, I figured out why I was getting that error message. I have the open flash api module installed so it clashed.

Thanks,

Daimian

Awesome

Thanks! Kind of funny that it takes that much effort, but hey...