17
DRUPAL 7 AND ENTITIES Erik Webb and Ashok Modi LA Drupal – February 2011

Drupal 7 and Entities

Embed Size (px)

Citation preview

Page 1: Drupal 7 and Entities

DRUPAL 7 AND ENTITIESErik Webb and Ashok ModiLA Drupal – February 2011

Page 2: Drupal 7 and Entities

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

Page 3: Drupal 7 and Entities

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.

Page 4: Drupal 7 and Entities

About Erik (erikwebb)

Technical Consultant at Acquia Best practices and infrastructure

Performance tuning Coding standards

Developing Entitlements API module

Page 5: Drupal 7 and Entities

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

Page 6: Drupal 7 and Entities

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.

Page 7: Drupal 7 and 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.

Page 8: Drupal 7 and Entities

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.

Page 9: Drupal 7 and Entities

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.

Page 10: Drupal 7 and Entities

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()

Page 11: Drupal 7 and Entities

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

Page 12: Drupal 7 and Entities

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.

Page 13: Drupal 7 and 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.

Page 14: Drupal 7 and Entities

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)

Page 15: Drupal 7 and Entities

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

Page 16: Drupal 7 and Entities

Code and Demo

Page 17: Drupal 7 and Entities

Questions and Answers

Thank you!