DRUPAL 7 AND ENTITIES Erik Webb and Ashok Modi LA Drupal – February 2011
Agenda <ul><li>Who are we? </li></ul><ul><li>What are entities? </li></ul><ul><ul><li>Examples of entities </li></ul></ul>...
About Ashok / btmash <ul><li>Originally from Canada. </li></ul><ul><ul><li>No relevance to anything. </li></ul></ul><ul><l...
About Erik (erikwebb) <ul><li>Technical Consultant at Acquia </li></ul><ul><li>Best practices and infrastructure </li></ul...
Credits <ul><li>Wolfgang Ziegler (fago) –  http://wolfgangziegler.net </li></ul><ul><ul><li>Maintainer of Entity API </li>...
What are entities? <ul><li>Till end of Drupal 6, nodes could be considered the main abstraction point. </li></ul><ul><ul><...
What are entities? (cont’d) <ul><li>Type of objects / data which Drupal is aware of. </li></ul><ul><ul><li>A way to descri...
Types of entities <ul><li>Drupal core already has quite a few. </li></ul><ul><li>A few modules out in contrib. </li></ul><...
When should you define your own entities? <ul><li>Trying to repurpose a node to do something when it may not be the best p...
How do you work with entities? <ul><li>Very similar to the hooks for nodes </li></ul><ul><ul><li>hook_node_info / hook_ent...
hook_entity_info() <ul><li>Consist of defining what your entity will have: </li></ul><ul><ul><li>Label </li></ul></ul><ul>...
EntityFieldQuery <ul><li>API for querying entities </li></ul><ul><ul><li>Supports conditions on entity properties and fiel...
What is the new structure like? <ul><li>Entities </li></ul><ul><li>Fields </li></ul><ul><ul><li>Primitive data type that c...
How do the pieces fit together? <ul><li>To create a node type bundle (in install file): </li></ul><ul><ul><li>$node_exampl...
Next Step: Field API <ul><li>Defining fields - hook_field_info(), hook_field_schema(), hook_field_validate(), hook_field_i...
Code and Demo
Questions and Answers <ul><li>Thank you! </li></ul>
Upcoming SlideShare
Loading in...5
×

Drupal 7 and Entities

5,905
-1

Published on

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

No Downloads
Views
Total Views
5,905
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
106
Comments
0
Likes
13
Embeds 0
No embeds

No notes for slide

