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