Can anyone advise me on how to change a cck field's label via the preprocess_node function? I'm trying to change the label for one field based on the values of some other fields.

In my theme's template.php theme_preprocess_node function I've tried something like the following (based on a search through print_r($vars);):

if ($vars['template_files'][0] == 'node-inventory') {
  $vars["node"]->content["field_inventory_price"]["field"]["#title"] = "Test Price:";
}

It looks like this field gets changed back after this, or the theming and conversion to html for this field happens before the preprocess_node function hits it. Is there another place I should be setting this?

Comments

markus_petrux’s picture

Status: Active » Fixed

If you implement MODULE_preprocess_node(), changes are available to node.tpl.php, but that takes effect only if you plan to render CCK fields in your node templates manually.

On the other hand, you could implement MODULE_preprocess_content_field(), and that should affect content-field.tpl.php and variants, which is different than node.tpl.php.

jacobkdavis’s picture

MODULE_preprocess_content_field() is exactly what I was looking for, thanks!

For posterity, in case anyone else is interested in the future, the code to change a field's label (similar to the non-working code above) would be:

function mymodule_preprocess_content_field(&$vars) {
  if ($vars['field']['field_name'] == "field_inventory_price") {
    $vars['label'] = "Test Price";
  }
}

Status: Fixed » Closed (fixed)
Issue tags: -label preprocess_node

Automatically closed -- issue fixed for 2 weeks with no activity.