Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Drupal Entity API

Drupal Entity API

Drupal Meetup with Paul Preibisch at Payroll Hero, BGC the Fort

Paul de Paula

June 19, 2013
Tweet

More Decks by Paul de Paula

Other Decks in Programming

Transcript

  1. Drupal  7  Field  and  En/ty  API       June

     19,  2013   Paul  de  Paula  @fusionx1  
  2. About  me   l  SoDware  Engineering  background   l  Working

     with  Drupal  since  2007   l  Infrastructure  Specialist,  Architect,  Devops   l  PHDUG  Core  Leader     l  Former  Abs-­‐cbnnews.com  lead  developer   l  Proud  Dad  
  3. Agenda   l  Ways  we  represent  data  in  D6  and

     D7   l  En//es,  en/ty  types,  &  bundles   l  Fields  formerly  CCK   l  PHDUG  Core  Leader     l  How  to  define  a  custom  en/ty   l  Using  En/tyFieldQuery,  En/ty  Property  Info  and  Leveraging  the  En/ty   Metatdata  Wrapper  
  4. D6  Data   l  Custom  database  table   l  User

      l  Comment   l  File   l  Taxonomy  Vocabulary  and  term   l  Node   l  Page,  Blog  Post,  Custom  Content  type  
  5. D7  Data   l  Custom  database  table   l  En/ty

        l  User   l  Comment   l  File   l  Taxonomy  Vocab  and  Term   l  Node   l  Page  ,  Blog  Post,  Custom  Content  Type  
  6. En/ty  Types   l  Elemental  Building  block  for  important  data

     in  Drupal  7   l  Represents  a  concept  or  noun   l  Different  types  store  different  data   l  Generally  in  a  single  DB  table   l  Node   l  Title,  body,  created  /mestamp,  etc.   l  User   l  Username,  created  /mestamp,  last  visit  /mestamp  
  7. En/ty   l  Instance  of  en/ty  type   l  Example

     of  en/tes  from  core   l  The  user  fusionx1  with  uid  10000   l  The  page  “Drupal  Pilipinas”  with  nid  77     l  Any  En/ty  can  be  loaded  with  en/ty_load  ()   l  en/ty_load(‘node’,  array(33,  44))   l  Returns  an  array  with  two  nodes  with  nids  33  and  44   l  Core  provides  some  convenience  wrapper   l  node_load()  and  user_load()  
  8. En/ty  Bundles   l  Subtype  of  an  en/ty    

    l  Page,  Ar/cle  and  Blog  Post   l  Three  content  types  (bundles)  of  node  en/ty  type   l  Not  all  en/ty  type  have  more  than  one  bundle   l  User   l  No  subtypes;  concept  stands  on  its  own          
  9. CCK  :  Content  Construc/on  Kit   l  Contrib  Module  

    l  Store  addi/onal  data  per  content  type   l  Fivestar   l  Contrib  module  leverage  contrib  CCK  API   l  Allows  nodes  to  be  rated(1-­‐5)   l  Let  users  rate  pages  and  blogs  
  10. Fields(D7)   l  Core  concept   l  Store  addi/onal  data

     per  en/ty  type   l  Applies  power  of  CCk  to  all  en/ty  types   l  Not  just  nodes   l  Fivestar   l  Contrib  module  leverages  core  FIELD  API   l  Allows  en//es  to  be  rated   l  Let  users  rate  pages,  users,  comments,  custom  en/ty  type    
  11. En//es  in  Core  VS  Contrib   l  En//es  are  a

     drupal  7  core  concept   l  Core  provides  the  “En/ty  API”   l  Not  everything  “made  it  into”  core   l  Thanks  we  have  En/ty  API  Contrib  
  12. En/ty  API  Contrib  Module   l  Makes  dealing  with  en//es

     easier   l  Provide  full  CRUD  func/onality   l  Allows  defini/on  of  metadata  about  en/tes   l  Op/onally  makes  en/ty  data:   l  Exportable   l  Revisionable   l  Op/onal  administra/ve  UI  for  managing  en/tes   l  Object  Oriented  representa/on  of  en/ty  types  
  13. Contrib  Modules  Defining  En//es   l  Commerce   l  Organic

     Groups   l  Rules   l  Message   l  Rooms    
  14. Implement  hook  schema   $schema['tbm_offer'] = array( // SIMPLIFIED FOR

    SLIDE! 'fields' => array(! 'offer_id' => array('type' => 'serial'),! 'isbn' => array('type' => 'int'),! 'retailer_id' => array('type' => 'int'),! 'price' => array('type' => 'int'),! 'last_updated' => array('type' => 'int'),! ),! 'primary key' => array('offer_id')! );!  
  15. Implement  hook_en/ty_info()   $entities['tbm_offer'] = array(! 'label' => t('Offer'),! 'plural

    label' => t('Offers'),! 'entity class' => 'Entity',! 'controller class' => 'EntityAPIController',! 'module' => 'tbm',! 'base table' => 'tbm_offer',! 'fieldable' => FALSE,! 'entity keys' => array(! 'id' => 'offer_id',! ));! !
  16. Make  an  En/ty  Type  Exportable   $entities['tbm_offer'] = array(! 'label'

    => t('Offer'),! 'plural label' => t('Offers'),! 'entity class' => 'Entity',! 'controller class' => 'EntityAPIControllerExportable',! 'module' => 'tbm',! 'base table' => 'tbm_offer',! 'fieldable' => FALSE, 'exportable' => TRUE,! 'entity keys' => array(! 'id' => 'offer_id', 'name' => 'my_machine_name'! ));!
  17. Make  an  En/ty  Type  Revisionable   $entities['tbm_offer'] = array(! 'label'

    => t('Offer'),! 'plural label' => t('Offers'),! 'entity class' => 'Entity',! 'controller class' => 'EntityAPIController',! 'module' => 'tbm',! 'base table' => 'tbm_offer', 'revision_table' => 'tbm_o_revision',! 'fieldable' => FALSE,! 'entity keys' => array(! 'id' => 'offer_id', 'revision' => 'revision_id',! ));! !
  18. En/ty  Field  Query   l  Tool  for  querying  en//es  

    l  Can  query  en/ty  proper%es  and  field  data   l  Can  query  field  data  across  en/ty  types   –  Eg:  “return  all  pages  and  users  tagged  with  taxonomy  term  456”   l  Returns  en/ty  IDs   –  Usually  you’ll  then  load  with  en/ty_load()  
  19. En/tyFieldQuery  Example   $query = new EntityFieldQuery();! $query! ->entityCondition('entity_type', 'node')!

    ->entityCondition('bundle', 'page')! ->propertyCondition('status', 1)! ->fieldCondition('field_awesome_factor', 'value', 3.2, '<')! ->fieldOrderBy('field_awesome_factor', 'value', 'DESC')! ->range(0, 10);! // Top 10 Published page nodes with an awesome factor < 3.2! !
  20. En/ty  Metadata  Wrapper  Examples   // Get node author's email

    address (given $nid)! ! // BEFORE! $node = node_load($nid);! $author = user_load($node->uid);! $email = check_plain($author->mail);! 
 // AFTER
 $wrapper = entity_metadata_wrapper('node', $nid);! $email = $wrapper->author->mail->value();! !
  21. More  Info   l  En/ty  API  documenta/on   –  Lots

     of  informa/on  in  many  subpages   l  En/tyFieldQuery  documenta/on   l  En/ty  API  contrib  module  page