I'm trying for hours now to show the fully themed field in my custom node templates.
Example:
I have a node-custom_foo.tpl.php file in my theme directory. Inside node-custom_foo.tpl.php I call:
<?php print $field_my_field[0]['view']; ?>
But all that is printed is the value of $field_my_field concatenated with the prefix. Using print_r($field_my_field[0]['view']) tells me, that $field_my_field[0]['view'] does not contain any HTML tags including the label, applied formatters and so on.
All content types which have no separate "node-content_type.tpl.php" file in my theme directory, are rendered fine (the fields have labels, correctly applied formatters, ...). So how do I access the fully rendered HTML version of a field in my custom node templates (and not just the value concatenated with the prefix)?
Comments
Comment #1
dodorama commentedprint content_format('field_my_field', $field_my_field[0]);CCK Documentation for themers
Comment #2
yojoe commentedcontent_format() doesn't help, it's the same as just accessing $field_my_field[0]['view'], it outputs no label and applies no formatter, just the plain value concatenated with the prefix :(
Comment #3
karens commentedApplies the default formatter, whatever that is.
Applies the 'trimmed' formatter, assuming you're on a field that has one. You're right that it does not apply a label, but it certainly does apply the formatter.
If you want the complete formatted item with a label and all the html in 5.x, you get it from the $content array, like:
The formatter used in the $node content array will be whatever one you selected in the 'Display fields' tab for the view you are using.
Using content_format() is suggested because it is future-proof. The place where the formatted code is found is different in 4.7 and 5.x and is likely to change again in 6.x, so if you use it, you will have to change all your templates every time you upgrade to a new Drupal version, but content_format() works the same in all versions.
In your example where you say $field_my_field[0]['view'] has no formatters applied because you see no html tags, that's incorrect. Most of the formatters do not wrap the element in html, they do things like apply filters to the content of the individual field value (for a textfield) or format a number with commas and decimals, so the fact that you don't see any html does not mean the formatters weren't applied.
The assumption is that if you are printing individual field items in your template, it's because you want to display it differently than the default template. If it came wrapped in html, it might make no sense at all in your next context, so if you use content_format() you need to add your own labels and html to it.
Comment #4
yched commentedIt's true that we should probably find a way to make the 'fully themed version' in a node-template variable...
Comment #5
yojoe commentedThank you,
<?php print($node->content['field_my_field']['#value']); ?>is what I was looking for.No, the assumption is, that I like the individual fields as they are in the default template, I just want to change the order of the fields and the layout of the node. I don't want the individual fields to be displayed differently, that's why I use the 'node-content_type.tpl.php' approach. Therefore it's ok, that the individual fields come wrapped in HTML, indeed it saves me a lot of work. This way I can still control the individual fields via the 'Display fields' tab. If I would want to display the individual fields differently, I would use field.tpl.php and/or field-my_field.tpl.php files.
Comment #6
dodorama commentedYou can even try to theme each field with its custom tpl file even if the syntax is a little bit more complicated .
This way you can manage fields order and formatters using the UI without having to deal with the template file whenever you want ot make changes.
Comment #7
bartoki commentedyojoe, I'm trying to do the same thing, and have been for DAYS, and I'm having trouble using
?php print($node->content['field_my_field']['#value']); ?>. This works great on basic fields and certain fields that were generated by other modules that use CCK (for example, the ubercart module creates a field called "sell price" and I'm able to reference this with 'sell_price'.However, I have a field called "genre" that I created with CCK by going to admin>content>taxonomy and adding a new vocabulary, and then attaching this to each node type I want it on. For the life of my, I can't figure out how to reference this field! I've tried many permutations, but to save some screen space here I'll only list one:
<?php print($node->content['field_genre]['#value']); ?>. But it seems like for a field created by taxonomy, the field name is something more mysterious.Do you have any thoughts?
Best,
David
Comment #8
lias commentedThank you for the explanation KarenS. I didn't know this content_format() and future upgrades
Comment #9
pathscollide commentedYeah, thanks KarenS! Your explanation also helped me not waste lots more time trying to figure this out.
By the way bartoki, though I don't know the solution to your problem, it sounds like you're working with a taxonomy term and not a CCK field. Those are two completely different things and the solution posted here won't work for you.
Comment #10
socialnicheguru commentedsubsribing
Comment #11
fxarte commentedHi
Same problem here.
If you use content_format for number, text, user, and node reference fields, these are the field formatter names :
Taken from each .module file in the CCK module, hook_field_formatter_info() implementation.
Comment #12
detot commentedWhy are you guys not using $FIELDNAME_rendered ?
Comment #13
karens commented$FIELDNAME_rendered is only in D6. The original question was answered I think.