2

Depending on the type of entity, it is either nid/vid/uid and possibly other. It is nice to have an ->id() method which allows you to work with data in an uniform fashion without relying on those details, but there are quite some use-cases, where I am running into troubles.

Just an example, getting list of revision ids for an generic entity seems not possible without knowing the name of the id-field.

$entityTypeId = $entity->getEntityTypeId(); $entityId = $entity->id(); $typeStorage = \Drupal::entityTypeManager()->getStorage($entityTypeId); $query = $typeStorage->getQuery(); $query->allRevisions(); $query->condition("??????", $entityId); 
1
  • 1
    Can't test it at the moment but try $id_field = $entity->getKey('id'); Commented Jan 5, 2022 at 13:13

1 Answer 1

3

EntityType provides a function to get field keys: EntityType::getKeys. The result is an array and the id key identifies the property name of the primary field.

See https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21EntityType.php/function/EntityType%3A%3AgetKeys

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.