Field API
From D7 to D8
Pavel Makhrinsky
About
Pavel Makhrinsky
Scrum master/Senior Drupal developer
Berlingske Media
History
CCK1 → Drupal 5, Drupal 6
CCK2, CCK3 → Drupal 5, Drupal 6
Drupal 7 → Field API
Drupal 8 → Entity Field API
D8 changes overview
Fields are
- base fields (properties)
- configurable fields (entities)
Fields are classes with interfa...
Moving field across
instances
Typed Data API
Typed Data: definition
/core/lib/Drupal/Core/Entity/Plugin/DataType/EmailItem.php
class EmailItem extends LegacyConfigFiel...
Typed Data: implementation
/**
* Plugin implementation of the 'email' field type.
* @FieldType(
* id = "email",
* label = ...
Default field types
File
Image
List
Number
Text
File
Image
List
Number
Text
EntityReference
Date and Time
Link
E-mail
Phone
EntityReference
Date and Time
Link, E-mail, Phone
New, best ever, widget
Validation API
Validators: D7
FAPI validation (https://drupal.org/project/fapi_validation)
Client side validation (https://drupal.org/pro...
Validators: D8
$definition = array(
'type' => 'integer',
'constraints' => array(
'Range' => array('min' => 5),
),
);
$type...
Accessing Field Data
Drupal 7
// Entities are complex, they contain other pieces of data.
$entity;
// Fields are not complex, they only contain...
Drupal 8: under the hood
$entity instanceof EntityInterface;
$entity->get('image') instanceof FieldInterface;
$entity->get...
Drupal 8
// With magic added by the Entity API.
$string = $entity->image[0]->alt;
// With more magic added by Entity API, ...
Language handling
@see Katya Marshalkina, 16:00
Implementing Formatter
Drupal 7
function hook_field_formatter_info()
function hook_field_formatter_settings_form()
function hook_field_formatter_...
Defining dependencies
// Field formatter annotation class.
use DrupalfieldAnnotationFieldFormatter;
// Annotation translat...
Defining formatter info
/**
* @FieldFormatter(
* id = "foo_formatter",
* label = @Translation("Foo formatter"),
* field_ty...
Adding settings
public function settingsForm(array $form, array &$form_state) {
$element = array();
$element['trim_length'...
Adding summary
public function settingsSummary() {
$summary = array();
$summary[] = t('Trim length: @trim_length',
array('...
Settings form
Field name Formatter name Summary
Alter formatter
function foo_field_formatter_settings_summary_alter(&$summary,
$context) {
if ($context['formatter']->getP...
Implementing Widget
Drupal 7
hook_field_widget_info()
hook_field_widget_settings_form()
hook_field_widget_form()
hook_field_widget_error()
Widget implementation
Annotation
WidgetInterface::settingsForm()
WidgetInterface::formElement()
WidgetInterface::errorElem...
Useful links
Original initiative
http://entity.worldempire.ch/
Plugin API
https://drupal.org/node/1637614
Field API series...
Questions?
Upcoming SlideShare
Loading in...5
×

Field api.From d7 to d8

456

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
456
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Field api.From d7 to d8

  1. 1. Field API From D7 to D8 Pavel Makhrinsky
  2. 2. About Pavel Makhrinsky Scrum master/Senior Drupal developer Berlingske Media
  3. 3. History CCK1 → Drupal 5, Drupal 6 CCK2, CCK3 → Drupal 5, Drupal 6 Drupal 7 → Field API Drupal 8 → Entity Field API
  4. 4. D8 changes overview Fields are - base fields (properties) - configurable fields (entities) Fields are classes with interfaces Configurable fields are separate kind of entity
  5. 5. Moving field across instances
  6. 6. Typed Data API
  7. 7. Typed Data: definition /core/lib/Drupal/Core/Entity/Plugin/DataType/EmailItem.php class 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; } }
  8. 8. 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(); …. } }
  9. 9. Default field types File Image List Number Text File Image List Number Text EntityReference Date and Time Link E-mail Phone
  10. 10. EntityReference
  11. 11. Date and Time Link, E-mail, Phone
  12. 12. New, best ever, widget
  13. 13. Validation API
  14. 14. Validators: D7 FAPI 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') );
  15. 15. 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().
  16. 16. Accessing Field Data
  17. 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. 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. 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;
  20. 20. Language handling @see Katya Marshalkina, 16:00
  21. 21. Implementing Formatter
  22. 22. Drupal 7 function hook_field_formatter_info() function hook_field_formatter_settings_form() function hook_field_formatter_settings_summary() function hook_field_formatter_view()
  23. 23. Defining dependencies // Field formatter annotation class. use DrupalfieldAnnotationFieldFormatter; // Annotation translation class. use DrupalCoreAnnotationTranslation; // FormatterBase class. use DrupalfieldPluginTypeFormatterFormatterBase; // Entityinterface. use DrupalCoreEntityEntityInterface; // Fieldinterface use DrupalCoreEntityFieldFieldInterface;
  24. 24. Defining formatter info /** * @FieldFormatter( * id = "foo_formatter", * label = @Translation("Foo formatter"), * field_types = { * "text", * "text_long" * }, * settings = { * "trim_length" = "600", * }, * ) */ class FooFormatter extends FormatterBase { }
  25. 25. Adding settings public 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; }
  26. 26. Adding summary public function settingsSummary() { $summary = array(); $summary[] = t('Trim length: @trim_length', array('@trim_length' => $this->getSetting('trim_length')) ); return $summary; }
  27. 27. Settings form Field name Formatter name Summary
  28. 28. Alter formatter function foo_field_formatter_settings_summary_alter(&$summary, $context) { if ($context['formatter']->getPluginId() == 'foo_formatter') { if ($context['formatter']->getSetting('mysetting')) { $summary[] = t('My setting enabled.'); } } }
  29. 29. Implementing Widget
  30. 30. Drupal 7 hook_field_widget_info() hook_field_widget_settings_form() hook_field_widget_form() hook_field_widget_error()
  31. 31. Widget implementation Annotation WidgetInterface::settingsForm() WidgetInterface::formElement() WidgetInterface::errorElement() WidgetInterface::settingsSummary()
  32. 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
  33. 33. Questions?
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×