Drupal Entities - Emerging Patterns of Usage

6,877 views

Published on

Entities are a powerful architectural approach and tool introduced in Drupal 7 - in this presentation I explain what they are and how they can be used with references to examples of how entities are used already.

Published in: Technology
0 Comments
16 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
6,877
On SlideShare
0
From Embeds
0
Number of Embeds
60
Actions
Shares
0
Downloads
59
Comments
0
Likes
16
Embeds 0
No embeds

No notes for slide

Drupal Entities - Emerging Patterns of Usage

  1. 1. Wednesday, August 24, 11
  2. 2. Code & Coders Entities - Emerging Patterns of Usage Presented by Ronald Ashri (ronald_istos)Wednesday, August 24, 11
  3. 3. @ronald_istos Web App Development Travel / Semantic Web www.istos.it Solutions for hotels, villas, B&Bs drupalrooms.com w/ Bluespark Labs Drupal Trainer network drupalteachers.com w/ BrightlemonWednesday, August 24, 11
  4. 4. Building with Entities ✤ How we got to entities ✤ What do they look like, what can we do, what’s missing ✤ When and how should we use them ✤ Learning from how they are used so far ✤ Questions / Next StepsWednesday, August 24, 11
  5. 5. slice up a drupal and you will find nodesWednesday, August 24, 11
  6. 6. drupalistas love making lots of nodes and mixing them up in different ways ... much like pastaWednesday, August 24, 11
  7. 7. they love them so much they nearly turned everything into a nodeWednesday, August 24, 11
  8. 8. user pro les = drupal.org/project/content_pro leWednesday, August 24, 11
  9. 9. taxonomy = drupal.org/project/taxonomy_nodeWednesday, August 24, 11
  10. 10. comments = drupal.org/project/comment_nodesWednesday, August 24, 11
  11. 11. nodes come in different shapes but still tied to the same methods and db structure => need for a more generic core-based solutionWednesday, August 24, 11
  12. 12. but the core development process is never simpleWednesday, August 24, 11
  13. 13. Huge driver turned out to be the Field API work - attaching elds to core “stuff” came rst and the need to streamline followedWednesday, August 24, 11
  14. 14. “loadable thingies, that can be optionally eldable” (http://drupal.org/node/460320)Wednesday, August 24, 11
  15. 15. entitiesWednesday, August 24, 11
  16. 16. Entities in Drupal 7 are the nodes we really wanted but didn’t know it yet The stuff / content that makes up our site nodes, users, taxonomy terms, comments, les User Node Taxonomy Term Comment Taxonomy File VocabularyWednesday, August 24, 11
  17. 17. one day drupal will have a yeah... sure smallcore and it will all be OO Drupal Core Developers Summit 2040 Entities bring us closer to Drupal the framework No mention in UI - almost no mention in changelogWednesday, August 24, 11
  18. 18. CHANGELOG.TXT... “In addition, any other object type may register with Field API and allow custom data elds to be attached to itself.” object type = entityWednesday, August 24, 11
  19. 19. The relationships between Entities - the ways entities can interact with each other - de ne our Drupal application nodes can have comments users author nodes taxonomy terms are attached to nodes these relationships are implicit - perhaps they could be made explicit?Wednesday, August 24, 11
  20. 20. how to cook - (or build entities)Wednesday, August 24, 11
  21. 21. Your Table PostIt postit_id name property1 property2 property3 + elds (drupal worries about elds)Wednesday, August 24, 11
  22. 22. you de ne an entity via a hook_entity_info (big array!): function postit_entity_info(){ $postit_info[postit] = array( label => t(PostIt Note), controller class => PostItController, base table => postit, uri callback => postit_uri, fieldable => TRUE, entity keys => array( id => pid, ), static cache => TRUE, bundles => array( postit=> array( label => PostIt, admin => array( path => admin/structure/postit/manage, access arguments => array(administer postits), ), ), ), view modes => array( full => array( label => t(Full PostIt), custom settings => FALSE, ), ) ); return $postit_info; }Wednesday, August 24, 11
  23. 23. function postit_entity_info(){ $postit_info[postit] = array( label => t(PostIt Note), controller class => PostItController, base table => postit, uri callback => postit_uri, fieldable => TRUE, You can provide you own controller class - typically entity keys => array( id => pid, ), extending the default DrupalDefaultEntityController static cache => TRUE, which worries about caching, querying, revisioning and bundles => array( postit=> array( attaching elds to entities label => PostIt, admin => array( path => admin/structure/postit/manage, access arguments => array(administer postits), ), ), ), view modes => array( full => array( label => t(Full PostIt), custom settings => FALSE, ), ) ); return $postit_info; }Wednesday, August 24, 11
  24. 24. function postit_entity_info(){ $postit_info[postit] = array( label => t(PostIt Note), controller class => PostItController, base table => postit, uri callback => postit_uri, fieldable => TRUE, entity keys => array( id => pid, ), static cache => TRUE, base table = where our main entity data lives bundles => array( uri callback = where to look up - view entities postit=> array( label => PostIt, eldable = are elds to be attached to our entity admin => array( path => admin/structure/postit/manage, access arguments => array(administer postits), ), ), ), view modes => array( full => array( label => t(Full PostIt), custom settings => FALSE, ), ) ); return $postit_info; }Wednesday, August 24, 11
  25. 25. function postit_entity_info(){ $postit_info[postit] = array( label => t(PostIt Note), controller class => PostItController, base table => postit, uri callback => postit_uri, fieldable => TRUE, entity keys => array( id => pid, ), static cache => TRUE, bundles => array( postit=> array( label => PostIt, admin => array( path => admin/structure/postit/manage, access arguments => array(administer postits), ), ), ), view modes => array( full => array( label => t(Full PostIt), Bundle = Entity + Con guration + Fields custom settings => FALSE, ), ) 1 entity (Node) can have many bundles (Article, Page) ); return $postit_info; }Wednesday, August 24, 11
  26. 26. tell drupal how to load your entity function postit_load($pid = NULL, $reset = FALSE){ $pids = (isset ($pid) ? array($pid) : array()); $postit = postit_load_multiple($pids, $reset); return $postit ? reset ($postit) : FALSE; } function postit_load_multiple($pids = array(), $conditions = array(), $reset = FALSE){ return entity_load(postit, $pids, $conditions, $reset); } function entity_load($entity_type, $ids = FALSE, $conditions = array(), $reset = FALSE) { if ($reset) { entity_get_controller($entity_type)->resetCache(); } return entity_get_controller($entity_type)->load($ids, $conditions); }Wednesday, August 24, 11
  27. 27. ready to cook! ✤ we also get a query API EntityFieldQuery ✤ conditions on properties and elds ✤ queries across entity types ✤ and we get hooks ✤ hook_entity_load() ✤ hook_entity_presave() ✤ hook_entity_insert() ✤ hook_entity_update() ✤ hook_entity_delete() ✤ hook_entity_prepare_view ✤ hook_entity_view()Wednesday, August 24, 11
  28. 28. Young module developer : Where is full CRUD, UI, Views integration, Tokens, Search, etc? Drupal Core: What? Am I supposed to do everything around here?Wednesday, August 24, 11
  29. 29. + Entity Module (drupal.org/project/entity) ✤ Supports full CRUD ✤ Quickly provide new entity types ✤ Standardised way of working with entity data ✤ Integration into drupal ecosystem (Tokens, Views, Rules, Exportables, Search API, RestWS, VBO) ✤ Full TestsWednesday, August 24, 11
  30. 30. so what are we cooking - and how? and why?Wednesday, August 24, 11
  31. 31. entities are a uniquely drupaly concept your stuff (your db table or something like that) + Field API + hooks in the drupal worldWednesday, August 24, 11
  32. 32. if your module / app is introducing data you should really be asking yourself why not via entities you get plugged in to the drupal world for (almost) zero cost and your data can remains as is (almost)Wednesday, August 24, 11
  33. 33. emerging patternsWednesday, August 24, 11
  34. 34. Commerce ✤ Great example as there is also historical perspective ✤ D6 Ubercart did almost everything “outside” of Drupal ✤ D7 Commerce is a fully signed up member to the Drupal ecosystemWednesday, August 24, 11
  35. 35. in order to add commerce capabilities we need to introduce a whole set of concepts (and their supporting data structures) Drupal Commerce de nes Customer Pro les Lines Items Orders Payment Transactions Products as entitiesWednesday, August 24, 11
  36. 36. Drupal Commerce defines only as much as it needs in terms of fields attached to these entities - Application core concepts (e.g. sku for product) live in entity base table not via field More complex core data (e.g. price) gets added via a locked field Allows the user add extra fields where it makes senseWednesday, August 24, 11
  37. 37. local action entity types Entity operational links Listing as a ViewWednesday, August 24, 11
  38. 38. Contains Line Order Entity Item Entities Related to payment entities address eld eld looks familiar!Wednesday, August 24, 11
  39. 39. use entities to introduce new data types that can be user-extensible provide minimum conf required - easily customisable in install profiles or via custom modules ps: did I mention - you really, really, really need a good reason not to use entities when introducing new data (and data tables) to DrupalWednesday, August 24, 11
  40. 40. Profile 2 ✤ Separates users from descriptions of users ✤ Uses different bundles to describe different aspects of the same user ✤ Use entities to provide pro le level type permissionsWednesday, August 24, 11
  41. 41. placed under Entity Module UI admin/structure for handling types per type con gurationWednesday, August 24, 11
  42. 42. use new entity relationships to extend the application provide configuration via entity typesWednesday, August 24, 11
  43. 43. Organic Groups ✤ Organic Groups uses Entities because via the Entity API it gets CRUD, Views, Rules integration etc for free ✤ Groups are still attached to nodes ✤ Automatically created via an indirect user actionWednesday, August 24, 11
  44. 44. Group Entity when you create a node as a Group Type this triggers the creation of the Group Entity separation of concerns opens up interesting possibilities and enables things that were not possible before like better internationalization supportWednesday, August 24, 11
  45. 45. Group Membership Entity content groupWednesday, August 24, 11
  46. 46. because relationships are entities we can add elds to them e.g. date eld indicating relationship expirationWednesday, August 24, 11
  47. 47. use Entities to separate concerns - using the Field API as a way to exibly add access to con guration options - Site builder can decide how much con g to make available to Site AdminsWednesday, August 24, 11
  48. 48. Rooms ✤ Hotel booking application ✤ Introduces Bookable Units and Bookings as entities ✤ Relates entities to non entify-able data ✤ Uses entity types for default values settings as well as a source of common data across all entities instancesWednesday, August 24, 11
  49. 49. Bookable Unit EntityWednesday, August 24, 11
  50. 50. Bookable Unit AvailabilityWednesday, August 24, 11
  51. 51. Entity Types de ne default property valuesWednesday, August 24, 11
  52. 52. Rooms interact via entities CommerceWednesday, August 24, 11
  53. 53. Kickstarting Development entities in a jarWednesday, August 24, 11
  54. 54. Model Entities drupal.org/project/model Implementation of a generic Entity and Entity Administration interface that you can use to kickstart your own project.Wednesday, August 24, 11
  55. 55. experiment and learn from others doing it your way is great... but also need to study and share patterns for doing thingsWednesday, August 24, 11
  56. 56. Summary ✤ Drupalize your data with entities ✤ Improves distinction between drupal the framework and drupal the CMS impoving app development ✤ Leverage the entity module ✤ Learn from examplesWednesday, August 24, 11
  57. 57. @ronald_istos Web App Development Travel / Semantic Web www.istos.it Solutions for hotels, villas, B&Bs drupalrooms.com w/ Bluespark Labs Drupal Trainer network drupalteachers.com w/ BrightlemonWednesday, August 24, 11
  58. 58. What did you think? Locate this session on the DrupalCon London website: http://london2011.drupal.org/conference/schedule Click the “Take the survey” link THANK YOU!Wednesday, August 24, 11

×