View
14.173
Download
2
Category
Preview:
Citation preview
1
D8: Entities and Fields
Alex Bronstein
2
About Me!!
• Alex Bronstein
3
What's an entity?!
• Lifecycle: create, save, load, delete
• Identity: id, uuid, uri
• Type: entityType, PHP class
4
37 core entity types!
Action Block Breakpoint BreakpointGroup (Contact)Category Comment CustomBlock CustomBlockType DateFormat Editor
EntityDisplay EntityFormDisplay EntityFormMode EntityViewMode Feed Field FieldInstance File FilterFormat ImageStyle
(Aggregator)Item Language Menu MenuLink Message Migration Node NodeType PictureMapping RdfMapping
Role ShortcutSet Term Tour User View Vocabulary
5
Compared to only 6 in Drupal 7!Action Block Breakpoint BreakpointGroup (Contact)Category Comment CustomBlock CustomBlockType DateFormat Editor
EntityDisplay EntityFormDisplay EntityFormMode EntityViewMode Feed Field FieldInstance File FilterFormat ImageStyle
(Aggregator)Item Language Menu MenuLink Message Migration Node NodeType PictureMapping RdfMapping
Role ShortcutSet Term Tour User View Vocabulary
6
11 due to new features!Action Block Breakpoint BreakpointGroup (Contact)Category Comment CustomBlock CustomBlockType DateFormat Editor
EntityDisplay EntityFormDisplay EntityFormMode EntityViewMode Feed Field FieldInstance File FilterFormat ImageStyle
(Aggregator)Item Language Menu MenuLink Message Migration Node NodeType PictureMapping RdfMapping
Role ShortcutSet Term Tour User View Vocabulary
7
Action Block Breakpoint BreakpointGroup (Contact)Category Comment CustomBlock CustomBlockType DateFormat Editor
EntityDisplay EntityFormDisplay EntityFormMode EntityViewMode Feed Field FieldInstance File FilterFormat ImageStyle
(Aggregator)Item Language Menu MenuLink Message Migration Node NodeType PictureMapping RdfMapping
Role ShortcutSet Term Tour User View Vocabulary
20 due to entifying existing features!
8
D8 has configuration management!!!!Configuration • Stored in YAML files, not database • Canonical workflow: change on dev, deploy to prod
Content • Stored in database • Canonical workflow: changes on prod constantly
• Drupal is not just a Content Management System; it's a Content and Configuration Management System :)
9
Two families of entity types!
Comment CustomBlock Feed File (Aggregator)Item MenuLink Message* Node Term User
Action Block Breakpoint BreakpointGroup (Contact)Category CustomBlockType DateFormat Editor EntityDisplay EntityFormDisplay
EntityFormMode EntityViewMode Field FieldInstance FilterFormat ImageStyle Language Menu Migration NodeType
PictureMapping RdfMapping Role ShortcutSet Tour View Vocabulary
Content Configuration
10
What's not an entity?!• Singular config (settings)
• Keyvalue (state, tempstore)
• Non-kv state (batch, config_snapshot, flood, queue, semaphore, sequences)
• Session (sessions)
• Special caches (locale_*, search_*, forum_index)
• History/statistics (comment_entity_statistics, history, node_counter, tracker_*)
• Log (watchdog)
• Other (ban_ip, book, content_translation, file_usage, forum, node_access, url_alias)
11
Entity lifecycle!// Create, Save, Load, Delete $entity = entity_create($type, $values); $entity->DOSTUFF(); $entity->save(); $entity = entity_load($type, $id); $entity->DOSTUFF(); $entity->save(); $entity->delete();
// Also $entities = entity_load_multiple_by_properties($type, $values)
12
Entity lifecycle hooks!hook_entity_create() hook_entity_presave() hook_entity_insert() hook_entity_load() hook_entity_presave() hook_entity_update() hook_entity_predelete() hook_entity_delete()
hook_TYPE_create() hook_TYPE_presave() hook_TYPE_insert() hook_TYPE_load() hook_TYPE_presave() hook_TYPE_update() hook_TYPE_predelete() hook_TYPE_delete()
Other entity hooks • Deal with access, view, revision, translation, bundle, ... • See entity.api.php
13
Two families of entity types!
Comment CustomBlock Feed File (Aggregator)Item MenuLink Message* Node Term User
Action Block Breakpoint BreakpointGroup (Contact)Category CustomBlockType DateFormat Editor EntityDisplay EntityFormDisplay
EntityFormMode EntityViewMode Field FieldInstance FilterFormat ImageStyle Language Menu Migration NodeType
PictureMapping RdfMapping Role ShortcutSet Tour View Vocabulary
Content Configuration
14
Two families of entity types!Content • revisionable • uses content translation API • fieldable • all data is a field and follows the
field data model • integrated with REST • ID is autogenerated, stable, non-
recyclable
Config • not revisionable (use git)
• uses config translation API • not fieldable • data is PHP scalars/arrays and
follows a type-defined schema • not integrated with REST (contrib?)
• ID is a machine name that can be edited and recycled
15
Let's see some code!• Anemic config entity type: DateFormat • Rich config entity type: ImageStyle • Content entity type: Node
16
Content entity data model!• EVERYTHING is a field • May or may not allow non-base fields • 3 kinds of fields:
o Base field = entity class defines it and can therefore provide accessor methods ($node->title)
o Configurable field = administrator can remove it either
via UI or via YAML ($node->body) o Custom? field = other module defines it but it is not
administrator configurable ($node->path)
17
Accessing a field item property!D7: $node->title $node->body[$langcode][0]['value']
D8: $node->title->value $node->body->value
OR
$node->getTitle() $node->get('body')->value
18
What about $langcode and $delta?!
$node->getTranslation($langcode)->field_tags[$delta]->target_id
OR
$node->getTranslation($langcode)->get('field_tags')->offsetGet($delta)->target_id
19
Code example!// D7 if (trim($comment->subject) == '') { $field = field_info_field('comment_body'); $langcode =
field_is_translatable('comment', $field) ? entity_language('comment', $comment) : LANGUAGE_NONE;
$comment_body = $comment->comment_body[$langcode][0];
$comment_text = check_markup($comment_body['value'], $comment_body['format']);
$comment->subject = truncate_utf8(trim(decode_entities(strip_tags($comment_text))), 29, TRUE);
}
// D8 if (trim($comment->subject->value)
== '') { // @todo Change to $comment->getSubject()
$comment_text = $comment->comment_body->processed;
// @todo Change to $comment->setSubject()
$comment->subject->value = Unicode::truncate(trim(String::decodeEntities(strip_tags($comment_text))), 29, TRUE);
}
20
Want to learn more?!• DrupalCamp Vienna sessions:
o Entity API: http://2013.drupalcamp.at/session/drupal-8-entity-apihttp://www.youtube.com/watch?v=1PYxHOfx6TU
o Field API: http://2013.drupalcamp.at/session/drupal-8-field-api-fields-rebornhttp://www.youtube.com/watch?v=WLGwEuU9GVw
o Entity Translation:http://2013.drupalcamp.at/session/multilingual-content-d8-highly-evolved-permutated-api
• In-progress documentation: https://drupal.org/developing/api/entity
21
Are you D8 ready?"Learn how-to!
22
D8 Readiness Resources!• D8 Blogs – With “how-to” topics ranging from “This week in Core”, OOP in D8, Migration, Spark and much more! http://www.acquia.com/blog/drupal-8 • D8 Webinars – Learn more about Multi-lingual, OOP, What to expect
etc in D8 - http://www.acquia.com/resources/recorded_webinars • D8 Live Panel Discussions with Google Hangouts –
http://www.acquia.com/resources/podcasts/acquia-podcast-93-power-php-meet-michelangelo-van-dam
• D8 Updates – Get the latest and the greatest in D8 on our developer page –
http://www.acquia.com/drupal-developers
23
Thanks!"Questions???
Recommended