The Old and New Field API in Drupal 8 (DrupalCon Portland Session Notes)

Wayne Eaker
The Old and New Field API in Drupal 8 (DrupalCon Portland Session Notes)

New Field Types

  • More power for site builders out of the box. Moved contrib fields into core.
  • EntityReference in core!
  • Date and Time fields (Date repeat still in contrib)
  • Link
  • Email/Phone

CMI

  • No more field_config or field_config_instance tables
  • Field and instance definitions in YAML files
  • Deployment between environments is much easier (No Features module)
  • New rule for Drupal 8: "Don't have core" is now "Don't hack your active config"
  • field_create_field(), etc functions are gone.
  • Create fields with entity_create(). Field configs are entities.

EntityDisplay

  • Drupal 7: Display settings spread around. In variables table, field_config_instance table, "extra fields", in custom tables. (like for Display Suite, Field Group modules) Hard to deploy because you have to know all the things to capture with Features.
  • Drupal 8: Single location for all settings about a field: such as entity.display.node.article.default.yml

EntityFormDisplay

  • Introduces the concept "form modes": like view modes for forms. Decide which fields can show in context. (like the "show on user registration screen" checkbox)

Plugins

  • Drupal 7: hook-based: hook_field_formatter_view(), etc.
  • Drupal 8: Widgets, Formatters, Field types are all plugins.
  • Example: class MyWidget extends WidgetBase { }
  • Annotations are the replacement for hook_field_info() sort of stuff.
  • Example: class TextFieldItem extends FieldItemBase { }

Everything on an entity is a "field"

  • Properties in Drupal 7: NID, Title, etc.
  • In Drupal 8: These are called "base fields"
  • "Configurable fields" are what used to be just called "fields"
  • "Fieldable entity types" is now "extensible entity types"

Improvements

  • Improved DX: $node->body->value or $node->body0->value instead of $node->bodyLANGUAGE_NONE['value']
  • Field metadata: $entity->$field0->getPropertyDefinition(); $is_string = $$entity->$field0 instanceof StringInterface.
  • TypedData API: a way to express metadata in Drupal. Primatives, field types, entity types.
  • Validation: Entity validation decoupled from form validation. Builds on Symfony Validator.
  • Hidden widget available on all field types. Renders nothing in form.
  • Field form HTML5 placeholders are available.
  • Multiple upload: built into Field and Image fields