0

I don't think I am yet grasping how entity field query works, all I want to do is get the ids of all the nodes connected to my node via an entity reference, how do I write that?

1 Answer 1

3

You want to find the entity IDs that are referenced from a node you've already loaded?

If the reference field is called 'field_reference', then:

global $language_content;
$langcode = $language_content->language;
$references = $node->field_reference[$langcode];
$target_ids = array();
foreach ($references as $delta => $reference) {
  $target_ids[] = $reference['target_id'];
}

Your mileage may vary with the language stuff.

Note that you usually want to keep track of the entity type (e.g. 'node' or 'user'). You can't really do anything with an entity without knowing its type.

So you might want something like:

$target_ids[$reference['target_type']][] = $reference['target_id'];

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.