SlideShare uma empresa Scribd logo
1 de 37
Baixar para ler offline
Leverage entities!
    by Wolfgang Ziegler // fago
Who am I?

●   Wolfgang Ziegler // fago
●   Drupal developer from Vienna, Austria;
    working for epiqo
●   Form API co-maintainer, Maintainer of
    Entity API, Rules, Content Access, Content Profile, Profile2, Auto nodetitle,
    Field-collection, RestWS, ...

●   Co-founder and active member of
    Drupal-Austria http://drupal-austria.at
●   http://wolfgangziegler.net
Overview

●   Short introduction
●   What are entities?
●   How to develop with entities?
●   How to provide a new entity type?
Entity API

●   So this talk is about the
    ●   Drupal 7's entity API
    ●   and the Entity API module


    http://drupal.org/drupal-7.0
    http://drupal.org/project/entity
What are entities?

●   Entities are the foundation for fields, but


              Entity != Fieldable Entity

●   While most entity types are fieldable, not all are.
    Fields are optional.
●   Entity types defined in core:
    nodes, users, taxomony terms, vocabularies, comments,
    files.
If not fields, what else is it about?

    Entities are required to be
    ●   Loadable (via entity_load())
    ●   Identifiable (what should be loaded?)


●   That's it.
●   It's not even required for an entity to live in the
    local database.
But there is more...

●   But core provides more. There is support for
    ●   Looking up labels (via entity_label())
    ●   Looking up URIs (via entity_uri(), view only)
    ●   View modes – define multiple view modes
●   And there are common hooks:
        hook_entity_load(), hook_entity_presave(),
        hook_entity_insert(), hook_entity_update(),
        hook_entity_delete(), hook_entity_prepare_view,
        hook_entity_view().
●   And...
EntityFieldQuery

●   API for querying entities
    ●   Supports conditions on entity properties as well as
        fields
    ●   Supports queries across multiple entity types (for
        fields).
    ●   Covers entity properties in the local database and
        querying fields in a single storage
●   Extendable via hook_entity_query_alter().
Fields !

●   Optionally, entity types are fieldable!
●   The field API requires a numeric identifier ('id'
    key)
●   Optionally, you may define bundles and revisions.
●   Bundles define which fields are available.
    ●   Default bundle == entity type
    ●   Node types for nodes, Vocabularies for terms.
Entity API module...

●   So far, this all is in Drupal 7.
●   The Entity API module builds upon that to
    ●   ease dealing with any entity type
    ●   easy providing new entity type
        (optionally fieldable, exportable)
Working with entities...

●   entity_load(), entity_save(), entity_create(),
    entity_delete()
●   entity_view(), entity_access(), entity_label(),
    entity_uri()
●   entity_id(), entity_extract_ids()
●   entity_export(), entity_import()
●   entity_get_info(), entity_get_property_info()

    core
Entity property info // Metadata

●   A hook defined by the Entity API module.
●   Allows modules to describe entity properties
    (including fields).
●   Properties have to be described by a set of
    defined data types ('text', 'date', 'user', ..)
●   Includes human readable description, 'getter
    callback', optionally a setter, access
    permissions.
Property info example
// Add meta-data about the basic node properties.
$properties = &$info['node']['properties'];

$properties['title'] = array(
  'label' => t("Title"),
  'type' => 'text',       // Default!
  'description' => t("The title of the node."),
  'setter callback' => 'entity_property_verbatim_set',
  'query callback' => 'entity_metadata_table_query',
  'required' => TRUE,
);
$properties['author'] = array(
  'label' => t("Author"),
  'type' => 'user',
  'description' => t("The author of the node."),
  'getter callback' => 'entity_metadata_node_get_properties',
  'setter callback' => 'entity_metadata_node_set_properties',
  'setter permission' => 'administer nodes',
  'required' => TRUE,
);
Entity metadata wrappers

●   Some useful wrapper classes that ease
    dealing with entities and their properties.
    $wrapper = entity_metadata_wrapper('node', $node);
    $wrapper = entity_metadata_wrapper('node', $nid);

    $wrapper->author->profile->field_name->value();
    $wrapper->author->profile->field_name->set('New name');

    $wrapper->language('de')->body->summary->value();

    $wrapper->author->mail->access('edit') ? TRUE : FALSE;
    $wrapper->author->roles->optionsList();

    $wrapper->field_files[0]->description = 'The first file';
    $wrapper->save();
    $node = $wrapper->value();
How to provide a new entity type?

●   Implement hook_entity_info().
●   Specify your controller class.
    ●   Make use of the entity API controller to get full
        CRUD support, via entity_{CRUD}() out of the box.
    ●   Define your schema via hook_schema().
●   Best practice:
    ●   add {entity_type}_load(), {entity_type}_create(),
        {entity_type}_delete(), {entity_type}_save(),
Entity CRUD API

    → EntityAPIController : For entity types stored in the local db.


●   Invokes all usual CRUD related hooks for you. Document them using
    the documentation template (→ handbooks).
●   Supports entities based on class, e.g. derived from the class “Entity”
●   Having class eases
    ●   customizing labels, URIs, display or saving.
    ●   Is just useful:
                          $entity->entityType();
                          $entity->identifier();
                          $entity->view();
                          $entity->delete();
                          $entity->save();
                          $entity->label(), $entity->uri(), $entity->entityInfo(), ..
Entity CRUD API & Fields

●   Define the entity type as fieldable and you are
    done (controller invokes FieldAPI for you).
●   Provides entity_view() including fields.
●   Supports you with managing bundles:
    ●   Add a separate entity type for bundles → Bundle
        entity (Profile type, Profile)
    ●   Specify the bundle entity type to be 'bundle of'
        another entity type (Profile type is bundle of Profile)
    ●   Again, Field API attachers are invoked for you.
Entity CRUD API: Example Profile2
/**
 * Implements hook_schema().
 */
function profile2_schema() {
  $schema['profile'] = array(
    'description' => 'Stores profile items.',
    'fields' => array(
      'pid' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Primary Key: Unique profile item ID.',
      ),
      'type' => array(
        'description' => 'The {profile_type}.type of this profile.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
…..
Entity CRUD API: Example Profile2
/**
 * Implements hook_entity_info().
 */
function profile2_entity_info() {
  return array(
    'profile2' => array(
      'label' => t('Profile'),
      'entity class' => 'Profile',                 // optional
      'controller class' => 'EntityAPIController',
      'base table' => 'profile',
      'fieldable' => TRUE,
      'entity keys' => array(
        'id' => 'pid',
        'bundle' => 'type',
      ),
      'label callback' => 'entity_class_label',    // optional
      'uri callback' => 'entity_class_uri',        // optional
      'module' => 'profile2',
    ),
    // ….
  );
}
Entity CRUD API: Example Profile2
/**
 * The class used for profile entities.
 */
class Profile extends Entity {

    public function __construct($values = array()) {
      parent::__construct($values, 'profile2');
    }

 public function defaultLabel() {
   return isset($this->label) ? $this->label : (isset($this->type) ? $this->type()-
>label : '');
 }

    public function defaultUri() {
      return array(
        'path' => 'user/' . $this->uid,
        'options' => array('fragment' => 'profile-' . $this->type),
      );
    }

    //...
}
Entity CRUD & Exportables

●   Specify your entity type as exportable in
    hook_entity_info().
●   Make use of machine names.
    ●   Your entity's main identifier will be the machine
        name, so entity_load('your_type', array('name'))
        works.
    ●   The 'id' key remains as numeric identifier, that is
        only used internally (storage, field API).
Entity CRUD & Exportables (2)

●   Add entity_exportable_schema_fields() to your
    schema ('module', 'status').
●   By default, hook_default_{entity_type}() will be
    invoked (+ the respective alter hook).
●   Default entities will be synced to the database
    once modules are enabled / disabled / the cache
    is cleared.
      → You may rely on the db for sorting, querying..
      → You may rely on standard hooks (insert/update/..).
Example: Profile2 types.
/**
 * Implements hook_entity_info().
 */
function profile2_entity_info() {
   $return['profile2_type'] = array(
    'label' => t('Profile type'),
    'entity class' => 'ProfileType',
    'controller class' => 'EntityAPIController',
    'base table' => 'profile_type',
    'fieldable' => FALSE,
    'bundle of' => 'profile2',
    'exportable' => TRUE,
    'entity keys' => array(
      'id' => 'id',
      'name' => 'type',
      'label' => 'label',
    ),
    'module' => 'profile2',
    // ...


function profile2_get_types($type_name = NULL) {
  $types = entity_load('profile2_type', isset($type_name) ? array($type_name) : FALSE);
  return isset($type_name) ? reset($types) : $types;
}
Import / Export

●   $export = entity_export($entity_type, $entity);
●   $entity = entity_import($entity_type, $export);
●   By default:
    ●   Export as JSON
        –   Parse-able without security implications
        –   Parsing JSON is simple.
Example profile type in code
/**
 * Implements hook_default_profile2_type().
 */
function recruiter_resume_default_profile2_type() {
  $items = array();
  $items['resume'] = entity_import('profile2_type', '{
    "userCategory" : false,
    "userView" : false,
    "type" : "resume",
    "label" : "Resume",
    "weight" : "0",
    "data" : { "use_page" : 1},
    "rdf_mapping" : []
  }');
  return $items;
}
Entity CRUD API: Module integrations

●   The entity API helps you integrating with
    modules
    ●   Features integration for exportables
    ●   Very basic entity property info (read-only)
    ●   Basic Views integration based on the property info
    ●   Rules integration (events)
    ●   Token support for all entity properties via the
        “Entity tokens” module
Entity CRUD API: Property info

●   Provide property info for your entity type, to
    ●   Get tokens for all properties, via entity tokens.
    ●   Get Views integration (as long the property has an
        equivalent column in the base table)
    ●   Obtain support of modules building upon it like
        –   Rules
        –   Search API
        –   Restful Web Services (short: RestWS)
Entity CRUD API – Admin UI

●   Enable the admin UI via hook_entity_info().
●   Requires you to write an entity form +
    ●   {entity_type}_load()
    ●   An access callback for entity_access().
●   Customize it by overriding the controller.
●   UI suits very well for managing bundle entities.
    → Used by Profile2 for managing profile types.
Entity Form // Best practices

●   Use form id {entity_type}_form
●   There is a wrapper function, putting
    ●   entity in $form_state[{entity_type}]
    ●   entity type in $form_state['entity_type']
●   Update the entity in $form_state in your submit
    handlers and rebuild / finish.
●   Use entity_form_submit_build_entity() to create
    {entity_type}_form_submit_build_{entity_type}, which
    submit handlers may re-use.
Entity API docs

→ in code entity.api.php
→ in the handbooks
    http://drupal.org/node/878784


→ Learn from examples!
Learn from examples...

●   The test module 'entity_test'.
●   Profile2:
    ●   Fieldable profile entity
    ●   Exportable bundle entity (Profile type)
    ●   Admin UI implemented via the Entity API UI
    ●   Views / Rules / Features integration via Entity API
●   Other modules using it
    Rules, Organic groups, Field-collection, Drupal
    commerce, Message, Rest web services, ..
So back to the start...

●   What is an entity?
So back to the start...

●   What is an entity?




                     A data unit.
So back to the start...

●   What is an entity?




                      A data unit.


             A data unit of which Drupal is aware of.
                        Stored anywhere.
Entity API for non-fieldable stuff? y?

●   Save your time (by not writing CRUD functions)
●   Benefit from a solution proved to work (tests!)
●   Benefit from existing tools, e.g. (static) caching.
●   Improve the DX
    ●   Get unified CRUD functions
    ●   Get unified hooks → Modularity.
    ●   Unified way to discover “data types”, their ids, labels, …
    ●   ...
Thanks!




          Questions?

Mais conteúdo relacionado

Mais procurados

Coding for Scale and Sanity
Coding for Scale and SanityCoding for Scale and Sanity
Coding for Scale and SanityJimKellerES
 
Dig Deeper into WordPress - WD Meetup Cairo
Dig Deeper into WordPress - WD Meetup CairoDig Deeper into WordPress - WD Meetup Cairo
Dig Deeper into WordPress - WD Meetup CairoMohamed Mosaad
 
What's New in Drupal 8: Entity Field API
What's New in Drupal 8: Entity Field APIWhat's New in Drupal 8: Entity Field API
What's New in Drupal 8: Entity Field APIDrupalize.Me
 
“Writing code that lasts” … or writing code you won’t hate tomorrow. - PHPKonf
“Writing code that lasts” … or writing code you won’t hate tomorrow. - PHPKonf“Writing code that lasts” … or writing code you won’t hate tomorrow. - PHPKonf
“Writing code that lasts” … or writing code you won’t hate tomorrow. - PHPKonfRafael Dohms
 
Introduction to DI(C)
Introduction to DI(C)Introduction to DI(C)
Introduction to DI(C)Radek Benkel
 
Your code sucks, let's fix it - PHP Master Series 2012
Your code sucks, let's fix it - PHP Master Series 2012Your code sucks, let's fix it - PHP Master Series 2012
Your code sucks, let's fix it - PHP Master Series 2012Rafael Dohms
 
The Django Book, Chapter 16: django.contrib
The Django Book, Chapter 16: django.contribThe Django Book, Chapter 16: django.contrib
The Django Book, Chapter 16: django.contribTzu-ping Chung
 
Your code sucks, let's fix it (CakeFest2012)
Your code sucks, let's fix it (CakeFest2012)Your code sucks, let's fix it (CakeFest2012)
Your code sucks, let's fix it (CakeFest2012)Rafael Dohms
 
Drupal 8: Routing & More
Drupal 8: Routing & MoreDrupal 8: Routing & More
Drupal 8: Routing & Moredrubb
 
Objective-C for Java Developers
Objective-C for Java DevelopersObjective-C for Java Developers
Objective-C for Java DevelopersBob McCune
 
Practical JavaScript Programming - Session 1/8
Practical JavaScript Programming - Session 1/8Practical JavaScript Programming - Session 1/8
Practical JavaScript Programming - Session 1/8Wilson Su
 
Let's write secure Drupal code! - DrupalCamp London 2019
Let's write secure Drupal code! - DrupalCamp London 2019Let's write secure Drupal code! - DrupalCamp London 2019
Let's write secure Drupal code! - DrupalCamp London 2019Balázs Tatár
 
Lecture 6, c++(complete reference,herbet sheidt)chapter-16
Lecture 6, c++(complete reference,herbet sheidt)chapter-16Lecture 6, c++(complete reference,herbet sheidt)chapter-16
Lecture 6, c++(complete reference,herbet sheidt)chapter-16Abu Saleh
 
BabelJS - James Kyle at Modern Web UI
BabelJS - James Kyle at Modern Web UIBabelJS - James Kyle at Modern Web UI
BabelJS - James Kyle at Modern Web UImodernweb
 
Powerful Generic Patterns With Django
Powerful Generic Patterns With DjangoPowerful Generic Patterns With Django
Powerful Generic Patterns With DjangoEric Satterwhite
 
Objective-C Crash Course for Web Developers
Objective-C Crash Course for Web DevelopersObjective-C Crash Course for Web Developers
Objective-C Crash Course for Web DevelopersJoris Verbogt
 
Jquery presentation
Jquery presentationJquery presentation
Jquery presentationguest5d87aa6
 

Mais procurados (20)

Coding for Scale and Sanity
Coding for Scale and SanityCoding for Scale and Sanity
Coding for Scale and Sanity
 
Dig Deeper into WordPress - WD Meetup Cairo
Dig Deeper into WordPress - WD Meetup CairoDig Deeper into WordPress - WD Meetup Cairo
Dig Deeper into WordPress - WD Meetup Cairo
 
What's New in Drupal 8: Entity Field API
What's New in Drupal 8: Entity Field APIWhat's New in Drupal 8: Entity Field API
What's New in Drupal 8: Entity Field API
 
Java script
Java scriptJava script
Java script
 
“Writing code that lasts” … or writing code you won’t hate tomorrow. - PHPKonf
“Writing code that lasts” … or writing code you won’t hate tomorrow. - PHPKonf“Writing code that lasts” … or writing code you won’t hate tomorrow. - PHPKonf
“Writing code that lasts” … or writing code you won’t hate tomorrow. - PHPKonf
 
Introduction to DI(C)
Introduction to DI(C)Introduction to DI(C)
Introduction to DI(C)
 
Your code sucks, let's fix it - PHP Master Series 2012
Your code sucks, let's fix it - PHP Master Series 2012Your code sucks, let's fix it - PHP Master Series 2012
Your code sucks, let's fix it - PHP Master Series 2012
 
The Django Book, Chapter 16: django.contrib
The Django Book, Chapter 16: django.contribThe Django Book, Chapter 16: django.contrib
The Django Book, Chapter 16: django.contrib
 
Your code sucks, let's fix it (CakeFest2012)
Your code sucks, let's fix it (CakeFest2012)Your code sucks, let's fix it (CakeFest2012)
Your code sucks, let's fix it (CakeFest2012)
 
Drupal 8: Routing & More
Drupal 8: Routing & MoreDrupal 8: Routing & More
Drupal 8: Routing & More
 
Objective-C for Java Developers
Objective-C for Java DevelopersObjective-C for Java Developers
Objective-C for Java Developers
 
Practical JavaScript Programming - Session 1/8
Practical JavaScript Programming - Session 1/8Practical JavaScript Programming - Session 1/8
Practical JavaScript Programming - Session 1/8
 
Let's write secure Drupal code! - DrupalCamp London 2019
Let's write secure Drupal code! - DrupalCamp London 2019Let's write secure Drupal code! - DrupalCamp London 2019
Let's write secure Drupal code! - DrupalCamp London 2019
 
Iphone course 1
Iphone course 1Iphone course 1
Iphone course 1
 
Lecture 6, c++(complete reference,herbet sheidt)chapter-16
Lecture 6, c++(complete reference,herbet sheidt)chapter-16Lecture 6, c++(complete reference,herbet sheidt)chapter-16
Lecture 6, c++(complete reference,herbet sheidt)chapter-16
 
BabelJS - James Kyle at Modern Web UI
BabelJS - James Kyle at Modern Web UIBabelJS - James Kyle at Modern Web UI
BabelJS - James Kyle at Modern Web UI
 
Powerful Generic Patterns With Django
Powerful Generic Patterns With DjangoPowerful Generic Patterns With Django
Powerful Generic Patterns With Django
 
jQuery introduction
jQuery introductionjQuery introduction
jQuery introduction
 
Objective-C Crash Course for Web Developers
Objective-C Crash Course for Web DevelopersObjective-C Crash Course for Web Developers
Objective-C Crash Course for Web Developers
 
Jquery presentation
Jquery presentationJquery presentation
Jquery presentation
 

Semelhante a Entity api

Drupal 7 Entity & Entity API
Drupal 7 Entity & Entity APIDrupal 7 Entity & Entity API
Drupal 7 Entity & Entity API均民 戴
 
Your Entity, Your Code
Your Entity, Your CodeYour Entity, Your Code
Your Entity, Your CodeDrupalDay
 
Drupal 8 entities & felds
Drupal 8 entities & feldsDrupal 8 entities & felds
Drupal 8 entities & feldsAndy Postnikov
 
Understanding the Entity API Module
Understanding the Entity API ModuleUnderstanding the Entity API Module
Understanding the Entity API ModuleSergiu Savva
 
Synapse india reviews on drupal 7 entities (stanford)
Synapse india reviews on drupal 7 entities (stanford)Synapse india reviews on drupal 7 entities (stanford)
Synapse india reviews on drupal 7 entities (stanford)Tarunsingh198
 
ознакомления с модулем Entity api
ознакомления с модулем Entity apiознакомления с модулем Entity api
ознакомления с модулем Entity apiDrupalCamp Kyiv Рысь
 
Drupal 8: Entities
Drupal 8: EntitiesDrupal 8: Entities
Drupal 8: Entitiesdrubb
 
DesignPatterns-IntroPresentation.pptx
DesignPatterns-IntroPresentation.pptxDesignPatterns-IntroPresentation.pptx
DesignPatterns-IntroPresentation.pptxMariusIoacara2
 
Let's write secure Drupal code! - DrupalCamp Oslo, 2018
Let's write secure Drupal code! - DrupalCamp Oslo, 2018Let's write secure Drupal code! - DrupalCamp Oslo, 2018
Let's write secure Drupal code! - DrupalCamp Oslo, 2018Balázs Tatár
 
Создание собственных сущностей с использованием Entity API
Создание собственных сущностей с использованием Entity APIСоздание собственных сущностей с использованием Entity API
Создание собственных сущностей с использованием Entity APIDrupalForumZP2012
 
Perl: Hate it for the Right Reasons
Perl: Hate it for the Right ReasonsPerl: Hate it for the Right Reasons
Perl: Hate it for the Right ReasonsMatt Follett
 
Drupal Field API. Practical usage
Drupal Field API. Practical usageDrupal Field API. Practical usage
Drupal Field API. Practical usagePavel Makhrinsky
 
DrupalJam 2018 - Maintaining a Drupal Module: Keep It Small and Simple
DrupalJam 2018 - Maintaining a Drupal Module: Keep It Small and SimpleDrupalJam 2018 - Maintaining a Drupal Module: Keep It Small and Simple
DrupalJam 2018 - Maintaining a Drupal Module: Keep It Small and SimpleAlexander Varwijk
 
SummaryHW6 Account ManagementIn HW4, you kept track of multiple.pdf
SummaryHW6 Account ManagementIn HW4, you kept track of multiple.pdfSummaryHW6 Account ManagementIn HW4, you kept track of multiple.pdf
SummaryHW6 Account ManagementIn HW4, you kept track of multiple.pdfARORACOCKERY2111
 
From mysql to MongoDB(MongoDB2011北京交流会)
From mysql to MongoDB(MongoDB2011北京交流会)From mysql to MongoDB(MongoDB2011北京交流会)
From mysql to MongoDB(MongoDB2011北京交流会)Night Sailer
 
Extending MySQL Enterprise Monitor
Extending MySQL Enterprise MonitorExtending MySQL Enterprise Monitor
Extending MySQL Enterprise MonitorMark Leith
 
The Naked Bundle - Tryout
The Naked Bundle - TryoutThe Naked Bundle - Tryout
The Naked Bundle - TryoutMatthias Noback
 

Semelhante a Entity api (20)

Drupal 7 Entity & Entity API
Drupal 7 Entity & Entity APIDrupal 7 Entity & Entity API
Drupal 7 Entity & Entity API
 
Your Entity, Your Code
Your Entity, Your CodeYour Entity, Your Code
Your Entity, Your Code
 
Drupal 8 entities & felds
Drupal 8 entities & feldsDrupal 8 entities & felds
Drupal 8 entities & felds
 
Understanding the Entity API Module
Understanding the Entity API ModuleUnderstanding the Entity API Module
Understanding the Entity API Module
 
Synapse india reviews on drupal 7 entities (stanford)
Synapse india reviews on drupal 7 entities (stanford)Synapse india reviews on drupal 7 entities (stanford)
Synapse india reviews on drupal 7 entities (stanford)
 
ознакомления с модулем Entity api
ознакомления с модулем Entity apiознакомления с модулем Entity api
ознакомления с модулем Entity api
 
Drupal 8: Entities
Drupal 8: EntitiesDrupal 8: Entities
Drupal 8: Entities
 
DesignPatterns-IntroPresentation.pptx
DesignPatterns-IntroPresentation.pptxDesignPatterns-IntroPresentation.pptx
DesignPatterns-IntroPresentation.pptx
 
Migrare da symfony 1 a Symfony2
 Migrare da symfony 1 a Symfony2  Migrare da symfony 1 a Symfony2
Migrare da symfony 1 a Symfony2
 
Let's write secure Drupal code! - DrupalCamp Oslo, 2018
Let's write secure Drupal code! - DrupalCamp Oslo, 2018Let's write secure Drupal code! - DrupalCamp Oslo, 2018
Let's write secure Drupal code! - DrupalCamp Oslo, 2018
 
Создание собственных сущностей с использованием Entity API
Создание собственных сущностей с использованием Entity APIСоздание собственных сущностей с использованием Entity API
Создание собственных сущностей с использованием Entity API
 
Perl: Hate it for the Right Reasons
Perl: Hate it for the Right ReasonsPerl: Hate it for the Right Reasons
Perl: Hate it for the Right Reasons
 
Drupal 8 Hooks
Drupal 8 HooksDrupal 8 Hooks
Drupal 8 Hooks
 
Drupal Field API. Practical usage
Drupal Field API. Practical usageDrupal Field API. Practical usage
Drupal Field API. Practical usage
 
DrupalJam 2018 - Maintaining a Drupal Module: Keep It Small and Simple
DrupalJam 2018 - Maintaining a Drupal Module: Keep It Small and SimpleDrupalJam 2018 - Maintaining a Drupal Module: Keep It Small and Simple
DrupalJam 2018 - Maintaining a Drupal Module: Keep It Small and Simple
 
Field api.From d7 to d8
Field api.From d7 to d8Field api.From d7 to d8
Field api.From d7 to d8
 
SummaryHW6 Account ManagementIn HW4, you kept track of multiple.pdf
SummaryHW6 Account ManagementIn HW4, you kept track of multiple.pdfSummaryHW6 Account ManagementIn HW4, you kept track of multiple.pdf
SummaryHW6 Account ManagementIn HW4, you kept track of multiple.pdf
 
From mysql to MongoDB(MongoDB2011北京交流会)
From mysql to MongoDB(MongoDB2011北京交流会)From mysql to MongoDB(MongoDB2011北京交流会)
From mysql to MongoDB(MongoDB2011北京交流会)
 
Extending MySQL Enterprise Monitor
Extending MySQL Enterprise MonitorExtending MySQL Enterprise Monitor
Extending MySQL Enterprise Monitor
 
The Naked Bundle - Tryout
The Naked Bundle - TryoutThe Naked Bundle - Tryout
The Naked Bundle - Tryout
 

Último

Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIShubhangi Sonawane
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 

Último (20)

Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 

Entity api

  • 1. Leverage entities! by Wolfgang Ziegler // fago
  • 2. Who am I? ● Wolfgang Ziegler // fago ● Drupal developer from Vienna, Austria; working for epiqo ● Form API co-maintainer, Maintainer of Entity API, Rules, Content Access, Content Profile, Profile2, Auto nodetitle, Field-collection, RestWS, ... ● Co-founder and active member of Drupal-Austria http://drupal-austria.at ● http://wolfgangziegler.net
  • 3. Overview ● Short introduction ● What are entities? ● How to develop with entities? ● How to provide a new entity type?
  • 4. Entity API ● So this talk is about the ● Drupal 7's entity API ● and the Entity API module http://drupal.org/drupal-7.0 http://drupal.org/project/entity
  • 5. What are entities? ● Entities are the foundation for fields, but Entity != Fieldable Entity ● While most entity types are fieldable, not all are. Fields are optional. ● Entity types defined in core: nodes, users, taxomony terms, vocabularies, comments, files.
  • 6. If not fields, what else is it about? Entities are required to be ● Loadable (via entity_load()) ● Identifiable (what should be loaded?) ● That's it. ● It's not even required for an entity to live in the local database.
  • 7. But there is more... ● But core provides more. There is support for ● Looking up labels (via entity_label()) ● Looking up URIs (via entity_uri(), view only) ● View modes – define multiple view modes ● And there are common hooks: hook_entity_load(), hook_entity_presave(), hook_entity_insert(), hook_entity_update(), hook_entity_delete(), hook_entity_prepare_view, hook_entity_view(). ● And...
  • 8. EntityFieldQuery ● API for querying entities ● Supports conditions on entity properties as well as fields ● Supports queries across multiple entity types (for fields). ● Covers entity properties in the local database and querying fields in a single storage ● Extendable via hook_entity_query_alter().
  • 9. Fields ! ● Optionally, entity types are fieldable! ● The field API requires a numeric identifier ('id' key) ● Optionally, you may define bundles and revisions. ● Bundles define which fields are available. ● Default bundle == entity type ● Node types for nodes, Vocabularies for terms.
  • 10. Entity API module... ● So far, this all is in Drupal 7. ● The Entity API module builds upon that to ● ease dealing with any entity type ● easy providing new entity type (optionally fieldable, exportable)
  • 11. Working with entities... ● entity_load(), entity_save(), entity_create(), entity_delete() ● entity_view(), entity_access(), entity_label(), entity_uri() ● entity_id(), entity_extract_ids() ● entity_export(), entity_import() ● entity_get_info(), entity_get_property_info() core
  • 12. Entity property info // Metadata ● A hook defined by the Entity API module. ● Allows modules to describe entity properties (including fields). ● Properties have to be described by a set of defined data types ('text', 'date', 'user', ..) ● Includes human readable description, 'getter callback', optionally a setter, access permissions.
  • 13. Property info example // Add meta-data about the basic node properties. $properties = &$info['node']['properties']; $properties['title'] = array( 'label' => t("Title"), 'type' => 'text', // Default! 'description' => t("The title of the node."), 'setter callback' => 'entity_property_verbatim_set', 'query callback' => 'entity_metadata_table_query', 'required' => TRUE, ); $properties['author'] = array( 'label' => t("Author"), 'type' => 'user', 'description' => t("The author of the node."), 'getter callback' => 'entity_metadata_node_get_properties', 'setter callback' => 'entity_metadata_node_set_properties', 'setter permission' => 'administer nodes', 'required' => TRUE, );
  • 14. Entity metadata wrappers ● Some useful wrapper classes that ease dealing with entities and their properties. $wrapper = entity_metadata_wrapper('node', $node); $wrapper = entity_metadata_wrapper('node', $nid); $wrapper->author->profile->field_name->value(); $wrapper->author->profile->field_name->set('New name'); $wrapper->language('de')->body->summary->value(); $wrapper->author->mail->access('edit') ? TRUE : FALSE; $wrapper->author->roles->optionsList(); $wrapper->field_files[0]->description = 'The first file'; $wrapper->save(); $node = $wrapper->value();
  • 15. How to provide a new entity type? ● Implement hook_entity_info(). ● Specify your controller class. ● Make use of the entity API controller to get full CRUD support, via entity_{CRUD}() out of the box. ● Define your schema via hook_schema(). ● Best practice: ● add {entity_type}_load(), {entity_type}_create(), {entity_type}_delete(), {entity_type}_save(),
  • 16. Entity CRUD API → EntityAPIController : For entity types stored in the local db. ● Invokes all usual CRUD related hooks for you. Document them using the documentation template (→ handbooks). ● Supports entities based on class, e.g. derived from the class “Entity” ● Having class eases ● customizing labels, URIs, display or saving. ● Is just useful: $entity->entityType(); $entity->identifier(); $entity->view(); $entity->delete(); $entity->save(); $entity->label(), $entity->uri(), $entity->entityInfo(), ..
  • 17. Entity CRUD API & Fields ● Define the entity type as fieldable and you are done (controller invokes FieldAPI for you). ● Provides entity_view() including fields. ● Supports you with managing bundles: ● Add a separate entity type for bundles → Bundle entity (Profile type, Profile) ● Specify the bundle entity type to be 'bundle of' another entity type (Profile type is bundle of Profile) ● Again, Field API attachers are invoked for you.
  • 18. Entity CRUD API: Example Profile2 /** * Implements hook_schema(). */ function profile2_schema() { $schema['profile'] = array( 'description' => 'Stores profile items.', 'fields' => array( 'pid' => array( 'type' => 'serial', 'not null' => TRUE, 'description' => 'Primary Key: Unique profile item ID.', ), 'type' => array( 'description' => 'The {profile_type}.type of this profile.', 'type' => 'varchar', 'length' => 32, 'not null' => TRUE, 'default' => '', ), …..
  • 19. Entity CRUD API: Example Profile2 /** * Implements hook_entity_info(). */ function profile2_entity_info() { return array( 'profile2' => array( 'label' => t('Profile'), 'entity class' => 'Profile', // optional 'controller class' => 'EntityAPIController', 'base table' => 'profile', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'pid', 'bundle' => 'type', ), 'label callback' => 'entity_class_label', // optional 'uri callback' => 'entity_class_uri', // optional 'module' => 'profile2', ), // …. ); }
  • 20. Entity CRUD API: Example Profile2 /** * The class used for profile entities. */ class Profile extends Entity { public function __construct($values = array()) { parent::__construct($values, 'profile2'); } public function defaultLabel() { return isset($this->label) ? $this->label : (isset($this->type) ? $this->type()- >label : ''); } public function defaultUri() { return array( 'path' => 'user/' . $this->uid, 'options' => array('fragment' => 'profile-' . $this->type), ); } //... }
  • 21. Entity CRUD & Exportables ● Specify your entity type as exportable in hook_entity_info(). ● Make use of machine names. ● Your entity's main identifier will be the machine name, so entity_load('your_type', array('name')) works. ● The 'id' key remains as numeric identifier, that is only used internally (storage, field API).
  • 22. Entity CRUD & Exportables (2) ● Add entity_exportable_schema_fields() to your schema ('module', 'status'). ● By default, hook_default_{entity_type}() will be invoked (+ the respective alter hook). ● Default entities will be synced to the database once modules are enabled / disabled / the cache is cleared. → You may rely on the db for sorting, querying.. → You may rely on standard hooks (insert/update/..).
  • 23. Example: Profile2 types. /** * Implements hook_entity_info(). */ function profile2_entity_info() { $return['profile2_type'] = array( 'label' => t('Profile type'), 'entity class' => 'ProfileType', 'controller class' => 'EntityAPIController', 'base table' => 'profile_type', 'fieldable' => FALSE, 'bundle of' => 'profile2', 'exportable' => TRUE, 'entity keys' => array( 'id' => 'id', 'name' => 'type', 'label' => 'label', ), 'module' => 'profile2', // ... function profile2_get_types($type_name = NULL) { $types = entity_load('profile2_type', isset($type_name) ? array($type_name) : FALSE); return isset($type_name) ? reset($types) : $types; }
  • 24. Import / Export ● $export = entity_export($entity_type, $entity); ● $entity = entity_import($entity_type, $export); ● By default: ● Export as JSON – Parse-able without security implications – Parsing JSON is simple.
  • 25. Example profile type in code /** * Implements hook_default_profile2_type(). */ function recruiter_resume_default_profile2_type() { $items = array(); $items['resume'] = entity_import('profile2_type', '{ "userCategory" : false, "userView" : false, "type" : "resume", "label" : "Resume", "weight" : "0", "data" : { "use_page" : 1}, "rdf_mapping" : [] }'); return $items; }
  • 26. Entity CRUD API: Module integrations ● The entity API helps you integrating with modules ● Features integration for exportables ● Very basic entity property info (read-only) ● Basic Views integration based on the property info ● Rules integration (events) ● Token support for all entity properties via the “Entity tokens” module
  • 27. Entity CRUD API: Property info ● Provide property info for your entity type, to ● Get tokens for all properties, via entity tokens. ● Get Views integration (as long the property has an equivalent column in the base table) ● Obtain support of modules building upon it like – Rules – Search API – Restful Web Services (short: RestWS)
  • 28. Entity CRUD API – Admin UI ● Enable the admin UI via hook_entity_info(). ● Requires you to write an entity form + ● {entity_type}_load() ● An access callback for entity_access(). ● Customize it by overriding the controller. ● UI suits very well for managing bundle entities. → Used by Profile2 for managing profile types.
  • 29.
  • 30. Entity Form // Best practices ● Use form id {entity_type}_form ● There is a wrapper function, putting ● entity in $form_state[{entity_type}] ● entity type in $form_state['entity_type'] ● Update the entity in $form_state in your submit handlers and rebuild / finish. ● Use entity_form_submit_build_entity() to create {entity_type}_form_submit_build_{entity_type}, which submit handlers may re-use.
  • 31. Entity API docs → in code entity.api.php → in the handbooks http://drupal.org/node/878784 → Learn from examples!
  • 32. Learn from examples... ● The test module 'entity_test'. ● Profile2: ● Fieldable profile entity ● Exportable bundle entity (Profile type) ● Admin UI implemented via the Entity API UI ● Views / Rules / Features integration via Entity API ● Other modules using it Rules, Organic groups, Field-collection, Drupal commerce, Message, Rest web services, ..
  • 33. So back to the start... ● What is an entity?
  • 34. So back to the start... ● What is an entity? A data unit.
  • 35. So back to the start... ● What is an entity? A data unit. A data unit of which Drupal is aware of. Stored anywhere.
  • 36. Entity API for non-fieldable stuff? y? ● Save your time (by not writing CRUD functions) ● Benefit from a solution proved to work (tests!) ● Benefit from existing tools, e.g. (static) caching. ● Improve the DX ● Get unified CRUD functions ● Get unified hooks → Modularity. ● Unified way to discover “data types”, their ids, labels, … ● ...
  • 37. Thanks! Questions?