Entity API
Understanding the Entity API Module
Sergiu Savva
Drupal developerwearepropeople.com
Summary
1. An introduction to entities
2. Entity types
3. Bundles
4. Fields
5. Entity
6. Entity API
7. Entity metadata wrapper
8. Getters ,Setters, Multilingual
An Introduction to Entities
We can build Entity types, which can make
Bundles, to which we can add Fields and then
create Entities.
Entity types > Bundles > Fields > Entities
Entity types
Fieldable entities make Drupal eminently
flexible. An entity type is a useful abstraction
to group together fields.
● Flexible
● Fieldable
● Used by Views and Rules
Entity types
● Nodes (content)
● User profiles
● Taxonomy terms
● Comments
Some examples of entity types:
Entity types
● label - The human-readable name of the type.
● controller class - The name of the class that is used to load the
objects.
● fieldable - Set to TRUE if you want your entity type to accept fields
being attached to it.
● bundles - An array describing all bundles for this object type.
● view modes - An array describing the view modes for the entity
type.
You can also build new kinds of entity types if the
options above don't suit your needs.
Read further about using the hook_entity_info()
Bundles
Bundles are an implementation of an entity type
to which fields can be attached. You can consider
bundles as subtypes of an entity type.
Bundles for entity type node:
Fields
Fields can be added to any of the bundles or
entity types to help organize their data.
Entity
An entity would be one instance of a particular
entity type such as a comment, taxonomy term,
user profile or a bundle such as a blog post, article
or product.
● An entity type is a base class
● A bundle is an extended class
● A field is a class member, property, variable or field instance
(depending on your naming preference)
● An entity is an object or instance of a base or extended class
Relation Entity - Field
Entity Fields
title
body
taxonomy
some text
number
Bundles
Node Page
News title
body
taxonomy
some text
number
Entity
Entity functions from core:
entity_get_controller Get the entity controller class for an entity type.
entity_get_info Get the entity info array of an entity type.
entity_label Returns the label of an entity.
entity_language Returns the language of an entity.
entity_load Load entities from the database.
entity_load_unchanged Loads the unchanged, i.e. not modified, entity from the database.
entity_prepare_view Invoke hook_entity_prepare_view().
entity_uri Returns the URI elements of an entity.
Core entities
entity_get_info($entity_type = NULL)
File : common.inc
Get the entity info array of an entity type.
Entity vs. Entity API
Entity API (contrib)
The project Entity API extends the entity API of Drupal
core in order to provide an unified way to deal with
entities and their properties.
Additionally, it provides an entity CRUD* controller, which
helps with simplifying the creation of new entity types.
[ CRUD - Create, read, update and delete ]
Entity API (contrib)
Name Description
entity_access Determines whether the given user has access to an entity.
entity_create Create a new entity object.
entity_delete Permanently delete the given entity.
entity_delete_multiple Permanently delete multiple entities.
entity_export Exports an entity.
ntity_import Imports an entity.
entity_load_single A wrapper around entity_load() to load a single entity by name or numeric id.
entity_metadata_wrapper Returns a property wrapper for the given data.
entity_revision_delete Deletes an entity revision.
entity_revision_load Loads an entity revision.
entity_save Permanently save an entity.
entity_theme Implements hook_theme().
entity_type_is_fieldable Checks whether an entity type is fieldable.
entity_view Generate an array for rendering the given entities.
entity_get_property_info Get the entity property info array of an entity type.
Entity API hooks
● hook_entity_view
● hook_entity_insert
● hook_entity_update
● hook_entity_presave
● hook_entity_delete
● hook_entity_load
Entity metadata wrapper
Why use entity metadata wrappers?
● Makes your code more readable
● Provides a standardised way of accessing field values and entities
through an API
● Stops you hard coding the language key into the array lookups
● Stops those nasty PHP warnings when you are trying to access
properties that do not exist
● The wrapper autoloads entities (when used in conjunction with
the ->value() accessor), which allow you to chain the callbacks
Entity metadata wrapper
$wrapper = entity_metadata_wrapper('node', $nid);
$mail = $wrapper-->author->mail-->value();
$wrapper-->author-->mail-->set('fago@example.com');
$text = $wrapper-->field_text-->value();
$wrapper-->language('de')-->field_text-->value();
$terms = $wrapper-->field_tags-->value();
$wrapper-->field_tags[] = $term;
$options = $wrapper-->field_tags-->optionsList();
$label = $wrapper-->field_tags[0]-->label();
$access = $wrapper-->field_tags-->access('edit');
Entity metadata wrapper
$node = entity_load_single('node',1);
$entity_wrapper = entity_metadata_wrapper('node', $node);
$entity_wrapper = entity_metadata_wrapper('node', 1);
Load
OR
Entity metadata wrapper
Getters
$entity_wrapper = entity_metadata_wrapper('node', 1);
dpm($entity_wrapper->field_product->raw());
dpm($entity_wrapper->field_product->value());
Entity metadata wrapper
Getters
$entity_wrapper = entity_metadata_wrapper('node', 1);
dpm($entity_wrapper->value());
Entity metadata wrapper
$wrapper = entity_metadata_wrapper('node', 1);
$mail = $wrapper->author->mail->value();
$wrapper->author->mail->set('foo@mail.com');
$wrapper->author->mail = 'foo@mail.com';
$wrapper->author->save();
Setters
Entity metadata wrapper
Multilingual
$wrapper = entity_metadata_wrapper('node',1);
dpm($wrapper->title_field->value());
dpm($wrapper->language('ro')->title_field->value());
dpm($wrapper->language('ru')->title_field->value());
Entity metadata wrapper in life
// add the items from the 'show_features' taxonomy
$show_features = array();
$features = $series->field_features;
if (is_array($features[LANGUAGE_NONE])) {
foreach($features[LANGUAGE_NONE] as $tids) {
$tid = $tids['tid'];
$term = taxonomy_term_load($tid);
$featureList[] = $term->name;
}
}
Old approach
Entity metadata wrapper in life
// add the items from the 'show_features' taxonomy
$show_features = array();
foreach ($wrapper->field_features->value() => $feature) {
$show_features[] = $feature->name;
}
Entity metadata wrapper approach
Questions ?
Thank you!

