0

I need to get all the names of the entity, my entity is called 'a_process'? How can I write such query in Drupal 8?

$nodes = entity_load_multiple('a_process', $name);

1 Answer 1

3

You can use the entity type manager to load entities of any type:

$entities = \Drupal::entityTypeManager()
  ->getStorage('a_process')
  ->loadMultiple();

foreach ($entities as $entity) {
  // ...
}

If your code is in a container aware context, be sure to inject the entity manager as a service instead of referring to \Drupal explicitly.

Of course if you always know the entity type up-front you can use the method on the entity class itself, eg

$entities = \Drupal\foo\Entity\AProcess::loadMultiple()
4
  • But I need to reach only 'name' field, with this code I get an endless array
    – badm
    Commented May 13, 2016 at 9:16
  • Well no, not endless. You can get field values through magic methods, so $entity->name->value, for example, if your field is called name
    – Clive
    Commented May 13, 2016 at 9:25
  • Hey, Clive :) You are my last hope. Still struggling with the fields. Now I am having a 'file' field. And after declaration the field in my content entity, the field was split into three: file__target_id, file__description, file__display in my database. How can I reach the field file__target_id ? the standard method $entity->file__target_id->value doesn't work (((
    – badm
    Commented May 18, 2016 at 8:19
  • 1
    Found the solution in 10 seconds after posting the comment: $entity->file->target_id works :)
    – badm
    Commented May 18, 2016 at 8:26

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.