SlideShare a Scribd company logo
1 of 34
Entities in Drupal 7
          & the Entity API



#sfdug
March 11, 2013
JD Leonard (@drupal_jd)
ModernBizConsulting.com
About Me

    Computer science background

    Working with Drupal since 2006

    Developer, architect, project manager

    Freelance through my business:
    Modern Biz Consulting

    Focus on complex web application development
Agenda

    Ways we represent data in D6 vs D7

    Entities, entity types, & bundles

    Fields (formerly CCK)

    The Entity API contrib module

    How (and why) to define a custom entity

    Using EntityFieldQuery

    Defining entity property info

    Leveraging the Entity Metadata Wrapper
Poll

    Who here is...
       –   Brand new to Drupal?
       –   A site builder?
       –   A module developer?
       –   On the business side of things?

    Who here has...
       –   Created a custom D7 entity?
       –   Not created a custom D7 entity?
D6 Data

    (Custom database table)

    User

    Comment

    File

    Taxonomy vocabulary

    Taxonomy term

    Node
       –   Page, Blog post, (custom content type)
D7 Data

    (Custom database table)

    Entity
      –   User
      –   Comment
      –   File
      –   Taxonomy vocabulary
      –   Taxonomy term
      –   Node
             •   Page, Blog post, (custom content type)

    (Custom entity type)
Entity Types

    Elemental building block for most important data in
    Drupal 7

    Represents a concept or noun

    Different types store different data
       –   Generally in a single DB table
       –   Eg: Node
             •   Title, body, created timestamp, etc.
       –   Eg: User
             •   Username, created timestamp, last visit timestamp, etc.

    Defined using hook_entity_info()
Entity

    Instance of an entity type

    Examples of entities from core:
       –   The user jdleonard with uid 80902
       –   The page “Drupal Rocks” with nid 44

    Any entity can be loaded with entity_load()
       –   Eg: entity_load(‘node’, array(44, 65))
             •   Returns an array with two nodes with nids 44 and 65
       –   Core provides some convenience wrappers
             •   Eg: node_load() and user_load()
Entity Bundles

    Subtype of an entity

    Eg: Page and Blog post
      –   Two content types (bundles) of node entity type

    Not all entity types have more than one bundle
      –   Eg: User
            •   No subtypes; concept stands on its own
Why We Need Entity Bundles

    Each entity type stores some data in a table
       –   We call these pieces of data “properties”
       –   Eg: node
             •   Title, body, author (uid), created timestamp
             •   But not all nodes are created equal
                    –   Page may have data beyond that captured by node
                    –   Blog posts get tagged
                    –   We need fields!
CCK: Content Construction Kit (D6)

    Contrib module

    Store additional data per content (node) content type

    Eg: Fivestar
       –   Contrib module leveraging contrib CCK API
       –   Allows nodes to be rated (think 1-5 stars)
       –   Eg: let users rate pages or blog posts
Fields (D7)

    Core concept

    Store additional data per entity type

    Applies power of CCK to all entity types
       –   Not just nodes!

    Eg: Fivestar
       –   Contrib module leveraging core Field API
       –   Allows entities to be rated
       –   Eg: let users rate pages, blog posts, users, comments,
           custom entities
Entities in Core vs Contrib

    Entities are a Drupal 7 core concept

    Core provides the “Entity API”
       –   Functions and hooks to define and interact with
           entities, entity types, and entity bundles

    Everything we've discussed so far is part of Drupal 7
    core (no contrib modules necessary)

    Not everything “made it into” core
       –   Thankfully there's the “Entity API” contrib module
             •   (confusingly named)
Entity API Contrib Module

    Makes dealing with entities easier

    Provides full CRUD functionality
       –   CReate, Update, and Delete

    Allows definition of metadata about entities

    Optionally makes entity data
       –   Exportable (for configuration)
       –   Revisionable (eg: node revisions)

    Optional administrative UI for managing entities

    Object-oriented representation of entity types
A Note on Documentation

    Documentation for core's Entity API is separate
    from that for the Entity API contrib module

    In practice, you'll probably always use the Entity
    API contrib module
       –   It provides so much awesome functionality!

    Don't get confused
       –   Always install the Entity API contrib module when
           defining an entity
       –   Make sure to read the module's documentation
