3

It's very easy to load entities with entity_metadata_wrapper('node', $nid) - but is there any way to load multiple entities, similar to node_load_multiple()?

I've tried passing in an array instead of a single id, but it gives the error: Invalid data value given.

If there isn't a function for this, is there an efficient way of doing this, or is a looping the only way?

2
  • Are you sure you can pass the node ID to entity_metadata_wrapper(), as your code shows?
    – apaderno
    Dec 14, 2016 at 7:52
  • @kiam Definitely, arg 2 is: The entity to wrap or its identifier per drupalcontrib.org/api/drupal/…
    – Clive
    Dec 14, 2016 at 8:58

1 Answer 1

2

There isn't, but you can just combine the two to get the same effect:

foreach (node_load_multiple($nids) as $node) {
  $wrapper = entity_metadata_wrapper('node', $node);
}
4
  • Well there's no need to load the node, as entity_metadata_wrapper also accepts $nids itself.
    – Chris
    Dec 14, 2016 at 10:11
  • 2
    Yep, which it then runs straight through entity_load, which in turn does the same thing as running it through node_load. Accepting the ID is just a convenience of the function, the node will still be loaded using core API functions, there's no way to get around that. That's why you're asking, right? Because you don't want to run node_load in a loop, you want to use functionality synonymous with node_load_multiple? EMWs don't have that built in, you need a bit of glue code like the above
    – Clive
    Dec 14, 2016 at 10:21
  • wouldn't $wrapper need to be an array and that second line should just pop the emw return value to the wrapper array?
    – cdmo
    May 19, 2017 at 14:35
  • @cdmo I think the idea here is that you're doing something with the $wrapper in each iteration. You could certainly build a $wrappers array for later use instead if that's what you're looking for.
    – KingAndy
    Jun 14, 2018 at 9:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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