Entity APIUnderstanding the Entity API ModuleSergiu SavvaDrupal developerwearepropeople.com
Summary1. An introduction to entities2. Entity types3. Bundles4. Fields5. Entity6. Entity API7. Entity metadata wrapper8. Getters ,Setters, Multilingual
An Introduction to EntitiesWe can build Entity types, which can makeBundles, to which we can add Fields and thencreate Entities.Entity types > Bundles > Fields > Entities
Entity typesFieldable entities make Drupal eminentlyflexible. An entity type is a useful abstractionto group together fields.● Flexible● Fieldable● Used by Views and Rules
Entity types● Nodes (content)● User profiles● Taxonomy terms● CommentsSome 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 theobjects.● fieldable - Set to TRUE if you want your entity type to accept fieldsbeing attached to it.● bundles - An array describing all bundles for this object type.● view modes - An array describing the view modes for the entitytype.You can also build new kinds of entity types if theoptions above dont suit your needs.Read further about using the hook_entity_info()
BundlesBundles are an implementation of an entity typeto which fields can be attached. You can considerbundles as subtypes of an entity type.Bundles for entity type node:
FieldsFields can be added to any of the bundles orentity types to help organize their data.
EntityAn entity would be one instance of a particularentity type such as a comment, taxonomy term,user profile or a bundle such as a blog post, articleor 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
EntityEntity 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 entitiesentity_get_info($entity_type = NULL)File : common.incGet the entity info array of an entity type.
Entity API (contrib)The project Entity API extends the entity API of Drupalcore in order to provide an unified way to deal withentities and their properties.Additionally, it provides an entity CRUD* controller, whichhelps with simplifying the creation of new entity types.[ CRUD - Create, read, update and delete ]
Entity API (contrib)Name Descriptionentity_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
Why use entity metadata wrappers?● Makes your code more readable● Provides a standardised way of accessing field values and entitiesthrough an API● Stops you hard coding the language key into the array lookups● Stops those nasty PHP warnings when you are trying to accessproperties that do not exist● The wrapper autoloads entities (when used in conjunction withthe ->value() accessor), which allow you to chain the callbacks