58
Wednesday, August 24, 11

Drupal Entities - Emerging Patterns of Usage

Embed Size (px)

DESCRIPTION

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.

Citation preview

Page 1: Drupal Entities - Emerging Patterns of Usage

Wednesday, August 24, 11

Page 2: Drupal Entities - Emerging Patterns of Usage

Entities - Emerging Patterns of Usage

Presented by Ronald Ashri (ronald_istos)

Code & Coders

Wednesday, August 24, 11

Page 3: Drupal Entities - Emerging Patterns of Usage

Solutions for hotels, villas, B&Bs

drupalrooms.comw/ Bluespark Labs

Web App DevelopmentTravel / Semantic Web

www.istos.it

Drupal Trainer networkdrupalteachers.com

w/ Brightlemon

@ronald_istos

Wednesday, August 24, 11

Page 4: Drupal Entities - Emerging Patterns of Usage

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 Steps

Wednesday, August 24, 11

Page 5: Drupal Entities - Emerging Patterns of Usage

slice up a drupal and you will find nodes

Wednesday, August 24, 11

Page 6: Drupal Entities - Emerging Patterns of Usage

drupalistas love making lots of nodes and mixing them up in different ways

... much like pastaWednesday, August 24, 11

Page 7: Drupal Entities - Emerging Patterns of Usage

they love them so much they nearly turned everything into a node

Wednesday, August 24, 11

Page 8: Drupal Entities - Emerging Patterns of Usage

user pro!les = drupal.org/project/content_pro!le

Wednesday, August 24, 11

Page 9: Drupal Entities - Emerging Patterns of Usage

taxonomy = drupal.org/project/taxonomy_node

Wednesday, August 24, 11

Page 10: Drupal Entities - Emerging Patterns of Usage

comments = drupal.org/project/comment_nodes

Wednesday, August 24, 11

Page 11: Drupal Entities - Emerging Patterns of Usage

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

Page 12: Drupal Entities - Emerging Patterns of Usage

but the core development process is never simple

Wednesday, August 24, 11

Page 13: Drupal Entities - Emerging Patterns of Usage

Huge driver turned out to be the Field API work -

attaching !elds to core “stuff” came !rstand the need to streamline followed

Wednesday, August 24, 11

Page 14: Drupal Entities - Emerging Patterns of Usage

“loadable thingies, that can be optionally !eldable” (http://drupal.org/node/460320)

Wednesday, August 24, 11

Page 15: Drupal Entities - Emerging Patterns of Usage

entities

Wednesday, August 24, 11

Page 16: Drupal Entities - Emerging Patterns of Usage

Entities in Drupal 7 are the nodes we really wanted but didn’t know it yet

The stuff / content that makes up our sitenodes, users, taxonomy terms, comments, !les

User

Taxonomy Term

Taxonomy Vocabulary

Node

Comment

File

Wednesday, August 24, 11

Page 17: Drupal Entities - Emerging Patterns of Usage

Entities bring us closer to Drupal the frameworkNo mention in UI - almost no mention in changelog

one day drupal will have a smallcore

yeah... sure and it will all

be OO

Drupal Core Developers Summit 2040

Wednesday, August 24, 11

Page 18: Drupal Entities - Emerging Patterns of Usage

“In addition, any other object type may register with Field API and allow custom data !elds to be attached to itself.”

CHANGELOG.TXT...

object type = entity

Wednesday, August 24, 11

Page 19: Drupal Entities - Emerging Patterns of Usage

The relationships between Entities - the ways entities can interact with each other - de!ne our

Drupal application

nodes can have commentsusers author nodes

taxonomy terms are attached to nodes

these relationships are implicit - perhaps they could be made explicit?

Wednesday, August 24, 11

Page 20: Drupal Entities - Emerging Patterns of Usage

how to cook - (or build entities)Wednesday, August 24, 11

Page 21: Drupal Entities - Emerging Patterns of Usage

postit_id name property1 property2 property3

Your Table

+ !elds(drupal worries about !elds)

PostIt

Wednesday, August 24, 11

Page 22: Drupal Entities - Emerging Patterns of Usage

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

Page 23: Drupal Entities - Emerging Patterns of Usage

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;}

You can provide you own controller class - typically extending the default DrupalDefaultEntityController

which worries about caching, querying, revisioning and attaching !elds to entities

Wednesday, August 24, 11

Page 24: Drupal Entities - Emerging Patterns of Usage

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;}

base table = where our main entity data livesuri callback = where to look up - view entities

!eldable = are !elds to be attached to our entity

Wednesday, August 24, 11

Page 25: Drupal Entities - Emerging Patterns of Usage

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;}

Bundle = Entity + Con!guration + Fields1 entity (Node) can have many bundles (Article, Page)

Wednesday, August 24, 11