Contrib Modules Defining Entities

    Commerce
      –     Commerce Product, Commerce Payment Transaction

    Organic Groups
      –     OG Membership, OG Membership Type

    Rules
      –     Rules Configuration

    Message (think activity stream)
      –     Message, Message Type, Message Type Category

    … and many more!
Example Custom Entity Type

    TextbookMadness.com
      –   Classified listings for textbooks at schools
      –   Online price comparison shopping (prices from
          Amazon, Textbooks.com, etc.)

    Online prices are
      –   Fetched from a third-party API and stored by Drupal
      –   Hereafter known as Offers
How Shall we Define an Offer?

    We could use the UI to define an offer node with a
    bunch of fields to store pricing information

    But there's a lot of overhead!
       –    Don't need/want a page (path) per offer
       –    Don't need/want revisions
       –    Don't need/want node base table information
              •   Language (and translation info), Title, Author,
                  Published status, Created date, Commenting,
                  Promoting, etc.

    We want an entity!
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')
);
Implement hook_entity_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',
 ));
Make an Entity 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'
 ));
Make an Entity 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',
 ));
Other hook_entity_info() configuration
$entities['tbm_offer'] = array( …
 'entity class' => 'TbmOfferEntity', // Extends Entity class
 'controller class' => 'TbmOfferEntityController',
 'access callback' => 'tbm_offer_access',
 'admin ui' => array(
  'path' => 'admin/structure/offers',
  'file' => 'tbm.admin.inc',
 ),
 'label callback' => 'entity_class_label',
 'uri callback' => 'entity_class_uri',
); // TbmOfferEntity->defaultLabel(), defaultUri()
Entity Property Info

    hook_entity_property_info()
      –   Defines entity metadata
      –   Provided for core entities

    Automatically generated from hook_schema()
      –   Doesn't understand foreign key relationships
          (references)
      –   Doesn't know when an integer is actually a date (eg:
          unix timestamps)

    Fill in the gaps with
    hook_entity_property_info_alter()
hook_entity_property_info_alter()
$offer = &$info['tbm_offer']['properties'];


$offer['url']['type'] = 'uri'; // was a varchar in hook_schema
$offer['url']['label'] = 'URL';


$offer['last_updated']['type'] = 'date'; // integer in hook_schema
$offer['last_updated']['label'] = t('Last updated');


// Other types: text, token (machine name), integer, decimal,
   duration, boolean, entity, struct, list<TYPE>
hook_entity_property_info_alter()
// Define a new property to reference an offer's retailer (eg:
   Amazon.com)
$offer['retailer'] = array(
 'label' => t('Retailer'),
 'type' => 'tbm_retailer', // The tbm_retailer entity type
 'description' => t('The retailer making the offer.'),
 'required' => TRUE,
 'schema field' => 'retailer_id', // as defined in hook_schema
);
Entity API Integrations

    Views
      –     Eg: in a view of offers, we can add a retailer
            relationship and include details about each offer's
            retailer

    Rules

    Search API

    Features

    I18n

    Token system - Eg: [tbm_offer:retailer:name]
EntityFieldQuery

    Tool for querying entities

    Can query entity properties and field data

    Can query field data across entity types
       –   Eg: “return all pages and users tagged with
           taxonomy term 456”

    Returns entity IDs
       –   Usually you’ll then load with entity_load()
EntityFieldQuery 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
EntityFieldQuery Example
$result = $query->execute(); // Keyed by entity type


if (isset($result['node'])) {
    $nids = array_keys($result['node']);
    $pages = node_load_multiple($nids);
    $same_as_pages = entity_load('node', $nids);
}
Entity 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();
Entity Metadata Wrapper Examples
// Set node author's email address
$wrapper->author->mail = 'me@example.com';
$wrapper->save();


// Iterate over a list of node's taxonomy terms
foreach ($wrapper->field_taxonomy_terms->getIterator() as
  $term_wrapper) {
    $label = $term_wrapper->label->value();
}
More Modules!

    Entity Construction Kit (ECK)
      –   CCK for entities
      –   Create entity types in a UI

    Entity Cache
      –   Integrates entities with Drupal's Cache API

    Entity Reference
      –   Like node reference field, but for any type of entity

    Automatic Entity Label
      –   Generic successor to Automatic Node Titles
