3. History
CCK1 → Drupal 5, Drupal 6
CCK2, CCK3 → Drupal 5, Drupal 6
Drupal 7 → Field API
Drupal 8 → Entity Field API
4. D8 changes overview
Fields are
- base fields (properties)
- configurable fields (entities)
Fields are classes with interfaces
Configurable fields are separate kind of entity
17. Drupal 7
// Entities are complex, they contain other pieces of data.
$entity;
// Fields are not complex, they only contain a list of items.
$entity->image;
// Items are complex , they contain other pieces of data. They’re also
translatable and accessible (has permissions).
$entity->image[LANGUAGE][0];
// The file ID is a primitive integer.
$entity->image[LANGUAGE][0][‘fid’];
// The alternative text is a primitive string.
$entity->image[LANGUAGE][0][‘alt’];
18. Drupal 8: under the hood
$entity instanceof EntityInterface;
$entity->get('image') instanceof FieldInterface;
$entity->get('image')->offsetGet(0) instanceof FieldItemInterface;
$entity->get('image')->offsetGet(0)->get('alt') instanceof String;
is_string($entity->get('image')->offsetGet(0)->get('alt')->getValue());
19. Drupal 8
// With magic added by the Entity API.
$string = $entity->image[0]->alt;
// With more magic added by Entity API, to fetch the first item
// in the list by default.
$string = $entity->image->alt;
23. Defining dependencies
// Field formatter annotation class.
use DrupalfieldAnnotationFieldFormatter;
// Annotation translation class.
use DrupalCoreAnnotationTranslation;
// FormatterBase class.
use DrupalfieldPluginTypeFormatterFormatterBase;
// Entityinterface.
use DrupalCoreEntityEntityInterface;
// Fieldinterface
use DrupalCoreEntityFieldFieldInterface;
32. Useful links
Original initiative
http://entity.worldempire.ch/
Plugin API
https://drupal.org/node/1637614
Field API series from Realize
http://realize.be/drupal-8-field-api-series-part-1-field-formatters
http://realize.be/drupal-8-field-api-series-part-2-field-widgets
How Entity API implements Typed Data API
https://drupal.org/node/1795854
Typed Data API
https://drupal.org/node/1794140