I would like to create a node programatically and one field field_supervisors has an entity reference field for the node type People.

In order to set the correct entity reference information I need to set:

  ["target_id" => 1],
  ["target_id" => 2],
  // etc..

How can I get the individual id of the entity reference from the field_supervisors. Do I really need to look up the node People before to get the id like $node->id() or is there a way to return something like this pseudo code:

Field->field_supervisors->allReferencedEntitiyOptions which will return all People that are available ?



1 Answer 1


Do I really need to look up the node People before to get the id

Yes, there's no auto-magic method for that. It's not a lot of code though. Without DI, for example:

$ids = \Drupal::entityQuery('node')->condition('type', 'person')->execute();
  • thanks, I was hoping there is a way to return all entity reference options a certain field has, but that will do the job.
    – supersize
    Nov 9, 2021 at 16:11
  • You could pull the ER field apart and I'm sure find the code to do that based on the field settings, but if you already know the conditions you want it seems like a waste of effort
    – Clive
    Nov 9, 2021 at 16:12

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.