More information

    Entity API documentation
      –   Lots of information in many subpages

    EntityFieldQuery documentation

    Entity API contrib module page

    Deck is on Slideshare

    Email me: jd at ModernBizConsulting.com

    Follow me: @drupal_jd

More Related Content

Recently uploaded

What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 

Recently uploaded (20)

What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Entities in Drupal 7 & the Entity API

  • 1. Entities in Drupal 7 & the Entity API #sfdug March 11, 2013 JD Leonard (@drupal_jd) ModernBizConsulting.com
  • 2. About Me  Computer science background  Working with Drupal since 2006  Developer, architect, project manager  Freelance through my business: Modern Biz Consulting  Focus on complex web application development
  • 3. Agenda  Ways we represent data in D6 vs D7  Entities, entity types, & bundles  Fields (formerly CCK)  The Entity API contrib module  How (and why) to define a custom entity  Using EntityFieldQuery  Defining entity property info  Leveraging the Entity Metadata Wrapper
  • 4. Poll  Who here is... – Brand new to Drupal? – A site builder? – A module developer? – On the business side of things?  Who here has... – Created a custom D7 entity? – Not created a custom D7 entity?
  • 5. D6 Data  (Custom database table)  User  Comment  File  Taxonomy vocabulary  Taxonomy term  Node – Page, Blog post, (custom content type)
  • 6. D7 Data  (Custom database table)  Entity – User – Comment – File – Taxonomy vocabulary – Taxonomy term – Node • Page, Blog post, (custom content type)  (Custom entity type)
  • 7. Entity Types  Elemental building block for most important data in Drupal 7  Represents a concept or noun  Different types store different data – Generally in a single DB table – Eg: Node • Title, body, created timestamp, etc. – Eg: User • Username, created timestamp, last visit timestamp, etc.  Defined using hook_entity_info()
  • 8. Entity  Instance of an entity type  Examples of entities from core: – The user jdleonard with uid 80902 – The page “Drupal Rocks” with nid 44  Any entity can be loaded with entity_load() – Eg: entity_load(‘node’, array(44, 65)) • Returns an array with two nodes with nids 44 and 65 – Core provides some convenience wrappers • Eg: node_load() and user_load()
  • 9. Entity Bundles  Subtype of an entity  Eg: Page and Blog post – Two content types (bundles) of node entity type  Not all entity types have more than one bundle – Eg: User • No subtypes; concept stands on its own
  • 10. Why We Need Entity Bundles  Each entity type stores some data in a table – We call these pieces of data “properties” – Eg: node • Title, body, author (uid), created timestamp • But not all nodes are created equal – Page may have data beyond that captured by node – Blog posts get tagged – We need fields!
  • 11. CCK: Content Construction Kit (D6)  Contrib module  Store additional data per content (node) content type  Eg: Fivestar – Contrib module leveraging contrib CCK API – Allows nodes to be rated (think 1-5 stars) – Eg: let users rate pages or blog posts
  • 12. Fields (D7)  Core concept  Store additional data per entity type  Applies power of CCK to all entity types – Not just nodes!  Eg: Fivestar – Contrib module leveraging core Field API – Allows entities to be rated – Eg: let users rate pages, blog posts, users, comments, custom entities
  • 13. Entities in Core vs Contrib  Entities are a Drupal 7 core concept  Core provides the “Entity API” – Functions and hooks to define and interact with entities, entity types, and entity bundles  Everything we've discussed so far is part of Drupal 7 core (no contrib modules necessary)  Not everything “made it into” core – Thankfully there's the “Entity API” contrib module • (confusingly named)
  • 14. Entity API Contrib Module  Makes dealing with entities easier  Provides full CRUD functionality – CReate, Update, and Delete  Allows definition of metadata about entities  Optionally makes entity data – Exportable (for configuration) – Revisionable (eg: node revisions)  Optional administrative UI for managing entities  Object-oriented representation of entity types
  • 15. A Note on Documentation  Documentation for core's Entity API is separate from that for the Entity API contrib module  In practice, you'll probably always use the Entity API contrib module – It provides so much awesome functionality!  Don't get confused – Always install the Entity API contrib module when defining an entity – Make sure to read the module's documentation
  • 16. Contrib Modules Defining Entities  Commerce – Commerce Product, Commerce Payment Transaction  Organic Groups – OG Membership, OG Membership Type  Rules – Rules Configuration  Message (think activity stream) – Message, Message Type, Message Type Category  … and many more!
  • 17. Example Custom Entity Type  TextbookMadness.com – Classified listings for textbooks at schools – Online price comparison shopping (prices from Amazon, Textbooks.com, etc.)  Online prices are – Fetched from a third-party API and stored by Drupal – Hereafter known as Offers
  • 18. How Shall we Define an Offer?  We could use the UI to define an offer node with a bunch of fields to store pricing information  But there's a lot of overhead! – Don't need/want a page (path) per offer – Don't need/want revisions – Don't need/want node base table information • Language (and translation info), Title, Author, Published status, Created date, Commenting, Promoting, etc.  We want an entity!
  • 19. 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') );
  • 20. Implement hook_entity_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', ));
  • 21. Make an Entity 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' ));
  • 22. Make an Entity 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', ));
  • 23. Other hook_entity_info() configuration $entities['tbm_offer'] = array( … 'entity class' => 'TbmOfferEntity', // Extends Entity class 'controller class' => 'TbmOfferEntityController', 'access callback' => 'tbm_offer_access', 'admin ui' => array( 'path' => 'admin/structure/offers', 'file' => 'tbm.admin.inc', ), 'label callback' => 'entity_class_label', 'uri callback' => 'entity_class_uri', ); // TbmOfferEntity->defaultLabel(), defaultUri()
  • 24. Entity Property Info  hook_entity_property_info() – Defines entity metadata – Provided for core entities  Automatically generated from hook_schema() – Doesn't understand foreign key relationships (references) – Doesn't know when an integer is actually a date (eg: unix timestamps)  Fill in the gaps with hook_entity_property_info_alter()
  • 25. hook_entity_property_info_alter() $offer = &$info['tbm_offer']['properties']; $offer['url']['type'] = 'uri'; // was a varchar in hook_schema $offer['url']['label'] = 'URL'; $offer['last_updated']['type'] = 'date'; // integer in hook_schema $offer['last_updated']['label'] = t('Last updated'); // Other types: text, token (machine name), integer, decimal, duration, boolean, entity, struct, list<TYPE>
  • 26. hook_entity_property_info_alter() // Define a new property to reference an offer's retailer (eg: Amazon.com) $offer['retailer'] = array( 'label' => t('Retailer'), 'type' => 'tbm_retailer', // The tbm_retailer entity type 'description' => t('The retailer making the offer.'), 'required' => TRUE, 'schema field' => 'retailer_id', // as defined in hook_schema );
  • 27. Entity API Integrations  Views – Eg: in a view of offers, we can add a retailer relationship and include details about each offer's retailer  Rules  Search API  Features  I18n  Token system - Eg: [tbm_offer:retailer:name]
  • 28. EntityFieldQuery  Tool for querying entities  Can query entity properties and field data  Can query field data across entity types – Eg: “return all pages and users tagged with taxonomy term 456”  Returns entity IDs – Usually you’ll then load with entity_load()
  • 29. EntityFieldQuery 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
  • 30. EntityFieldQuery Example $result = $query->execute(); // Keyed by entity type if (isset($result['node'])) { $nids = array_keys($result['node']); $pages = node_load_multiple($nids); $same_as_pages = entity_load('node', $nids); }
  • 31. Entity 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();
  • 32. Entity Metadata Wrapper Examples // Set node author's email address $wrapper->author->mail = 'me@example.com'; $wrapper->save(); // Iterate over a list of node's taxonomy terms foreach ($wrapper->field_taxonomy_terms->getIterator() as $term_wrapper) { $label = $term_wrapper->label->value(); }
  • 33. More Modules!  Entity Construction Kit (ECK) – CCK for entities – Create entity types in a UI  Entity Cache – Integrates entities with Drupal's Cache API  Entity Reference – Like node reference field, but for any type of entity  Automatic Entity Label – Generic successor to Automatic Node Titles
  • 34. More information  Entity API documentation – Lots of information in many subpages  EntityFieldQuery documentation  Entity API contrib module page  Deck is on Slideshare  Email me: jd at ModernBizConsulting.com  Follow me: @drupal_jd