ознакомления с модулем Entity api

  • 1.
    Entity API Understanding theEntity API Module Sergiu Savva Drupal developerwearepropeople.com
  • 2.
    Summary 1. An introductionto entities 2. Entity types 3. Bundles 4. Fields 5. Entity 6. Entity API 7. Entity metadata wrapper 8. Getters ,Setters, Multilingual
  • 3.
    An Introduction toEntities We can build Entity types, which can make Bundles, to which we can add Fields and then create Entities. Entity types > Bundles > Fields > Entities
  • 4.
    Entity types Fieldable entitiesmake Drupal eminently flexible. An entity type is a useful abstraction to group together fields. ● Flexible ● Fieldable ● Used by Views and Rules
  • 5.
    Entity types ● Nodes(content) ● User profiles ● Taxonomy terms ● Comments Some examples of entity types:
  • 6.
    Entity types ● label- The human-readable name of the type. ● controller class - The name of the class that is used to load the objects. ● fieldable - Set to TRUE if you want your entity type to accept fields being attached to it. ● bundles - An array describing all bundles for this object type. ● view modes - An array describing the view modes for the entity type. You can also build new kinds of entity types if the options above don't suit your needs. Read further about using the hook_entity_info()
  • 7.
    Bundles Bundles are animplementation of an entity type to which fields can be attached. You can consider bundles as subtypes of an entity type. Bundles for entity type node:
  • 8.
    Fields Fields can beadded to any of the bundles or entity types to help organize their data.
  • 9.
    Entity An entity wouldbe one instance of a particular entity type such as a comment, taxonomy term, user profile or a bundle such as a blog post, article or product. ● An entity type is a base class ● A bundle is an extended class ● A field is a class member, property, variable or field instance (depending on your naming preference) ● An entity is an object or instance of a base or extended class
  • 10.
    Relation Entity -Field Entity Fields title body taxonomy some text number Bundles Node Page News title body taxonomy some text number
  • 11.
    Entity Entity functions fromcore: entity_get_controller Get the entity controller class for an entity type. entity_get_info Get the entity info array of an entity type. entity_label Returns the label of an entity. entity_language Returns the language of an entity. entity_load Load entities from the database. entity_load_unchanged Loads the unchanged, i.e. not modified, entity from the database. entity_prepare_view Invoke hook_entity_prepare_view(). entity_uri Returns the URI elements of an entity.
  • 12.
    Core entities entity_get_info($entity_type =NULL) File : common.inc Get the entity info array of an entity type.
  • 13.
  • 14.
    Entity API (contrib) Theproject Entity API extends the entity API of Drupal core in order to provide an unified way to deal with entities and their properties. Additionally, it provides an entity CRUD* controller, which helps with simplifying the creation of new entity types. [ CRUD - Create, read, update and delete ]
  • 15.
    Entity API (contrib) NameDescription entity_access Determines whether the given user has access to an entity. entity_create Create a new entity object. entity_delete Permanently delete the given entity. entity_delete_multiple Permanently delete multiple entities. entity_export Exports an entity. ntity_import Imports an entity. entity_load_single A wrapper around entity_load() to load a single entity by name or numeric id. entity_metadata_wrapper Returns a property wrapper for the given data. entity_revision_delete Deletes an entity revision. entity_revision_load Loads an entity revision. entity_save Permanently save an entity. entity_theme Implements hook_theme(). entity_type_is_fieldable Checks whether an entity type is fieldable. entity_view Generate an array for rendering the given entities. entity_get_property_info Get the entity property info array of an entity type.
  • 16.
    Entity API hooks ●hook_entity_view ● hook_entity_insert ● hook_entity_update ● hook_entity_presave ● hook_entity_delete ● hook_entity_load
  • 17.
  • 18.
    Why use entitymetadata wrappers? ● Makes your code more readable ● Provides a standardised way of accessing field values and entities through an API ● Stops you hard coding the language key into the array lookups ● Stops those nasty PHP warnings when you are trying to access properties that do not exist ● The wrapper autoloads entities (when used in conjunction with the ->value() accessor), which allow you to chain the callbacks
  • 19.
    Entity metadata wrapper $wrapper= entity_metadata_wrapper('node', $nid); $mail = $wrapper-->author->mail-->value(); $wrapper-->author-->mail-->set('fago@example.com'); $text = $wrapper-->field_text-->value(); $wrapper-->language('de')-->field_text-->value(); $terms = $wrapper-->field_tags-->value(); $wrapper-->field_tags[] = $term; $options = $wrapper-->field_tags-->optionsList(); $label = $wrapper-->field_tags[0]-->label(); $access = $wrapper-->field_tags-->access('edit');
  • 20.
    Entity metadata wrapper $node= entity_load_single('node',1); $entity_wrapper = entity_metadata_wrapper('node', $node); $entity_wrapper = entity_metadata_wrapper('node', 1); Load OR
  • 21.
    Entity metadata wrapper Getters $entity_wrapper= entity_metadata_wrapper('node', 1); dpm($entity_wrapper->field_product->raw()); dpm($entity_wrapper->field_product->value());
  • 22.
    Entity metadata wrapper Getters $entity_wrapper= entity_metadata_wrapper('node', 1); dpm($entity_wrapper->value());
  • 23.
    Entity metadata wrapper $wrapper= entity_metadata_wrapper('node', 1); $mail = $wrapper->author->mail->value(); $wrapper->author->mail->set('foo@mail.com'); $wrapper->author->mail = 'foo@mail.com'; $wrapper->author->save(); Setters
  • 24.
    Entity metadata wrapper Multilingual $wrapper= entity_metadata_wrapper('node',1); dpm($wrapper->title_field->value()); dpm($wrapper->language('ro')->title_field->value()); dpm($wrapper->language('ru')->title_field->value());
  • 25.
    Entity metadata wrapperin life // add the items from the 'show_features' taxonomy $show_features = array(); $features = $series->field_features; if (is_array($features[LANGUAGE_NONE])) { foreach($features[LANGUAGE_NONE] as $tids) { $tid = $tids['tid']; $term = taxonomy_term_load($tid); $featureList[] = $term->name; } } Old approach
  • 26.
    Entity metadata wrapperin life // add the items from the 'show_features' taxonomy $show_features = array(); foreach ($wrapper->field_features->value() => $feature) { $show_features[] = $feature->name; } Entity metadata wrapper approach
  • 27.
  • 28.