SlideShare ist ein Scribd-Unternehmen logo
1 von 33
Downloaden Sie, um offline zu lesen
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?

Weitere ähnliche Inhalte

Was ist angesagt?

Doctrine fixtures
Doctrine fixturesDoctrine fixtures
Doctrine fixtures
Bill Chang
 
Design Patterns in PHP5
Design Patterns in PHP5 Design Patterns in PHP5
Design Patterns in PHP5
Wildan Maulana
 
international PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secretsinternational PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secrets
smueller_sandsmedia
 

Was ist angesagt? (20)

Modularity and Layered Data Model
Modularity and Layered Data ModelModularity and Layered Data Model
Modularity and Layered Data Model
 
PHP Data Objects
PHP Data ObjectsPHP Data Objects
PHP Data Objects
 
Drupal 8 Sample Module
Drupal 8 Sample ModuleDrupal 8 Sample Module
Drupal 8 Sample Module
 
Lithium Best
Lithium Best Lithium Best
Lithium Best
 
Doctrine fixtures
Doctrine fixturesDoctrine fixtures
Doctrine fixtures
 
Drupal 8: Routing & More
Drupal 8: Routing & MoreDrupal 8: Routing & More
Drupal 8: Routing & More
 
Doctrine for NoSQL
Doctrine for NoSQLDoctrine for NoSQL
Doctrine for NoSQL
 
Doctrine and NoSQL
Doctrine and NoSQLDoctrine and NoSQL
Doctrine and NoSQL
 
The Origin of Lithium
The Origin of LithiumThe Origin of Lithium
The Origin of Lithium
 
Entities in drupal 7
Entities in drupal 7Entities in drupal 7
Entities in drupal 7
 
Building Lithium Apps
Building Lithium AppsBuilding Lithium Apps
Building Lithium Apps
 
Drupal 8 migrate!
Drupal 8 migrate!Drupal 8 migrate!
Drupal 8 migrate!
 
Rich domain model with symfony 2.5 and doctrine 2.5
Rich domain model with symfony 2.5 and doctrine 2.5Rich domain model with symfony 2.5 and doctrine 2.5
Rich domain model with symfony 2.5 and doctrine 2.5
 
Design Patterns in PHP5
Design Patterns in PHP5 Design Patterns in PHP5
Design Patterns in PHP5
 
Advanced Querying with CakePHP 3
Advanced Querying with CakePHP 3Advanced Querying with CakePHP 3
Advanced Querying with CakePHP 3
 
international PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secretsinternational PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secrets
 
Php unit the-mostunknownparts
Php unit the-mostunknownpartsPhp unit the-mostunknownparts
Php unit the-mostunknownparts
 
Quebec pdo
Quebec pdoQuebec pdo
Quebec pdo
 
Drupal II: The SQL
Drupal II: The SQLDrupal II: The SQL
Drupal II: The SQL
 
Top Ten Reasons to Use EntityFieldQuery in Drupal
Top Ten Reasons to Use EntityFieldQuery in DrupalTop Ten Reasons to Use EntityFieldQuery in Drupal
Top Ten Reasons to Use EntityFieldQuery in Drupal
 

Ähnlich wie Field api.From d7 to d8

Models Best Practices (ZF MVC)
Models Best Practices (ZF MVC)Models Best Practices (ZF MVC)
Models Best Practices (ZF MVC)
eddiejaoude
 
The Magic Revealed: Four Real-World Examples of Using the Client Object Model...
The Magic Revealed: Four Real-World Examples of Using the Client Object Model...The Magic Revealed: Four Real-World Examples of Using the Client Object Model...
The Magic Revealed: Four Real-World Examples of Using the Client Object Model...
SPTechCon
 
YiiConf 2012 - Alexander Makarov - Yii2, what's new
YiiConf 2012 - Alexander Makarov - Yii2, what's newYiiConf 2012 - Alexander Makarov - Yii2, what's new
YiiConf 2012 - Alexander Makarov - Yii2, what's new
Alexander Makarov
 

