When I load a node in a custom module using node_load, I get an object that contains an array per field that looks approximately like this:
[field_fieldname] => Array
(
[und] => Array
(
[0] => Array
(
[value] => <p>Hello World!</p>
[something] => <p>Hello World!</p>
[type] => full
)
[1] => Array
(
[value] => <p>Hello Other World!</p>
[something] => <p>Hello Other World!</p>
[type] => full
)
)
)I want to have this rendered/themed the way Drupal 7 does, i.e. something like:
<div class="field field-name-field-fieldname field-type-text field-label-hidden">
<div class="field-items">
<div class="field-item even"><p>Hello World!</p></div>
<div class="field-item odd"><p>Hello Other World!</p></div>
</div>
</div> The Drupal functions that seem to be intended for this seem to expect either a node object as argument, or a richer field array. I don't like passing node objects through series of functions, and don't know how to build the richer field.
(Trying something similar in D6 I finally managed to get themed fields using node_build_content(), but I had to write my own parsing code, and in D7 that node_build_content() returned an empty string.)
I searched for hours on this site, but people seem perfectly happy to just take the contents of $node->field_fieldname['und'][0]['safe_value'] and do their own handling. Myself, I'd prefer to use the standard way, and not to reinvent the wheel.
Comments
Comment #1
yched commentedHave you tried field_view_field() / field_view_value() ?
Comment #2
brankoc commentedI am sure I tried that, but I think it must have gone wrong the first time, because I did not further pursue that direction. Also, field_view_field() did not seem the right function, because you have to pass it a node even if you just want a field themed. Thanks for pointing me in the right direction!
What is the default display supposed to be? I would have figured the default setting for the node-type of the loaded node, but I have mine set to hide the label, and yet when I run drupal_render() on the return value of field_view_field('node', $node), I still get a label.
Comment #3
yched commentedTrue, because there's no such thing as a field value outside of an actual entity. Some formatters need info from the containing entity (e.g
file formatters let you link to the node URL)
field_view_field('node', $node) (without the $display param) just uses the defaults : default formatter for the field type (the one specified in hook_field_info()) with default values for its settings, and 'label' above.
You can use the $display param to specify the name of a view mode, or an actual array of display settings - see the PHPdoc for the function.
Comment #4
Anonymous (not verified) commentedTake a look at field.tpl.php to override the output as well, and perhaps (override) a function in template.php is needed?
See for more info:
Note: if you are using a child theme you may not have the templates folder. You can make one in the root folder of your theme folder, and then add the fields.tpl.php in there.
So for example:
Comment #5
dddave commented