Summary
- The metadata for an entity type in Drupal 8 is declared within an @EntityType annotation.
- Until Drupal 8.0-alpha8, the return value of EntityManagerInterface::getDefinition() was an array.
- Similary, in Drupal 7, the return value of the equivalent function entity_get_info() was also an array.
- The annotation class now returns a classed object with methods that are backed by an interface: EntityTypeInterface.
The changes in this change notice do not affect how the metadata is declared. It is still declared within an annotation, as before.
The way we get the metadata for an entity has not changed either, it is still returned by the entity manager.
If we wanted to, we can get the metadata for the Node entity type like this:
$entity_info = \Drupal::entityManager()->getDefinition('node');
What has changed is that such an $entity_info variable is now a classed object that implements EntityTypeInterface, rather than an array. Note that the name of the interface suggests naming the return variable $entity_type
rather than $entity_info
. However, $entity_type
was previously commonly used for denoting the name of the entity type (e.g. 'node'
). See also https://drupal.org/node/2165155
Drupal 7
In Drupal 7, an $entity_info variable was a large array with any number of possibly unknown keys.
Examples:
if (!empty($entity_info['entity_keys']['revision'])) {
$duplicate->{$entity_info['entity_keys']['revision']}->value = NULL;
}
//
$link_templates = isset($entity_info['links']) ? $entity_info['links'] : array();
Drupal 8
In Drupal 8, an $entity_info variable as declared above will return a classed object.
Examples:
$entity_class = $this->entityInfo->getClass();
//
if ($entity_info->hasKey('revision')) {
$duplicate->{$entity_info->getKey('revision')}->value = NULL;
}
//
$link_templates = $entity_info->getLinkTemplates();
Note that as a result of https://drupal.org/node/2168333, some entity info properties have been renamed. In particular, those which return a class have been renamed to reflect this: getFormClass(), setFormClass(), getStorageClass(), setStorageClass().
API changes
Because the @EntityType annotation now needs to return objects, the AnnotatedClassDiscovery and AnnotationInterface needed changes.
Specifically, AnnotationInterface has 5 new methods: getProvider(), setProvider(), getClass(), setClass(), and getId(). There is no setter for the ID, that is handled internally by the annotation parser.
EntityControllerInterface was previously passed the DIC, $entity_type as a string, and the $entity_info as an array. Now it is passed the DIC and the entity info as an EntityTypeInterface object. To get the entity type as a string, use the getId() method.
hook_entity_info()/hook_entity_info_alter() were passed an array of arrays, they are now passed an array of objects.
Every previous property has a get*() or is*() method, and set*()/has*() where appropriate. For any arbitrary properties provided by modules, the generic get()/set() methods can be used.