entities in drupal 7

Post on 08-May-2015

3.845 Views

Category:

Technology

4 Downloads

Preview:

Click to see full reader

DESCRIPTION

Slides form the Drupalcamp Arad 2012 presentation.

TRANSCRIPT

Entities in Drupal 7Drupalcamp Arad 2012

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

short introduction to entitiesdeclaring an entityentity APIentity propertiesfieldable entitiesentity metadata wrappersentity class and the entity controllerentity field queries

Summary

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

All of the entities are:LoadableIdentifiable

Can be stored anywhere.Also they can be fieldable.

Introduction to Entities

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',      

Declaring an entity

      'access callback' => 'drupalcamp_profile_access',      'bundles' => array(        'profile' => array('label' => 'Profile'),      ),      'entity keys' => array(        'id' => 'id',      ),    ),  );   return $return;}

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

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;}

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...

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

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

Entity API

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

Unified access to entity dataValidationAccess information

Several contrib modules are using it:Entity viewsRulesEntity tokensSearch APIWSClientVBO, OG, Drupal commerce

Entity properties

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

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

$wrapper = entity_metadata_wrapper('node', $node);$mail = $wrapper­>author­>mail­>value();$wrapper­>author­>mail  ­>set('ztasnadi@pitechplus.com');$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

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

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

$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

$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

And the guy is Charles Darwin and not Santa, because we are:

top related