5

I have a node.tpl.php file where I need to output a field from an entity referenced via the entity module (a reference field). I have tried using:

print render($content['field_parent']['#items'][0]['entity']->field_main_image);

but nothing appears! I also tried this way:

$parent = node_load($node->field_parent['und'][0]['target_id']);
print render($parent->field_main_image);

I just would like to render() a related field just as I do with $content fields.

Any hints ? Thank you

  • How are you getting your $node object? – Alex Gill Jul 3 '12 at 9:20
  • it comes as a default variable from drupal core – Antonio Madonna Jul 4 '12 at 10:14
  • I mean would help to show it in your code. Have you tried a var dump of $node to see if you are getting any data? – Alex Gill Jul 4 '12 at 10:16
1

You can use https://www.drupal.org/project/inline_entity_display to do that with entity reference and field collection field.

| improve this answer | |
0

You can't render the member variable of a node directly. In a frist step you have to bring it in a renderable from (array):

//returns the field in the correct language
$field_parent = field_get_items('node', $node, 'field_parent');

//retruns an array which you can render
$render_array = field_view_value('node', $node, 'field_parent', $field_parent [0]);

print render ($render_array);

Here's the differents between the two structures of these arrays. The array in the member variable of $node looks like:

array(1) {
  ["und"]=>
  array(1) {
    [0]=>
    array(3) {
      ["value"]=>
      string(15) "Your value"
      ["format"]=>
      NULL
      ["safe_value"]=>
      string(15) "Your value"
    }
  }
}

A renderable array (for a textfield) looks like that:

array(2) {
  ["#markup"]=>
  string(18) "Your value"
  ["#access"]=>
  bool(true)
}
| improve this answer | |
0
$parent = node_load($node->field_parent['und'][0]['target_id']);
print render($parent->field_main_image);

should work as

$parent = node_load($node->field_parent['und'][0]['target_id']);
print drupal_render(field_view_field('node', $parent, 'field_main_image', 'displayname'));
| improve this answer | |

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.