15

I have a entity_reference field with BaseFieldDefinition::CARDINALITY_UNLIMITED cardinality. I want a list of referenced entity ids. I know that I can get all the referenced entities by calling $x->referencedEntities(), but I want only the ids to get the entities loaded later. Currently I am doing this:

  $a = [];
  foreach ($entity->autoren as $v) {
    $a[] = $v->target_id;
  }
  $this->setCurrentAnwaltReferenzen($a);

Does anyone know a better solution like it was in D7 (->raw())?

Thanks

1 Answer 1

38

As an alternative you can do it in one line with an array function instead of a loop:

$ids = array_column($entity->autoren->getValue(), 'target_id');

Edit: Use getValue() to only get the array of the field.

0

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.