Drupal 8 Entity API: DrupalCon Austin Session Notes

June 3, 2014 by Wayne Eaker
: Module Developers
Drupal 8 Entity API: DrupalCon Austin Session Notes

Session Link: https://austin2014.drupal.org/session/drupal-8-entity-api

  • Drupal 7 Entities were a side effect of breaking fields out from nodes.
  • In Drupal 8, everything is an entity. Not just content, but configuration
  • ContentEntityInterface and ConfigEntityInterface are two different types
  • \Drupal::entityManager(); $entity = $manager->getStorage('comment')->load($id);
  • Comment::load($id); //shortcut
  • No more LANGUAGE_NONE. $entity->subject->value;
  • $term_id = $entity->field_tags[2]->target_id;
  • $entity->hasField($field_name);
  • Methods for common fields like $node->getAuthor()
  • Translation: $entity->getTranslation('de');
  • $manager->getTranslationFromContext($entity);
  • $entity->getFieldDefinition($field_name);
  • $entity_manager->getFieldDefinitions('node', 'article');


  • Base Fields (node title, node id, user name, user roles, etc.)
  • Bundle Fields (node body, node tags, user tags, all configurable fields)
  • Configurable Fields: show in UI, managed by configuration system
  • Field Storage Definitions: $manager->getFieldStorageDefinitions('node'); // what used to be in field_config
  • Module provided fields: hook_entity_base_field_info();
  • Computed fields: allows you compute when it's accessed: $node->body->processed
  • Computed fields for entity references: $node->uid->entity->label();
  • new UUID field on all content entity types (no UI)
  • Language field
  • Path field

Config entity

  • No fields
  • $view->name is a string vs $node->title is an object
  • Typed Data API: can get useful formats for fields

Providing New Content Entity Type

  • Use a class with an annotation
  • Can define base field definitions in public static function baseFieldDefinitions()
  • Widgets and formatters can be used with base fields.
  • Field definitions generate the entity tables automatically!! No requirement for hook_schema()
  • preSave() / postSave hook type functions in EntityInterface
  • ViewBuilder will display all fields using formatters
  • FormBuilder will build your entity form.
  • Extend EntityAccessController for access control
  • Entity lists (mainly for config entities) like node_get_types()

Entity Validation API

  • Decoupled from form validation so it works with REST
  • makes use of Symfony Validator compoent
  • based on Constraint plugins
  • $violations = $entity->validate();

Entity Query

  • \Drupal::entityQuery('block')->condition()->execute();
  • No more propertyCondition/fieldCondition. Just condition();
  • Works independently of the storage.
  • Has language support, relationship support, aggregation support

Do you want to become an expert Drupal developer? Our classes will get you there in record time!

"The entire course was much more than I expected. I did attend a previous Drupal course, but there was no comparison. This ecourse rocks!"
Missi - Webmaster at a public library system