I'm using an EntityFieldQuery to load a node and try to access some of its fields, but can't seem to get any of the fields. The code is below:

$nodestoload = $query->execute();
$nids = array_keys($nodestoload['node']);
$nodes = entity_load('node', $nids);
$output = field_view_field('node', $nodes, 'suppliername');

However, $output is returning an empty array. I can see that the node loads using the print_r. I've also tried various options (such as replacing 'node' with the actual bundle type). But nothing seems to be working... How do I access fields from a node that I loaded programmatically?

  • Names for fields added through the UI are prefixed with field_, any chance that's the problem? – Clive Feb 10 '13 at 3:12
  • When I try that I get a "EntityMalformedException: Missing bundle property on entity of type node. in entity_extract_ids" error?? – RedRaven Feb 10 '13 at 3:13
  • 1
    field_view_field() is for a single value, you just need to loop over $nodes... – Clive Feb 10 '13 at 3:14
  • Seems to have worked, thanks. Want to put it as the answer? – RedRaven Feb 10 '13 at 4:12

The correct code is the following one.

$nodes_to_load = $query->execute();
if (isset($nodes_to_load['node'])) {
  $nodes = entity_load('node', array_keys($nodes_to_load['node']));

  foreach ($nodes as $node) {
    $output = field_view_field('node', $node, 'suppliername');
    // ...

field_view_field() accepts a single entity object, as second argument; you cannot pass it an array of entities.


Your code will only work if there is only a single value returned by your EFQ. Whatever you want to do with the nodes, you need to put in a foreach():

$nodestoload = $query->execute();
foreach ($nodestoload as $loaded_node) {
  $nid = array_keys($loaded_node['node']);
  $node = entity_load('node', $nid);
  $output = field_view_field('node', $node, 'suppliername');

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.