Ähnlich wie Field api.From d7 to d8 (20)

Your Entity, Your Code
Your Entity, Your CodeYour Entity, Your Code
Your Entity, Your Code
 
Your Entity, Your Code
Your Entity, Your CodeYour Entity, Your Code
Your Entity, Your Code
 
Drupal 7 Entity & Entity API
Drupal 7 Entity & Entity APIDrupal 7 Entity & Entity API
Drupal 7 Entity & Entity API
 
Drupal 8: Fields reborn
Drupal 8: Fields rebornDrupal 8: Fields reborn
Drupal 8: Fields reborn
 
Models Best Practices (ZF MVC)
Models Best Practices (ZF MVC)Models Best Practices (ZF MVC)
Models Best Practices (ZF MVC)
 
DrupalJam 2018 - Maintaining a Drupal Module: Keep It Small and Simple
DrupalJam 2018 - Maintaining a Drupal Module: Keep It Small and SimpleDrupalJam 2018 - Maintaining a Drupal Module: Keep It Small and Simple
DrupalJam 2018 - Maintaining a Drupal Module: Keep It Small and Simple
 
Drupal 8 Hooks
Drupal 8 HooksDrupal 8 Hooks
Drupal 8 Hooks
 
Typed data in drupal 8
Typed data in drupal 8Typed data in drupal 8
Typed data in drupal 8
 
Drupal 8 Services
Drupal 8 ServicesDrupal 8 Services
Drupal 8 Services
 
Entity api
Entity apiEntity api
Entity api
 
Drupal 7 entities & TextbookMadness.com
Drupal 7 entities & TextbookMadness.comDrupal 7 entities & TextbookMadness.com
Drupal 7 entities & TextbookMadness.com
 
Migrare da symfony 1 a Symfony2
 Migrare da symfony 1 a Symfony2  Migrare da symfony 1 a Symfony2
Migrare da symfony 1 a Symfony2
 
Introducing CakeEntity
Introducing CakeEntityIntroducing CakeEntity
Introducing CakeEntity
 
Mikhail Kraynuk. Form api. Drupal 8
Mikhail Kraynuk. Form api. Drupal 8Mikhail Kraynuk. Form api. Drupal 8
Mikhail Kraynuk. Form api. Drupal 8
 
Mikhail Kraynuk. Form api. Drupal 8
Mikhail Kraynuk. Form api. Drupal 8Mikhail Kraynuk. Form api. Drupal 8
Mikhail Kraynuk. Form api. Drupal 8
 
The state of hooking into Drupal - DrupalCon Dublin
The state of hooking into Drupal - DrupalCon DublinThe state of hooking into Drupal - DrupalCon Dublin
The state of hooking into Drupal - DrupalCon Dublin
 
The Magic Revealed: Four Real-World Examples of Using the Client Object Model...
The Magic Revealed: Four Real-World Examples of Using the Client Object Model...The Magic Revealed: Four Real-World Examples of Using the Client Object Model...
The Magic Revealed: Four Real-World Examples of Using the Client Object Model...
 
How Kris Writes Symfony Apps
How Kris Writes Symfony AppsHow Kris Writes Symfony Apps
How Kris Writes Symfony Apps
 
What's new in the Drupal 7 API?
What's new in the Drupal 7 API?What's new in the Drupal 7 API?
What's new in the Drupal 7 API?
 
YiiConf 2012 - Alexander Makarov - Yii2, what's new
YiiConf 2012 - Alexander Makarov - Yii2, what's newYiiConf 2012 - Alexander Makarov - Yii2, what's new
YiiConf 2012 - Alexander Makarov - Yii2, what's new
 

Kürzlich hochgeladen

Kürzlich hochgeladen (20)

From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 

Field api.From d7 to d8