Drupal 8 Preview: What You Need to Know about Entity and Field APIs
Upcoming SlideShare
Loading in...5
×
 

Drupal 8 Preview: What You Need to Know about Entity and Field APIs

on

  • 4,122 views

 

Statistics

Views

Total Views
4,122
Views on SlideShare
3,802
Embed Views
320

Actions

Likes
5
Downloads
40
Comments
0

4 Embeds 320

https://www.acquia.com 299
http://devs.dream-portal.net 18
http://4room.hue 2
http://www.acquia.com 1

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

Drupal 8 Preview: What You Need to Know about Entity and Field APIs Drupal 8 Preview: What You Need to Know about Entity and Field APIs Presentation Transcript

  • D8: Entities and Fields Alex Bronstein 1
  • About Me!! •  Alex Bronstein 2
  • What's an entity?! • Lifecycle: create, save, load, delete • Identity: id, uuid, uri • Type: entityType, PHP class 3
  • 37 core entity types! Action Block Breakpoint BreakpointGroup (Contact)Category Comment CustomBlock CustomBlockType DateFormat Editor 4 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
  • Compared to only 6 in Drupal 7! Action Block Breakpoint BreakpointGroup (Contact)Category Comment CustomBlock CustomBlockType DateFormat Editor 5 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
  • 11 due to new features! Action Block Breakpoint BreakpointGroup (Contact)Category Comment CustomBlock CustomBlockType DateFormat Editor 6 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! Action Block Breakpoint BreakpointGroup (Contact)Category Comment CustomBlock CustomBlockType DateFormat Editor 7 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
  • 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 :) 8
  • Two families of entity types! Content Configuration Comment CustomBlock Feed File (Aggregator)Item MenuLink Message* Node Term User Action Block Breakpoint BreakpointGroup (Contact)Category CustomBlockType DateFormat Editor EntityDisplay EntityFormDisplay 9 EntityFormMode EntityViewMode Field FieldInstance FilterFormat ImageStyle Language Menu Migration NodeType PictureMapping RdfMapping Role ShortcutSet Tour View Vocabulary
  • What's not an entity?! •  Singular config •  Keyvalue •  Non-kv state •  Session •  Special caches •  History/statistics •  Log •  Other (settings) (state, tempstore) (batch, config_snapshot, flood, queue, semaphore, sequences) (sessions) (locale_*, search_*, forum_index) (comment_entity_statistics, history, node_counter, tracker_*) (watchdog) (ban_ip, book, content_translation, file_usage, forum, node_access, url_alias) 10
  • 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) 11
  • 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 •  •  12 Deal with access, view, revision, translation, bundle, ... See entity.api.php
  • Two families of entity types! Content Configuration Comment CustomBlock Feed File (Aggregator)Item MenuLink Message* Node Term User Action Block Breakpoint BreakpointGroup (Contact)Category CustomBlockType DateFormat Editor EntityDisplay EntityFormDisplay 13 EntityFormMode EntityViewMode Field FieldInstance FilterFormat ImageStyle Language Menu Migration NodeType PictureMapping RdfMapping Role ShortcutSet Tour View Vocabulary
  • Two families of entity types! Content •  •  •  •  •  •  14 revisionable uses content translation API fieldable all data is a field and follows the field data model integrated with REST ID is autogenerated, stable, nonrecyclable 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
  • Let's see some code! •  Anemic config entity type: DateFormat •  Rich config entity type: ImageStyle •  Content entity type: Node 15
  • Content entity data model! •  EVERYTHING is a field •  May or may not allow non-base fields •  3 kinds of fields: o  o  o  16 Base field = entity class defines it and can therefore provide accessor methods ($node->title) Configurable field = administrator can remove it either via UI or via YAML ($node->body) Custom? field = other module defines it but it is not administrator configurable ($node->path)
  • Accessing a field item property! D7: D8: $node->title $node->body[$langcode][0]['value'] $node->title->value $node->body->value OR $node->getTitle() $node->get('body')->value 17
  • What about $langcode and $delta?! $node->getTranslation($langcode)>field_tags[$delta]->target_id OR $node->getTranslation($langcode)>get('field_tags')->offsetGet($delta)>target_id 18
  • Code example! // D7 // D8 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); } 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::dec odeEntities(strip_tags($comment_ text))), 29, TRUE); } 19
  • Want to learn more?! •  •  20 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-highlyevolved-permutated-api In-progress documentation: https://drupal.org/developing/api/entity
  • Are you D8 ready?" Learn how-to! 21
  • 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-phpmeet-michelangelo-van-dam •  D8 Updates – Get the latest and the greatest in D8 on our developer page – http://www.acquia.com/drupal-developers 22
  • Thanks!" Questions??? 23