Hello!

I tried to make a custom theme that displays each field of my content in a tabular form as a name-value list. Unfortunately I realized, that the cck field label attribute does not appear in the $node object passed to the template.

I made this little patch to content.module in order to get my field labels, but since this was the first time I saw the module code, there may be a more elegant solution.

yours,
pentike

CommentFileSizeAuthor
#4 add_labels_cck_1_7.patch633 bytesxjm
content.module.diff722 bytespentike

Comments

xjm’s picture

This is a feature I need as well. I will try applying the patch and see if it works for me.

xjm’s picture

When I apply this patch, the label item is indeed added to the $node object. However, something strange happens: node types that use the default CCK-generated body have serious rendering errors as soon as I apply the patch. I furthermore cannot get rid of the rendering errors simply by reverting the patch; I have to resubmit each and every node that uses the CCK-generated body so that the body goes back to the way it's supposed to be. (So: as usual, do not test this patch on production sites.)

Here's an example of HTML for a field with the un-patched CCK body.

<div class="field field-type-text field-field-med-school">
  <div class="field-items">
    <div class="field-item">
      <div class="field-label-inline-first">
        Medical school:&nbsp;
      </div>
      Medical College of Pennsylvania
    </div>
  </div>
</div>

When the patch is applied it immediately becomes:

<div class="field field-type-text field-field-med-school">
  <div class="field-items">
    <div class="field-item">
      <div class="field-label-inline-first">
        Medical school:&nbsp;
      </div>
      Medical College of Pennsylvania
    </div>
    <div class="field-item">
      <div class="field-label-inline">
        Medical school:&nbsp;
      </div>
      M
    </div>
  </div>
</div>

It looks like CCK is treating the label as an additional field item, as if it were part of a list of multiple values. I am betting the text widget recursively uses something like $field_items[$item_key][0] to get the text strings for multiple values, so that's why it's printing the first letter of the label as an additional field value.

So, it looks like your solution is incompatible with the existing CCK text widget display (and possibly other field types that have multiple values). I will do some testing and see if I can come up with a better way to implement your change that will work with these widgets.

xjm’s picture

I was able to resolve the bug by modifying your code slightly:

      while ($value = db_fetch_array($result)) {
        // MODIFIED BY XJM
        $type_info = _content_type_info(FALSE);
        $label_text = t($type_info['fields'][$field['field_name']]['widget']['label']);
        $value['label_text'] = $label_text;
        // END MODIFICATION

        $additions[$field['field_name']][] = $value;
      }
      return $additions;

This puts the label in the correct array.

I'll roll a revised patch and post it.

xjm’s picture

Version: 5.x-1.6 » 5.x-1.7
StatusFileSize
new633 bytes

Here's the patch. I made it against the 1.7 release version, but it should work just as well for 1.6 and the current dev build. Please review/test.

xjm’s picture

magnumz’s picture

Tested xjm's patch and now it outputs field label when i use print $field_name[0]['label_text']; in template ,
but the output is not localized. When i use print t($field_name[0]['label_text']); field's label output is localized. I guess the t() used in patch on field label is not working like it should.

xjm’s picture

I don't know much about how localization works... is there some reason that the localization would not be run until after this bit of code?

$label_text = t($type_info['fields'][$field['field_name']]['widget']['label']);
$value['label_text'] = $label_text;

Is there anything wrong with the code above?

psynaptic’s picture

Version: 5.x-1.7 » 7.x-2.x-dev

Just like to say I would find this useful. Much better than hardcoding labels into node.tpl.php files.

psynaptic’s picture

Version: 7.x-2.x-dev » 6.x-2.1
Status: Needs review » Fixed

And now I see that it's present in the 6.x version!

xjm’s picture

Version: 6.x-2.1 » 5.x-1.10
Status: Fixed » Needs review

Patch is still required for this functionality in the 5.x version (most recent is 1.10) which is the version the issue was created for.

psynaptic’s picture

Sorry!

mishoboss’s picture

Version: 5.x-1.10 » 6.x-2.3

I don't get it - psynaptic says it's fixed for 6.x-2.1, but now I updated to 6.x-2.3 and there is still no "label" data...

How could this be done?

xjm’s picture

The patch above is specific to the Drupal 5 version of CCK. If the labels are present in the 6.x version of the $node object, well and good. If not, then the patch would need to be ported to the 6.x version or a new patch created.

bur’s picture

In 6.x it is possible to print labels in this way:

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

markus_petrux’s picture

Version: 6.x-2.3 » 6.x-2.6
Component: content.module » Documentation
Category: feature » support
Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

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

Anonymous’s picture

Yep, I learnd that from http://www.davidnewkerk.com/book/30

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

to print labels and if your fields are part of a group then use

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

Cheers.

psynaptic’s picture

Category: support » feature
Status: Closed (fixed) » Active

This still kinda sucks though. Ideally, it would be best to have the fully rendered field in the node object including the label and its formatting set in the CCK UI.

Sorry to re-open but I think a new feature request is the right outcome from this point.

sunchaser’s picture

Is there still no way to call the field label of a cck field in node - view ?
The above suggested print $node->content['fieldname']['field']['#title'] doesn't seem to work on my end.

a print_r($node) also reveals the labels are not available in the node object.

FuXXz’s picture

Where i can find the solution for this issue?

I want to print the label or the best solution for me is to print the entire CCK Field with label, items, display settings

manute’s picture

FuXXz you can try with: <? print $node->content['#content_extra_fields']['title']['label']; ?>
In $node->content['#content_extra_fields'] you will find all fields and their labels.

flokosiol’s picture

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

This works fine, but not if the field is inside a CCK fieldgroup. In this case try ...

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

EDIT: sorry, duplicated entry see #17

Anonymous’s picture

Version: 6.x-2.6 » 6.x-2.9

#17 worked for me (Thanks, filmoreha!). Echo #18.