Drupal 7 and Entities

  1. 1. DRUPAL 7 AND ENTITIES Erik Webb and Ashok Modi LA Drupal – February 2011
  2. 2. Agenda <ul><li>Who are we? </li></ul><ul><li>What are entities? </li></ul><ul><ul><li>Examples of entities </li></ul></ul><ul><li>How do you work with entities? </li></ul><ul><li>How do you make your own entities? </li></ul><ul><ul><li>hook_entity_info() </li></ul></ul><ul><ul><li>EntityFieldQuery </li></ul></ul><ul><li>What ties into Entities? </li></ul><ul><ul><li>Field API </li></ul></ul><ul><li>Code and Demo </li></ul><ul><li>Q&A </li></ul>
  3. 3. About Ashok / btmash <ul><li>Originally from Canada. </li></ul><ul><ul><li>No relevance to anything. </li></ul></ul><ul><li>Work at CalArts. </li></ul><ul><ul><li>Maintain Webfont Loader API, Flexicolor, Workflow Flag and provide patches. </li></ul></ul><ul><li>Trying to learn more about the core of Drupal 7. </li></ul>
  4. 4. About Erik (erikwebb) <ul><li>Technical Consultant at Acquia </li></ul><ul><li>Best practices and infrastructure </li></ul><ul><ul><li>Performance tuning </li></ul></ul><ul><ul><li>Coding standards </li></ul></ul><ul><li>Developing Entitlements API module </li></ul>
  5. 5. Credits <ul><li>Wolfgang Ziegler (fago) – http://wolfgangziegler.net </li></ul><ul><ul><li>Maintainer of Entity API </li></ul></ul><ul><li>Ronald Ashri - http://www.istos.it </li></ul><ul><ul><li>Writeup on entities at http://goo.gl/9RhV9 </li></ul></ul>
  6. 6. What are entities? <ul><li>Till end of Drupal 6, nodes could be considered the main abstraction point. </li></ul><ul><ul><li>Fields </li></ul></ul><ul><ul><li>Versioning </li></ul></ul><ul><ul><li>Translations </li></ul></ul><ul><ul><li>List goes on </li></ul></ul><ul><li>Why not make everything a node (users, comments)? </li></ul><ul><ul><li>Users have content profiles, comments as nodes, taxonomy nodes. </li></ul></ul><ul><ul><li>Discussion on this lead to entities. </li></ul></ul>
  7. 7. What are entities? (cont’d) <ul><li>Type of objects / data which Drupal is aware of. </li></ul><ul><ul><li>A way to describe to the rest of your Drupal application something that is relevant and unique to your domain, that can have its own database table, but at the same time... </li></ul></ul><ul><ul><li>“ The new node”. </li></ul></ul><ul><li>Most types of data in Drupal Core are now defined as entities. </li></ul><ul><ul><li>Nodes, Comments, Terms, Files, Users. </li></ul></ul><ul><li>You package fields/functionality together within an entity and then create a ‘Bundle. </li></ul><ul><li>All of the above can have fields, and altered at various stages. </li></ul>
  8. 8. Types of entities <ul><li>Drupal core already has quite a few. </li></ul><ul><li>A few modules out in contrib. </li></ul><ul><ul><li>Erik will demo his work on the Entitlements API ways to use entities. </li></ul></ul><ul><ul><li>Rules module uses entities to store configurations. </li></ul></ul><ul><ul><li>Media module treats all media as entities. </li></ul></ul><ul><ul><li>Organic groups module combines group attributes into a single entity. </li></ul></ul>
  9. 9. When should you define your own entities? <ul><li>Trying to repurpose a node to do something when it may not be the best place to store content </li></ul><ul><ul><li>Make use of powerful EntityFieldQuery. </li></ul></ul><ul><ul><li>Small data structures that avoided nodes in D6. </li></ul></ul><ul><ul><li>For example, Drupal Commerce line items. </li></ul></ul><ul><li>Why wouldn’t you? </li></ul><ul><ul><li>Currently difficult for non-developers. </li></ul></ul><ul><ul><ul><li>No UI like in pre-configured to create / administer entities on the fly. </li></ul></ul></ul><ul><ul><ul><li>Need to write code. </li></ul></ul></ul>
  10. 10. How do you work with entities? <ul><li>Very similar to the hooks for nodes </li></ul><ul><ul><li>hook_node_info / hook_entity_info </li></ul></ul><ul><ul><li>hook_node_load / hook_entity_load </li></ul></ul><ul><ul><li>… </li></ul></ul><ul><ul><li>hook_entity_presave(), hook_entity_insert(), hook_entity_update(), hook_entity_delete(), hook_entity_prepare_view, hook_entity_view() </li></ul></ul>
  11. 11. hook_entity_info() <ul><li>Consist of defining what your entity will have: </li></ul><ul><ul><li>Label </li></ul></ul><ul><ul><li>Controller (name of class which defines the hooks of the entity – hook_entity_info_alter() allows you to change such things) </li></ul></ul><ul><ul><li>Bundles </li></ul></ul><ul><ul><li>View modes </li></ul></ul><ul><ul><li>Static and field-level caching </li></ul></ul><ul><ul><li>Field ability </li></ul></ul>
  12. 12. EntityFieldQuery <ul><li>API for querying entities </li></ul><ul><ul><li>Supports conditions on entity properties and fields. </li></ul></ul><ul><ul><li>Supports queries across multiple types of entities (e.g. for fields). </li></ul></ul><ul><ul><li>Covers entity properties in the local database and querying fields in a single storage. </li></ul></ul><ul><ul><li>Implement hook_entity_query_alter() to alter the queries for lists of entities. </li></ul></ul>
  13. 13. What is the new structure like? <ul><li>Entities </li></ul><ul><li>Fields </li></ul><ul><ul><li>Primitive data type that can be attached to any entity and shared between entities. </li></ul></ul><ul><ul><ul><li>Can have widgets and formatters. </li></ul></ul></ul><ul><ul><li>Defined using the Field APIs </li></ul></ul><ul><li>Field instances </li></ul><ul><ul><li>Instances of a field attached to an entity. </li></ul></ul><ul><li>Bundles </li></ul><ul><ul><li>Instance of an Entity (represented by the entity type and the bundle name) </li></ul></ul><ul><ul><ul><li>Group of fields </li></ul></ul></ul><ul><ul><li>Node types, Vocabularies. </li></ul></ul><ul><ul><li>Users are ‘both. </li></ul></ul>
  14. 14. How do the pieces fit together? <ul><li>To create a node type bundle (in install file): </li></ul><ul><ul><li>$node_example = array(my_node_type_info) </li></ul></ul><ul><ul><li>$content_type = node_type_set_defaults($node_example) </li></ul></ul><ul><ul><li>node_add_body_field($content_type) </li></ul></ul><ul><ul><li>node_type_save($content_type) </li></ul></ul><ul><ul><li>field_create_field($field_info) </li></ul></ul><ul><ul><li>field_create_instance($field + $bundle_info) </li></ul></ul>
  15. 15. Next Step: Field API <ul><li>Defining fields - hook_field_info(), hook_field_schema(), hook_field_validate(), hook_field_is_empty() </li></ul><ul><li>Formatters - hook_field_formatter_info(), hook_field_formatter_view() </li></ul><ul><li>Field widget - hook_field_widget_info(), hook_field_widget_form() </li></ul><ul><li>@see Examples module </li></ul>
  16. 16. Code and Demo
  17. 17. Questions and Answers <ul><li>Thank you! </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×