I'm theming a content type in a node-mycontenttype.tpl.php. According to Handbook about CCK Field Formatters I managed to print all needed field values, but I could not find a way to print the field labels.

I tried to understand how cck/theme/template.php fetches a label, but when it comes to drupals hooks I'm still lost. BTW, Contemplate does not help with this.

Thanks for any help.

Comments

ZrO-1’s picture

I found the (partial)* solution you are looking for. You can use: <?php print($node->content['field_my_field']['#value']); ?> to pull the entire formatted field for your content type. This will display the field the way you have specified in the "Display Fields" tab of your content type - including the label.

* I say partial because I don't know if this will work on a "multiple values" field. I havn't tried it yet. (I am new to drupal).
Hope that helps...

guidot’s picture

Title: How to print field labels? » How to print field labels in node-mycontenttype.tpl.php?

Thanks, I tried that, and it does print the themed label plus content.

I need to process the content separately, and I tried but could not figure out how the expression has to be modified to give me just the label.

ZrO-1’s picture

have you tried <?php print $field_my_field[0]['label']; ?>? That is from the CCK theme folder example template file, I just replaced 'view' with 'label'. I think that should work...

guidot’s picture

Don't remember, if I did. I tried so many variations, so I give it a try it now.

Let's see...

...no, nothing. I also tried to replace ['label'] with ['#label'], but still nothing gets displayed.

altavis’s picture

I'm also interested in field label theming. If someone knows the solution please post it.

Thanks in advance.

jeffrey.dalton’s picture

Category: support » bug

I did quite a bit of research on finding a posted solution to this issue as well and came up short... is this a bug in CCK?

CMatters’s picture

I wouldn't call it a bug, perhaps just a feature left out or not well documented. You can override the theme of each field (through template.php) and it gives you the label. Here is the expert from CCK's README...

Available variables in field templates :
$items          : an array containg the values of the field.
                  $items[n]['view'] contains the ready-to-use, filtered, formatted value
$label          : the label of the field
$label_display  : the display settings for the label ('hidden', 'above', or 'inline')
$field_empty    : TRUE if there is nothing to display in the field
$field_type     : the type of the field,
$field_name     : the name of the field,
$field_type_css : same as above, with '-' signs replaced with '_' for use in css properties
$field_name_css : same as above, with '-' signs replaced with '_' for use in css properties
$field          : an array containing the full CCK field object

The supplied template.php code is...

<?php
// $Id: template.php,v 1.3.2.4 2007/03/05 22:19:04 yched Exp $

function phptemplate_field(&$node, &$field, &$items, $teaser, $page) {
  $field_empty = TRUE;
  foreach ($items as $delta => $item) {
    if (!empty($item['view']) || $item['view'] === "0") {
      $field_empty = FALSE;
      break;
    }
  }

  $variables = array(
    'node' => $node,
    'field' => $field,
    'field_type' => $field['type'],
    'field_name' => $field['field_name'],
    'field_type_css' => strtr($field['type'], '_', '-'),
    'field_name_css' => strtr($field['field_name'], '_', '-'),
    'label' => t($field['widget']['label']),
    'label_display' => isset($field['display_settings']['label']['format']) ? $field['display_settings']['label']['format'] : 'above',
    'field_empty' => $field_empty,
    'items' => $items,
    'teaser' => $teaser,
    'page' => $page,
  );

  return _phptemplate_callback('field', $variables, array('field-'. $field['field_name']));
}
SOLUTION!?!

So there is the ability to use field labels when theming just certain fields.. but in a complete node theme? Good question. I would say so, I tried a couple of things but it made my head hurt so I went on to try something else. Here's the workaround I settled one. I use $node->content['field_add_detail']['#value'] as previously discussed and use two PHP functions, strpos and substr, to trim it down to JUST the label. Here's some code:

First I created a function at the top of my node-contenttype.tpl.php.

function makelabel($label) {
  $separator = ":";                      // the colon separates the label from the value in our string
  $pos = strpos($label,$separator);    // finds the position of our separator in the string
  return substr($label,0,$pos);            // Take the beginning of the string (0)  to the separator ($pos), send it back, and trash the rest
}

Second, when I coded the actual output I called my new function to format (chop?) the label. Here's an example:

<?php echo makelabel($node->content['field_my_field']['#value']); ?>
<?php echo $field_my_field[0]['view']; ?>

Voila, ici! It's an ugly hack but it works for me. I hope that it works for you.

tmai’s picture

Hello have the same problem with printing field's label, I tried your guide, it partially works, because the label of number value didn't show when I use ['#value'}, do you have any idea for this, please share it with me.
Thanks
Thuan

xjm’s picture

I'm having a similar issue; I want to be able to get and use the label value separately from the field value (in my case, in a table). I've considered using regular expressions to parse out the label from the [#value] array element, but obviously some sort CCK solution would be preferable. Has anyone looked into a patch for CCK that would add the label alongside the value, weight, etc. to the data in the $node object?

xjm’s picture

Update -- I just found this patch:

http://drupal.org/node/209229

I have not tested it yet but it may be a solution.

xjm’s picture

I created a revision of the patch at:
http://drupal.org/node/209229#comment-886855

Patch file is at:
http://drupal.org/files/issues/add_labels_cck_1_7.patch

Please review/test there.

jmlavarenne’s picture

Careful - I don't think $node->field['#value'] is run through any filters.

xjm’s picture

Actually #value is filtered and then some; it includes an HTML-rendered label, which is why I would have had to regex it out. Anyway, I used the patch (linked above) to get the functionality I needed.

xjm’s picture

Status: Active » Closed (duplicate)

Marking as duplicate; see http://drupal.org/node/209229.

jmlavarenne’s picture

I always thought the filtered information was $node->field['#view'] - that's what I use.

Anonymous’s picture

Version: 5.x-1.6-1 » 6.x-2.6

This is a very old thread, but as I am fairly new to drupal theming and found the solution myself after searching for days, I am posting it here for future reference.

The code for displaying labels is:

<?php print $node->content['field_name']['field']['#title'] ?>

The source for this is http://www.davidnewkerk.com/book/30 however if your fields are part of a group, then you'll find you need to modify it to the following code.

print $node->content['group_specs']['group']['field_name']['field']['#title']

I figured that out after reading up my arrays. Cheers!

xjm’s picture

Version: 6.x-2.6 » 5.x-1.x-dev

The issue was for D5.x versions of the module. It was patched in the D6 version, but not in the D5 version.

unik’s picture

Michsk’s picture

mind sharing your snippet, i can't figure out what value to put where. Maybe a real life example would help.

bushman’s picture

<?php print $node->content['field_name']['field']['#title'] ?>

These works! GREATE Printing label of cck field

Anonymous’s picture

Issue summary: View changes

I know this Fred is old, but maybe it still helps someone.

#16 works! At least, when your field is part of a group.