6

I'm trying to figure out what is the purpose of entity_keys in ContentEntityBase entities and what fields should be set as entity_keys.

For example in \Drupal\node\Entity\Node we have such entity_keys:

entity_keys = {
 "id" = "nid",
 "revision" = "vid",
 "bundle" = "type",
 "label" = "title",
 "langcode" = "langcode",
 "uuid" = "uuid",
 "status" = "status",
 "uid" = "uid",
}

Why exactly these fields are selected to be entity_keys? And, for example, why in \Drupal\comment\Entity\Comment status isn't in entity_keys?

9

entity_keys are basically an alias for fields. It allows generic code, like the storage backends, to access common fields that have different names, for example the ID. They need write the saved value, so the can't just use id() as most places.

Internally, those keys are also used by generic methods like id(), label(), getRevisionId() and so on, so they work out of the box.

There isn't really a reason that comment doesn't define status. Nothing really uses status, if you search for getKey('status'), there are no matches, while getKey('id') finds a lot.

Node only uses it internally for the isPublished().

  • Maybe important to point out: The annotation has "entity_keys" with entries like "id" = "nid". The entity has a property $entity->entityKeys with entries like ['id' => 5]. So this property holds the values of those keys, not the keys themselves. – donquixote Aug 19 '17 at 11:58
  • So If I guess correctly If we dont add them for some fields they can be just accessed/used as defaults? – Marko Blazekovic Sep 12 '18 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.