• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Field api.From d7 to d8
 

Field api.From d7 to d8

on

  • 420 views

 

Statistics

Views

Total Views
420
Views on SlideShare
420
Embed Views
0

Actions

Likes
0
Downloads
2
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Field api.From d7 to d8 Field api.From d7 to d8 Presentation Transcript

    • 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 interfaces Configurable fields are separate kind of entity
    • Moving field across instances
    • Typed Data API
    • 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; } }
    • 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(); …. } }
    • 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/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') );
    • 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().
    • Accessing Field Data
    • 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’];
    • 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());
    • 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;
    • 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_settings_summary() function hook_field_formatter_view()
    • Defining dependencies // Field formatter annotation class. use DrupalfieldAnnotationFieldFormatter; // Annotation translation class. use DrupalCoreAnnotationTranslation; // FormatterBase class. use DrupalfieldPluginTypeFormatterFormatterBase; // Entityinterface. use DrupalCoreEntityEntityInterface; // Fieldinterface use DrupalCoreEntityFieldFieldInterface;
    • Defining formatter info /** * @FieldFormatter( * id = "foo_formatter", * label = @Translation("Foo formatter"), * field_types = { * "text", * "text_long" * }, * settings = { * "trim_length" = "600", * }, * ) */ class FooFormatter extends FormatterBase { }
    • 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; }
    • Adding summary public function settingsSummary() { $summary = array(); $summary[] = t('Trim length: @trim_length', array('@trim_length' => $this->getSetting('trim_length')) ); return $summary; }
    • Settings form Field name Formatter name Summary
    • 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.'); } } }
    • 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::errorElement() WidgetInterface::settingsSummary()
    • 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
    • Questions?