Page 26: Drupal Entities - Emerging Patterns of Usage

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

Page 27: Drupal Entities - Emerging Patterns of Usage

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

Page 28: Drupal Entities - Emerging Patterns of Usage

Drupal Core: What? Am I supposed to do everything around here?

Young module developer : Where is full CRUD, UI, Views integration, Tokens, Search, etc?

Wednesday, August 24, 11

Page 29: Drupal Entities - Emerging Patterns of Usage

+ 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 Tests

Wednesday, August 24, 11

Page 30: Drupal Entities - Emerging Patterns of Usage

so what are we cooking - and how?and why?

Wednesday, August 24, 11

Page 31: Drupal Entities - Emerging Patterns of Usage

entities are a uniquely drupaly concept

your stuff (your db table or something like that)

+Field API

+hooks in the drupal world

Wednesday, August 24, 11

Page 32: Drupal Entities - Emerging Patterns of Usage

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

Page 33: Drupal Entities - Emerging Patterns of Usage

emerging patterns

Wednesday, August 24, 11

Page 34: Drupal Entities - Emerging Patterns of Usage

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 ecosystem

Wednesday, August 24, 11

Page 35: Drupal Entities - Emerging Patterns of Usage

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 entities

Wednesday, August 24, 11

Page 36: Drupal Entities - Emerging Patterns of Usage

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 sense

Wednesday, August 24, 11

Page 37: Drupal Entities - Emerging Patterns of Usage

Entity Listing as a View operational links

entity typeslocal action

Wednesday, August 24, 11

Page 38: Drupal Entities - Emerging Patterns of Usage

Order Entity Contains LineItem Entities

Related to payment entities

looks familiar!

address!eld !eld

Wednesday, August 24, 11

Page 39: Drupal Entities - Emerging Patterns of Usage

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 Drupal

Wednesday, August 24, 11

Page 40: Drupal Entities - Emerging Patterns of Usage

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 permissions

Wednesday, August 24, 11

Page 41: Drupal Entities - Emerging Patterns of Usage

Entity Module UIfor handling types

placed under admin/structure

per type con!guration

Wednesday, August 24, 11

Page 42: Drupal Entities - Emerging Patterns of Usage

use new entity relationships to extend the application

provide configuration via entity types

Wednesday, August 24, 11

Page 43: Drupal Entities - Emerging Patterns of Usage

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 action

Wednesday, August 24, 11

Page 44: Drupal Entities - Emerging Patterns of Usage

when you create a node as a Group Type this triggersthe creation of the

Group Entity

Group Entity

separation of concerns opens up interesting possibilities and enables things that were not possible before like

better internationalization support

Wednesday, August 24, 11

Page 45: Drupal Entities - Emerging Patterns of Usage

Group Membership Entity

groupcontent

Wednesday, August 24, 11

Page 46: Drupal Entities - Emerging Patterns of Usage

because relationships are entities we can add !elds to them

e.g. date !eld indicating relationship expiration

Wednesday, August 24, 11

Page 47: Drupal Entities - Emerging Patterns of Usage

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 Admins

Wednesday, August 24, 11

Page 48: Drupal Entities - Emerging Patterns of Usage

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 instances

Wednesday, August 24, 11

Page 49: Drupal Entities - Emerging Patterns of Usage

Bookable Unit Entity

Wednesday, August 24, 11

Page 50: Drupal Entities - Emerging Patterns of Usage

Bookable Unit Availability

Wednesday, August 24, 11

Page 51: Drupal Entities - Emerging Patterns of Usage

Entity Types de!ne default property values

Wednesday, August 24, 11

Page 52: Drupal Entities - Emerging Patterns of Usage

Rooms

Commerce

interact via entities

Wednesday, August 24, 11

Page 53: Drupal Entities - Emerging Patterns of Usage

Kickstarting Developmententities in a jar

Wednesday, August 24, 11

Page 54: Drupal Entities - Emerging Patterns of Usage

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

Page 55: Drupal Entities - Emerging Patterns of Usage

experiment and learn from othersdoing it your way is great...

but also need to study and share patterns for doing things

Wednesday, August 24, 11

Page 56: Drupal Entities - Emerging Patterns of Usage

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 examples

Wednesday, August 24, 11

Page 57: Drupal Entities - Emerging Patterns of Usage

Solutions for hotels, villas, B&Bs

drupalrooms.comw/ Bluespark Labs

Web App DevelopmentTravel / Semantic Web

www.istos.it

Drupal Trainer networkdrupalteachers.com

w/ Brightlemon

@ronald_istos

Wednesday, August 24, 11

Page 58: Drupal Entities - Emerging Patterns of Usage

THANK YOU!

What did you think?Locate this session on the DrupalCon London website:http://london2011.drupal.org/conference/schedule

Click the “Take the survey” link

Wednesday, August 24, 11