+ All Categories
Home > Technology > Field api.From d7 to d8

Field api.From d7 to d8

Date post: 08-May-2015
Category:
Upload: pavel-makhrinsky
View: 421 times
Download: 0 times
Share this document with a friend
33
Field API From D7 to D8 Pavel Makhrinsky
Transcript
Page 1: Field api.From d7 to d8

Field APIFrom D7 to D8

Pavel Makhrinsky

Page 2: Field api.From d7 to d8

About

Pavel MakhrinskyScrum master/Senior Drupal developerBerlingske Media

Page 3: Field api.From d7 to d8

History

CCK1 → Drupal 5, Drupal 6CCK2, CCK3 → Drupal 5, Drupal 6Drupal 7 → Field APIDrupal 8 → Entity Field API

Page 4: Field api.From d7 to d8

D8 changes overview

Fields are- base fields (properties)- configurable fields (entities)Fields are classes with interfacesConfigurable fields are separate kind of entity

Page 5: Field api.From d7 to d8

Moving field across instances

Page 6: Field api.From d7 to d8

Typed Data API

Page 7: Field api.From d7 to d8

Typed Data: definition/core/lib/Drupal/Core/Entity/Plugin/DataType/EmailItem.phpclass EmailItem extends LegacyConfigFieldItem { static $propertyDefinitions;

public function getPropertyDefinitions() {

if (!isset(static::$propertyDefinitions)) { static::$propertyDefinitions['value'] = array( 'type' => 'email', 'label' => t('E-mail value'), ); } return static::$propertyDefinitions; }}

Page 8: Field api.From d7 to d8

Typed Data: implementation /** * Plugin implementation of the 'email' field type. * @FieldType( * id = "email", * label = @Translation("E-mail"), * default_widget = "email_default", * ) */class ConfigurableEmailItem extends EmailItem { public static function schema(FieldInterface $field) { return array( 'columns' => array(… }

public function getConstraints() { $constraint_manager = \Drupal::typedData()->getValidationConstraintManager(); …. }}

Page 9: Field api.From d7 to d8

Default field types

FileImageListNumberText

FileImageListNumberTextEntityReferenceDate and TimeLinkE-mailPhone

Page 10: Field api.From d7 to d8

EntityReference

Page 11: Field api.From d7 to d8

Date and TimeLink, E-mail, Phone

Page 12: Field api.From d7 to d8

New, best ever, widget

Page 13: Field api.From d7 to d8

Validation API

Page 14: Field api.From d7 to d8

Validators: D7FAPI validation (https://drupal.org/project/fapi_validation)Client side validation (https://drupal.org/project/clientside_validation)$form['myfield'] = array( '#type' => 'textfield', '#title' => 'My Field', '#required' => TRUE, '#rules' => array(

'email','length[10, 50]',array('rule' => 'alpha_numeric', 'error' => 'Please, use only

alpha numeric characters at %field.'),array('rule' => 'match_field[otherfield]', 'error callback' =>

'mymodule_validation_error_msg'), ), '#filters' => array('trim', 'uppercase'));

Page 15: Field api.From d7 to d8

Validators: D8$definition = array( 'type' => 'integer', 'constraints' => array( 'Range' => array('min' => 5), ),);$typed_data = \Drupal::typedData()->create($definition, 10);$violations = $typed_data->validate();

Get applied constrains$typed_data->getConstraints().

Page 16: Field api.From d7 to d8

Accessing Field Data

Page 17: Field api.From d7 to d8

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’];

Page 18: Field api.From d7 to d8

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());

Page 19: Field api.From d7 to d8

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;

Page 20: Field api.From d7 to d8

Language handling@see Katya Marshalkina, 16:00

Page 21: Field api.From d7 to d8

Implementing Formatter

Page 22: Field api.From d7 to d8

Drupal 7function hook_field_formatter_info()function hook_field_formatter_settings_form()function hook_field_formatter_settings_summary()function hook_field_formatter_view()

Page 23: Field api.From d7 to d8

Defining dependencies// Field formatter annotation class.use Drupal\field\Annotation\FieldFormatter;// Annotation translation class.use Drupal\Core\Annotation\Translation;// FormatterBase class.use Drupal\field\Plugin\Type\Formatter\FormatterBase;// Entityinterface.use Drupal\Core\Entity\EntityInterface;// Fieldinterfaceuse Drupal\Core\Entity\Field\FieldInterface;

Page 24: Field api.From d7 to d8

Defining formatter info/*** @FieldFormatter(* id = "foo_formatter",* label = @Translation("Foo formatter"),* field_types = {* "text",* "text_long"* },* settings = {* "trim_length" = "600",* },* )*/class FooFormatter extends FormatterBase { }

Page 25: Field api.From d7 to d8

Adding settingspublic function settingsForm(array $form, array &$form_state) { $element = array(); $element['trim_length'] = array( '#title' => t('Trim length'), '#type' => 'number', '#default_value' => $this->getSetting('trim_length'), '#min' => 1, '#required' => TRUE,

); return $element; }

Page 26: Field api.From d7 to d8

Adding summarypublic function settingsSummary() { $summary = array(); $summary[] = t('Trim length: @trim_length', array('@trim_length' => $this->getSetting('trim_length')) ); return $summary; }

Page 27: Field api.From d7 to d8

Settings form

Field name Formatter name Summary

Page 28: Field api.From d7 to d8

Alter formatterfunction foo_field_formatter_settings_summary_alter(&$summary, $context) { if ($context['formatter']->getPluginId() == 'foo_formatter') { if ($context['formatter']->getSetting('mysetting')) { $summary[] = t('My setting enabled.'); } }}

Page 29: Field api.From d7 to d8

Implementing Widget

Page 30: Field api.From d7 to d8

Drupal 7

hook_field_widget_info()hook_field_widget_settings_form()hook_field_widget_form()hook_field_widget_error()

Page 31: Field api.From d7 to d8

Widget implementation

AnnotationWidgetInterface::settingsForm()WidgetInterface::formElement()WidgetInterface::errorElement()WidgetInterface::settingsSummary()

Page 32: Field api.From d7 to d8

Useful linksOriginal initiativehttp://entity.worldempire.ch/Plugin APIhttps://drupal.org/node/1637614Field API series from Realizehttp://realize.be/drupal-8-field-api-series-part-1-field-formattershttp://realize.be/drupal-8-field-api-series-part-2-field-widgetsHow Entity API implements Typed Data API

https://drupal.org/node/1795854

Typed Data API

https://drupal.org/node/1794140

Page 33: Field api.From d7 to d8

Questions?


Recommended