D8: Fields reborn

Yves Chedemois - yched

D8: Fields reborn

Yves Chedemois - @yched

DrupalCamp Vienna
nov. 2013

Related sessions

Drupal 8 Entity API
Wolfgang Ziegler
was earlier today ;-)

Multilingual content in D8: A highly evolved permutated API
Francesco Placella
Saturday, 14:00 - room vi knallgrau

Site builing 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

Fields are tied to an entity type

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

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

http://www.flickr.com/photos/25792994@N04/5667529239

APIs !!!

There will be code

... for the 3rd time

swentel, amateescu,
fago, Berdir, plach,
effulgentsia, ...

gittip.com/for/drupal
www.drupalfund.us

CMI

CMI

Field definition structures

Conversion "only" took 1½ year

FrontEnd United - London, March 2013

D8: Field structures as ConfigEntities

entity type / class names: not exactly yet in current HEAD...

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

EntityDisplay (ConfigEntity)

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

entity.display.node.article.teaser.php


    

EntityDisplay 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

Data structures

Disclaimer: still in flux...

Data model recap

Data model - D7

Data model - D7

D8 Entity translation

Data model - D8

Data model - D8

Data model - D8

Data model - D8

Data model - D8

Data model - D8

Data model - D8

Data model - D8

In a nutshell

Navigating

$instances = field_info_instances($entity_type, $bundle);
foreach ($instances as $field_name => $instance) {
  $items = $entity[$field_name][$langcode];
  do_something($items, $entity, $langcode, $instance);
}
foreach ($entity as $field_name => $items) {
  $items->doSomething(); // or $object->doSomething($items);
}

Everything is a field

Everything

Everything in a ContentEntity is a field

Unified APIs and features

Naming is hard...

Different kinds of fields...

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

Code architecture

Well, we're getting there...

Unified FieldDefinitionInterface

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

Unified FieldDefinitionInterface


        
        Implementations:
            
  • configurable fields: FieldConfig, FieldInstanceConfig

Defining base fields

In the entity class (here Term.php):

    

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)

Still in flux...

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 ?

Work in progress...
UI impact not fully clear yet

Field types

D7: field type "hooks"


    

FieldType plugin type

Data model - D8

FieldType plugins are the objects holding field values

FieldItemInterface


        

    

FieldItemInterface (hopefully in 8.0...)


    

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


    

Dedicated FieldItemList classes

Dedicated FieldItemList classes

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

Dedicated FieldItemList classes

Specify a list_class in the FieldType definition

        

Note: ImageItem extends FileItem extends EntityReferenceItem :-)

/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

Field storage

Field storage (@todo, fingers crossed)

Problem: Plan:

Thanks!

Questions?