2

I can get the field using entity_metadata_wrapper like this

$node = node_load($nid);
$node_wrapper = entity_metadata_wrapper('node', $node, array(
    'langcode' => $GLOBALS['language'],
    'bundle' => 'unit'
     )
);

$field_value = $node_wrapper->field_photo->value();

And that returns an array which I don't know how to render. I tried drupal_render() but it seems that it doesn't do the job.

So how can I render this field ?

3

The photo field is an image field? The value() function returns an array which is actual file data linked to the image. To render (or to create a renderable build), you can for example use theme_image function, which prints out an <img> tag.

$node = node_load(43);
$node_wrapper = entity_metadata_wrapper('node', $node, array(
    'langcode' => $GLOBALS['language'],
    'bundle' => 'subject'
));

$field_value = $node_wrapper->field_teaser_image->value();
$build = array(
    '#theme' => 'image',
    '#path' => $field_value['uri'],
    '#alt' => $field_value['alt'],
    '#title' => $field_value['title'],
    '#width' => $field_value['width'],
    '#height' => $field_value['height'],
);

print drupal_render($build);
| improve this answer | |
  • 1
    That works only for image fields. but I have other fields I want to render I just used that one as an example – Youssef Bouhjira Aug 27 '13 at 15:09
  • Depending the type of field, the method for rendering is different ... (plain values, arrays, ...). Why don't you use field_get_items and field_view_value for the rendering? – Mike Vranckx Aug 27 '13 at 15:48
  • I will only need this photo field for now after all, so I'll mark this as the accepted answer. – Youssef Bouhjira Aug 27 '13 at 15:52
  • 1
    This shows a huge shortcoming of metadata_wrappers in my opinion. I would hope that we could just do $node_wrapper->field_teaser_image->render(); – Dan Shumaker May 24 '17 at 16:41

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.