Entities in drupal 7
-
Upload
zsolt-tasnadi -
Category
Technology
-
view
3.839 -
download
4
description
Transcript of Entities in drupal 7
![Page 1: Entities in drupal 7](https://reader034.fdocuments.us/reader034/viewer/2022052315/554bde5eb4c9058f6c8b57f6/html5/thumbnails/1.jpg)
Entities in Drupal 7Drupalcamp Arad 2012
![Page 2: Entities in drupal 7](https://reader034.fdocuments.us/reader034/viewer/2022052315/554bde5eb4c9058f6c8b57f6/html5/thumbnails/2.jpg)
Senior LAMP developer at Pitech+plus
Drupal projects worked on:http://www.louvre.frhttp://www.cartier.us
Module contributor:Taxonomy revisions: http://drupal.org/project/taxonomy_revisionMaxlength widget for textareas: http://drupal.org/project/maxlength_js_widget
drupal.org profile:http://drupal.org/user/350126
IRC nickname: skipyT
Twitter: @ztasnadi
About me
![Page 3: Entities in drupal 7](https://reader034.fdocuments.us/reader034/viewer/2022052315/554bde5eb4c9058f6c8b57f6/html5/thumbnails/3.jpg)
short introduction to entitiesdeclaring an entityentity APIentity propertiesfieldable entitiesentity metadata wrappersentity class and the entity controllerentity field queries
Summary
![Page 4: Entities in drupal 7](https://reader034.fdocuments.us/reader034/viewer/2022052315/554bde5eb4c9058f6c8b57f6/html5/thumbnails/4.jpg)
What is an entity?A data unit!
A data unit which Drupal is aware of!Stored Anywhere...
Entities in core:node = entity content type = bundle
taxonomy term = entity vocabulary = bundleand others like files, vocabularies.
Entities
![Page 5: Entities in drupal 7](https://reader034.fdocuments.us/reader034/viewer/2022052315/554bde5eb4c9058f6c8b57f6/html5/thumbnails/5.jpg)
All of the entities are:LoadableIdentifiable
Can be stored anywhere.Also they can be fieldable.
Introduction to Entities
![Page 6: Entities in drupal 7](https://reader034.fdocuments.us/reader034/viewer/2022052315/554bde5eb4c9058f6c8b57f6/html5/thumbnails/6.jpg)
Implement hook_entity_info()Specify the controller class
Declaring an entity
/** * Implements hook_entity_info(). */function drupalcamp_entity_info() { $return = array( 'profile' => array( 'label' => t('Profile'), 'entity class' => 'ProfileEntity', 'controller class' => 'EntityAPIController', 'module' => 'drupalcamp', 'base table' => 'profile',
![Page 7: Entities in drupal 7](https://reader034.fdocuments.us/reader034/viewer/2022052315/554bde5eb4c9058f6c8b57f6/html5/thumbnails/7.jpg)
Declaring an entity
'access callback' => 'drupalcamp_profile_access', 'bundles' => array( 'profile' => array('label' => 'Profile'), ), 'entity keys' => array( 'id' => 'id', ), ), ); return $return;}
![Page 8: Entities in drupal 7](https://reader034.fdocuments.us/reader034/viewer/2022052315/554bde5eb4c9058f6c8b57f6/html5/thumbnails/8.jpg)
Declaring an entity
Implement hook_schema()/** * Implements hook_schema(). */function drupalcamp_product_schema() { $schema = array();
$schema['profile'] = array( 'description' => "Base table of the profile entity.", 'fields' => array( 'id' => array( 'description' => 'The primary id of a profile.', 'type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE, ),
![Page 9: Entities in drupal 7](https://reader034.fdocuments.us/reader034/viewer/2022052315/554bde5eb4c9058f6c8b57f6/html5/thumbnails/9.jpg)
Declaring an entity
'created' => array( 'description' => 'The Unix timestamp when the profile was created.', 'type' => 'int', 'not null' => TRUE, 'default' => 0, ), ), 'primary key' => array('id'), );
return $schema;}
![Page 10: Entities in drupal 7](https://reader034.fdocuments.us/reader034/viewer/2022052315/554bde5eb4c9058f6c8b57f6/html5/thumbnails/10.jpg)
Declaring an entity
And now you can use functions from core like:
entity_load();entity_get_info();entity_get_controller();
But this is not enough!Let's check Entity API...
![Page 11: Entities in drupal 7](https://reader034.fdocuments.us/reader034/viewer/2022052315/554bde5eb4c9058f6c8b57f6/html5/thumbnails/11.jpg)
Assists us interacting with entities.
entity_create()entity_access()entity_view()entity_save()entity_delete()entity_load_single()entity_get_property_info()entity_metadata_wrapper()
Entity API
![Page 12: Entities in drupal 7](https://reader034.fdocuments.us/reader034/viewer/2022052315/554bde5eb4c9058f6c8b57f6/html5/thumbnails/12.jpg)
Provides a base entity class and a base entity controller class.
EntityEntityAPIController
Supports revisions, database transactions, methods for create, delete, load, etc.Provides unified CRUD interface
Entity API
![Page 13: Entities in drupal 7](https://reader034.fdocuments.us/reader034/viewer/2022052315/554bde5eb4c9058f6c8b57f6/html5/thumbnails/13.jpg)
Entity API
![Page 14: Entities in drupal 7](https://reader034.fdocuments.us/reader034/viewer/2022052315/554bde5eb4c9058f6c8b57f6/html5/thumbnails/14.jpg)
Entities are fieldable if we set:'fieldable' = TRUE, //in hook_entity_info()
Bundles are like content types for nodes
Check the entity bundle plugin also: http://drupal.org/project/entity_bundle_plugin
Fieldable entities
![Page 15: Entities in drupal 7](https://reader034.fdocuments.us/reader034/viewer/2022052315/554bde5eb4c9058f6c8b57f6/html5/thumbnails/15.jpg)
Unified access to entity dataValidationAccess information
Several contrib modules are using it:Entity viewsRulesEntity tokensSearch APIWSClientVBO, OG, Drupal commerce
Entity properties
![Page 16: Entities in drupal 7](https://reader034.fdocuments.us/reader034/viewer/2022052315/554bde5eb4c9058f6c8b57f6/html5/thumbnails/16.jpg)
In hook_entity_property_info()And hook_entity_property_info_alter()
$properties['mail'] = array( 'label' => t("Email"), 'type' => 'text', 'description' => t("The email address of ..."), 'setter callback' => 'entity_property_verbatim_set', 'validation callback' => 'valid_email_address', 'required' => TRUE, 'access callback' => 'user_properties_access', 'schema field' => 'mail',);
Entity properties
![Page 17: Entities in drupal 7](https://reader034.fdocuments.us/reader034/viewer/2022052315/554bde5eb4c9058f6c8b57f6/html5/thumbnails/17.jpg)
Example of entity property use:
/** * Implements hook_entity_property_info(). */function drupalcamp_entity_property_info_alter(&$entity_info) { $entity_info['drupalcamp_product_display']['properties']['sold_online'] = array( 'label' => t('Product sold online'), 'type' => 'list<text>', 'options list' => 'drupalcamp_sold_online_options_list', 'description' => t('A flag indicating whether or not the product is sold online.'), 'getter callback' => 'drupalcamp_sold_online_getter', 'computed' => TRUE, );}
Entity properties
![Page 18: Entities in drupal 7](https://reader034.fdocuments.us/reader034/viewer/2022052315/554bde5eb4c9058f6c8b57f6/html5/thumbnails/18.jpg)
$wrapper = entity_metadata_wrapper('node', $node);$mail = $wrapper>author>mail>value();$wrapper>author>mail >set('[email protected]');$text = $wrapper>field_text>value();$wrapper>language('ro')>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 wrappers
![Page 19: Entities in drupal 7](https://reader034.fdocuments.us/reader034/viewer/2022052315/554bde5eb4c9058f6c8b57f6/html5/thumbnails/19.jpg)
We can use the entity class methods to avoid to introduce new implementations for:hook_entity_presavehook_entity_inserthook_entity_update
Or to do custom methods like entity health check, tagging, etc.
Entity class
![Page 20: Entities in drupal 7](https://reader034.fdocuments.us/reader034/viewer/2022052315/554bde5eb4c9058f6c8b57f6/html5/thumbnails/20.jpg)
Example of usage:
During hook_cron we want to update, touch multiple entities.
Don't forget to call the resetCache and the mark to reindex if you are updating the entities. Good example is when you are enabling several entities based on their publication date.
Entity controller
![Page 21: Entities in drupal 7](https://reader034.fdocuments.us/reader034/viewer/2022052315/554bde5eb4c9058f6c8b57f6/html5/thumbnails/21.jpg)
$query = new EntityFieldQuery();$query>entityCondition('entity_type', 'profile');$query>entityCondition('bundle', 'community_profile');$query>entityCondition('id', 2, '<>');
$query>propertyCondition('created_at', time() 3600, '<');$query>propertyCondition('user_id', $user>uid);
$query>fieldCondition('field_taxonomy_category', 'target_id', $term>tid);$query>fieldCondition('field_taxonomy_category', 'target_type', 'watches');
$query>range(0, 10) >addMetaData('account', user_load(1)); // Run the query as user 1.
// Other methods: count, propertyOrderBy, age (FIELD_LOAD_CURRENT, FIELD_LOAD_REVISION)
Entity field queries
![Page 22: Entities in drupal 7](https://reader034.fdocuments.us/reader034/viewer/2022052315/554bde5eb4c9058f6c8b57f6/html5/thumbnails/22.jpg)
$result = $query>execute();
if (!empty($result['profile'])) { $profiles = entity_load('profile', array_keys($result['profile']));}
// The above example works only if the $query age is set to FIELD_LOAD_CURRENT, which is the default value. For FIELD_LOAD_REVISION the results are keyed by the revision id.
Entity field queries
![Page 23: Entities in drupal 7](https://reader034.fdocuments.us/reader034/viewer/2022052315/554bde5eb4c9058f6c8b57f6/html5/thumbnails/23.jpg)
And the guy is Charles Darwin and not Santa, because we are: