D8: Fields reborn

Kristof De Jaeger - swentel

http://realize.be/slides/devdays-2014

D8: Fields reborn

Kristof De Jaeger - @swentel

Drupal Developer Days Szeged
march 2014

http://realize.be/slides/devdays-2014

Related sessions

Drupal 8 Entity API
Wolfgang Ziegler
was earlier today :)

Code-Driven Content Modelling with Drupal 8
Tobias Stöckler
Friday - 10u (workshop)

Broken record

Amazing changes

... for the 3rd time

yched, amateescu, swentel
fago, Berdir, plach,
effulgentsia, plopesc, andypost ..

Site building features

New field types

Entity reference

Fairly complete port of D7 entity_reference.module
Taxo, file, image fields: still separate field types

Date / Datetime

The "repeat" features from D7 stays in contrib

Link

Basic version (URL, text)
No support for internal paths (e.g. node/1)

Email

In Place Editing

Fieldable blocks

aka Beans in core

Form modes

Beware of required fields with no default values...

Form modes UI

Field Overview UI

Fields are tied to an entity type

A field cannot be "shared" across entity types,
only across bundles of a given entity type

APIs !!!

There will be code

Unified APIs and features

Naming is hard...

Everything is a field

Everything

Everything in a ContentEntity is a field

Different kinds of fields...

Properties: what you find inside a FieldItem ('value', 'format', 'target_id'...)

Code architecture

Unified FieldDefinitionInterface

Field definition: name, type, label, cardinality, settings, default value...

Unified FieldDefinitionInterface


        
        Implementations:
            
  • configurable fields: FieldConfig, FieldInstanceConfig

Grabbing field definitions

field_info_fields()
            field_info_field($field_name)
            field_info_instances($entity_type, $bundle)
            field_info_instances($entity_type, $bundle, $field_name)

Field storage

Field storage (@todo, fingers crossed)

Problem: Plan:

CMI

CMI

Field definition structures

Conversion "only" took 1½ year

FrontEnd United - London, March 2013

D8: Field structures as ConfigEntities

field.field.node.body.php


    

field.instance.node.article.body.php


    

"Don't hack core" is replaced by
"Don't hack your active config"

@alexpott - Core committer and CMI maintainer

Edit in your staging folder, then import
...or use drush cedit

CRUD API - D7

Dedicated functions:
field_create_field(array());
field_update_field(array());
field_delete_field($field_name);
............_instance(array());
+ associated hooks...

CRUD API - D8

Regular Entity CRUD API:
$field = entity_create('field_config', array(
  'name' => 'body',
  'entity_type' => 'node',
  'type' => 'text_with_summary',
);
$field->save();

$field->cardinality = 2;
$field->save();

$field->delete();
+ regular hook_entity_[ENTITY_TYPE]_[OP]() hooks

EntityDisplay

Display settings in D7

Scattered around: Each with separate "alter" hooks
Loads needless stuff in memory

EntityViewDisplay (ConfigEntity)

entity_view($entity, $view_mode) :
  • Loads the relevant display
  • Alters it as a whole
         hook_entity_view_display_alter(EntityViewDisplay $display);
  • Injects it into all the callstack
         EntityViewBuilder::buildContent(array $entities, array $displays);
    hook_entity_view(EntityInterface $entity, EntityViewDisplay $display);

entity.display.node.article.teaser.php


    

EntityViewDisplay API

$display = entity_get_display('node', 'article', 'teaser');

$display->setComponent('body', array(
  'type' => 'text_trimmed',
  'settings' => array('trim_length' => '600'
))
->removeComponent('image')
->save();

$options = $display->getComponent('body');
// array(
//   'type' => 'text_default'
//   'weight' => 0,
//   'settings' => array(),
//   'label' => 'hidden',
// )
// or NULL if 'body' is hidden

EntityFormDisplay

Formatters

D7: "hooks" (well, magic callbacks)


        
    

D8: FieldFormatter plugins


        
    

D8: FieldFormatter plugin type

/core/modules/number/lib/Drupal/number/Plugin/Field/FieldFormatter/NumberUnformattedFormatter.php


    

/core/lib/Drupal/Core/Field/Annotation/FieldFormatter.php


    

/core/modules/link/lib/Drupal/link/Plugin/Field/FieldFormatter/LinkFormatter.php


    

Inheritance example

Working in formatters

Widgets

FieldWidget plugin type


        
    

/core/modules/email/lib/Drupal/email/Plugin/Field/FieldWidget/EmailDefaultWidget.php


    

Working in widgets

Widgets / formatters on base fields

/core/modules/node/lib/Drupal/node/Entity/Node.php


    

Field types

D7: field type "hooks"


    

FieldType plugin type

Data model - D8

FieldType plugins are the objects holding field values

FieldItemInterface At some point during 8.x


        

    

FieldItemInterface Now in 8.x !


    

/core/modules/link/lib/Drupal/link/Plugin/Field/FieldType/LinkItem.php


    

Dedicated FieldItemList classes

Put logic at the level of the list of "multiple values".

/core/modules/file/lib/Drupal/file/Plugin/Field/FieldType/FileFieldItemList.php


    

Computed properties

Properties on a FieldItem that are: $entity->field_text->processed
$entity->field_ref->entity->field_foo->value
$entity->field_taxo->parent->field_foo->value
Requires providing a class that specifies the behavior of the property

API changes done at Drupal Dev Days

Api changes / beta targets coming up (maybe during dev days)

Get involved

Thanks!

Questions?