Your SlideShare is downloading. ×
0
Field api.From d7 to d8
Field api.From d7 to d8
Field api.From d7 to d8
Field api.From d7 to d8
Field api.From d7 to d8
Field api.From d7 to d8
Field api.From d7 to d8
Field api.From d7 to d8
Field api.From d7 to d8
Field api.From d7 to d8
Field api.From d7 to d8
Field api.From d7 to d8
Field api.From d7 to d8
Field api.From d7 to d8
Field api.From d7 to d8
Field api.From d7 to d8
Field api.From d7 to d8
Field api.From d7 to d8
Field api.From d7 to d8
Field api.From d7 to d8
Field api.From d7 to d8
Field api.From d7 to d8
Field api.From d7 to d8
Field api.From d7 to d8
Field api.From d7 to d8
Field api.From d7 to d8
Field api.From d7 to d8
Field api.From d7 to d8
Field api.From d7 to d8
Field api.From d7 to d8
Field api.From d7 to d8
Field api.From d7 to d8
Field api.From d7 to d8
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Field api.From d7 to d8

407

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
407
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Field API From D7 to D8 Pavel Makhrinsky
  • 2. About Pavel Makhrinsky Scrum master/Senior Drupal developer Berlingske Media
  • 3. History CCK1 → Drupal 5, Drupal 6 CCK2, CCK3 → Drupal 5, Drupal 6 Drupal 7 → Field API Drupal 8 → Entity Field API
  • 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. Moving field across instances
  • 6. Typed Data API
  • 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. 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. Default field types File Image List Number Text File Image List Number Text EntityReference Date and Time Link E-mail Phone
  • 10. EntityReference
  • 11. Date and Time Link, E-mail, Phone
  • 12. New, best ever, widget
  • 13. Validation API
  • 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. 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. Accessing Field Data
  • 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. 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. 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. Language handling @see Katya Marshalkina, 16:00
  • 21. Implementing Formatter
  • 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. Defining dependencies // Field formatter annotation class. use DrupalfieldAnnotationFieldFormatter; // Annotation translation class. use DrupalCoreAnnotationTranslation; // FormatterBase class. use DrupalfieldPluginTypeFormatterFormatterBase; // Entityinterface. use DrupalCoreEntityEntityInterface; // Fieldinterface use DrupalCoreEntityFieldFieldInterface;
  • 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. 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. Adding summary public function settingsSummary() { $summary = array(); $summary[] = t('Trim length: @trim_length', array('@trim_length' => $this->getSetting('trim_length')) ); return $summary; }
  • 27. Settings form Field name Formatter name Summary
  • 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. Implementing Widget
  • 30. Drupal 7 hook_field_widget_info() hook_field_widget_settings_form() hook_field_widget_form() hook_field_widget_error()
  • 31. Widget implementation Annotation WidgetInterface::settingsForm() WidgetInterface::formElement() WidgetInterface::errorElement() WidgetInterface::settingsSummary()
  • 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. Questions?

×