Your SlideShare is downloading. ×
  • Like
Entities in drupal 7
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.


Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Entities in drupal 7


Slides form the Drupalcamp Arad 2012 presentation.

Slides form the Drupalcamp Arad 2012 presentation.

Published in Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads


Total Views
On SlideShare
From Embeds
Number of Embeds



Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide


  • 1. Entities in Drupal 7 Drupalcamp Arad 2012
  • 2. About meSenior LAMP developer at Pitech+plusDrupal projects worked on: contributor:Taxonomy revisions: widget for textareas: profile: nickname: skipyTTwitter: @ztasnadi
  • 3. Summary short introduction to entities declaring an entity entity API entity properties fieldable entities entity metadata wrappers entity class and the entity controller entity field queries
  • 4. Entities What is an entity? A data unit! A data unit which Drupal is aware of! Stored Anywhere... Entities in core: node = entity content type = bundletaxonomy term = entity vocabulary = bundle and others like files, vocabularies.
  • 5. Introduction to EntitiesAll of the entities are:LoadableIdentifiableCan be stored anywhere.Also they can be fieldable.
  • 6. Declaring an entity Implement hook_entity_info() Specify the controller class/** * 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,      
  • 7. Declaring an entity      access callback => drupalcamp_profile_access,      bundles => array(        profile => array(label => Profile),      ),      entity keys => array(        id => id,      ),    ),  );   return $return;}
  • 8. Declaring an entityImplement 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,      ),      
  • 9. 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;}
  • 10. Declaring an entityAnd now you can use functions from core like:entity_load();entity_get_info();entity_get_controller();But this is not enough!Lets check Entity API...
  • 11. Entity APIAssists us interacting with entities.entity_create()entity_access()entity_view()entity_save()entity_delete()entity_load_single()entity_get_property_info()entity_metadata_wrapper()
  • 12. Entity APIProvides a base entity class and abase entity controller class.EntityEntityAPIControllerSupports revisions, databasetransactions, methods for create,delete, load, etc.Provides unified CRUD interface
  • 13. Entity API
  • 14. Fieldable entities 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:
  • 15. Entity properties Unified access to entity data Validation Access informationSeveral contrib modules are using it: Entity views Rules Entity tokens Search API WSClient VBO, OG, Drupal commerce
  • 16. Entity propertiesIn 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,);
  • 17. Entity propertiesExample 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,  );}
  • 18. Entity wrappers$wrapper = entity_metadata_wrapper(node, $node);$mail = $wrapper­>author­>mail­>value();$wrapper­>author­>mail  ­>set(;$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);
  • 19. Entity classWe can use the entity class methodsto avoid to introduce newimplementations for: hook_entity_presave hook_entity_insert hook_entity_update Or to do custom methods like entityhealth check, tagging, etc.
  • 20. Entity controllerExample of usage: During hook_cron we want to update,touch multiple entities.Dont forget to call the resetCacheand the mark to reindex if you areupdating the entities. Good exampleis when you are enabling severalentities based on their publicationdate.
  • 21. Entity field queries$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)
  • 22. 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.
  • 23. And the guy is Charles Darwinand not Santa, because we are: