Documente Academic
Documente Profesional
Documente Cultură
{ JD Leonard
Freelance Drupal Consultant & Developer
Founder, TextbookMadness.com { Berkeley DUG 2/27/12
Entities, Entities, Entities
Powerful new concept in Drupal 7
Agenda:
How data is represented
Drupal 6 vs. Drupal 7
Entities
Fields (CCK)
Entities vs. nodes
The Entity API (contrib module)
Live example: entities, nodes, and fields used
to build TextbookMadness.com
{ Drupal 6 { Drupal 7
Drupal Data
Entity Types
Elemental building block for most data in Drupal 7
Represents a concept or noun
In core: user, comment, taxonomy, & node
Stores data differently
Generally in a single DB table
Node
Title, body, created timestamp
User
Username, created timestamp, last visit timestamp
Defined using hook_entity_info() example later!
Entity
Instance of an entity type
In core:
Users, comments, taxonomies, and nodes are entities
Eg: the user jdl1234 with uid 12 is an entity
Eg: the page Drupal rocks with nid 44 is an entity
Any entity can be loaded with entity_load()
Eg: entity_load(node, array(44, 65))
Returns an array with two nodes with nids 44 and 65
Core provides some wrappers that do extra work
Eg: node_load() and user_load()
Entity Bundles
Bundle
Subtype of an entity
Eg: page & blog post
Two content types of node entity type
Bundles of node entity type
Not all entity types have more than one
Eg: user
Concept stands on its own
Why bother?
Each entity type stores some data in a table
Eg: node
Title, body, created timestamp
But not all nodes are created equal
Page may have data beyond that captured by node
Blog posts get tagged
CCK, Fields, & Entities
Drupal 6 CCK (Content Construction Kit)
Contrib module
Store additional data per content (node) type
Eg: Fivestar
Contrib module leveraging contrib CCK API
Allows nodes to be rated (think 1-5 stars)
Eg: let users rate pages
Drupal 7 fields
Core concept
Store additional data per entity type
Applies power of CCK to all entity types
Eg: Fivestar
Contrib module leveraging core Field API
Allows entities to be rated
Eg: let users rate pages, users, comments, custom entities
Entities & Nodes
Similarities
Represent things; store data
Fieldable
Types can be defined in code
{ Nodes { Entities
Site building Optionally fieldable
UI to add/edit Development
content types, fields, Lightweight
and content No UI provided
Versioning built in Data storage up to
Data stored in nodes you
table and fields Can be more
performant/scalable
/**
* Load an array of uids of users at the school with the given ID.
* @param int $school_id The ID of the school to load users for
*/
function tbm_school_uids($school_id) {
$query = new EntityFieldQuery();
$result = $query
->entityCondition('entity_type', 'user')
->propertyCondition('status', 1)
->fieldCondition('field_school', 'school_id', $school_id)
->execute();