Entities in drupal 7
Upcoming SlideShare
Loading in...5
×
 

Entities in drupal 7

on

  • 2,761 views

Slides form the Drupalcamp Arad 2012 presentation.

Slides form the Drupalcamp Arad 2012 presentation.

Statistics

Views

Total Views
2,761
Views on SlideShare
2,759
Embed Views
2

Actions

Likes
3
Downloads
33
Comments
0

1 Embed 2

https://twitter.com 2

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Entities in drupal 7 Entities in drupal 7 Presentation Transcript

    • Entities in Drupal 7 Drupalcamp Arad 2012
    • About meSenior LAMP developer at Pitech+plusDrupal projects worked on: http://www.louvre.fr http://www.cartier.usModule contributor:Taxonomy revisions: http://drupal.org/project/taxonomy_revisionMaxlength widget for textareas: http://drupal.org/project/maxlength_js_widgetdrupal.org profile:http://drupal.org/user/350126IRC nickname: skipyTTwitter: @ztasnadi
    • 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
    • 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.
    • Introduction to EntitiesAll of the entities are:LoadableIdentifiableCan be stored anywhere.Also they can be fieldable.
    • 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,      
    • Declaring an entity      access callback => drupalcamp_profile_access,      bundles => array(        profile => array(label => Profile),      ),      entity keys => array(        id => id,      ),    ),  );   return $return;}
    • 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,      ),      
    • 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 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...
    • 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()
    • Entity APIProvides a base entity class and abase entity controller class.EntityEntityAPIControllerSupports revisions, databasetransactions, methods for create,delete, load, etc.Provides unified CRUD interface
    • Entity API
    • 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: http://drupal.org/project/entity_bundle_plugin
    • 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
    • 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,);
    • 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,  );}
    • Entity wrappers$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 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.
    • 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.
    • 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)
    • 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.
    • And the guy is Charles Darwinand not Santa, because we are: