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

guidot - October 6, 2007 - 08:21
Project:Content Construction Kit (CCK)
Version:5.x-1.6-1
Component:General
Category:bug report
Priority:normal
Assigned:Unassigned
Status:active
Description

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.

#1

ZrO-1 - October 6, 2007 - 21:21

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

#2

guidot - October 8, 2007 - 18:44
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.

#3

ZrO-1 - October 9, 2007 - 02:48

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

#4

guidot - October 9, 2007 - 07:46

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.

#5

obi22 - November 24, 2007 - 13:40

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

Thanks in advance.

#6

jeffreydalton.info - January 25, 2008 - 22:35
Category:support request» bug report

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?

#7

CMatters - February 28, 2008 - 06:29

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.

#8

tmai - April 1, 2008 - 09:16

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

#9

xjm - June 10, 2008 - 19:13

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?

#10

xjm - June 10, 2008 - 18:55

Update -- I just found this patch:

http://drupal.org/node/209229

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

#11

xjm - June 18, 2008 - 17:50

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.

 
 

Drupal is a registered trademark of Dries Buytaert.