3

I'm confused in how the fields can be rendered. There are 2 variables in node.tpl.php $node and $content, both of which contain fields. My task was to render all fields separately, to surround them with custom html. First I've tried

print render($content['field_service_summary']) 

and got

array(1) { [0]=> array(1) { ["#markup"]=> string(11) "Engineering" } } Engineering 

in result.

Trying to get rid of that array, I've taken another approach so the code became:

$field = field_get_items('node', $node, 'field_service_summary'); 
$output = field_view_value('node', $node, 'field_service_summary', $field[0]);
print render($output);

which actually worked, so, to clear this out I have following questions:

1) Where did that array come from when doing print render(content['field_service_engineering']); and how can I get rid of it(I still like this rendering approach better cause it's just shorter) ?

2) Is the second method I've used totally good, with no drawbacks ?

3) What's the point in keeping fields info both in $content and $node especially regarding that in vanilla node.tpl.php only $content is used for rendering fields ?

4) Just my curiosity: in $content array every field has value #object which seems to contain the node object itself. What is it for ?

1 Answer 1

1
  1. That should be print render($content['field_service_summary']); (notice the $ sign prefixing the variable name).
  2. It's technically OK, but unless you've customised the preprocess workflow to a large degree, what you'll find at $content['field_service_summary'] is actually the result of a call to field_view_value() anyway, so you're just duplicating logic. If you find that method to be the way forward for whatever reason, at least move it into a preprocess function so it's not cluttering up the template file.
  3. When nodes are loaded, they get field data attached to them. That data is in a certain format. When a field is viewed (e.g. with field_view_field()) that data is restructured into a render array, suitable to be run through render()/drupal_render(). The arrays might well contain the same data in some places, but they're formatted differently for their various different purposes.
  4. It's a reference to the node object. It might be used by a theme/render function, or it might not. Core puts it there by convention. Remember it's just a reference to the object, it's not a duplicate.
2
  • Yeah, I forgot the '$' when writing question, it's all-right in code. Anyway, the problem seems that $content['field_service_summary'] is different from what field_view_value() returns, so the render function returns different results placing some array in the beginning of rendered value in case of render($content['field_service_summary']). May 25, 2013 at 16:53
  • It seems, I've found where the bug is - in the end of every field render array there is a [0] => Array ( [#markup] => Page not found ) section. Trying to understand where did it come from. May 25, 2013 at 17:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.