SlideShare a Scribd company logo
1 of 18
Drupal 7 Entities
                      {    Entities, nodes, fields, the Entity API &
                           TextbookMadness.com




{   JD Leonard
    Freelance Drupal Consultant & Developer
    Founder, TextbookMadness.com                                 { Berkeley DUG 2/27/12
Entities, Entities, Entities
             Powerful new concept in Drupal 7


    Agenda:
        How data is represented
            Drupal 6 vs. Drupal 7
            Entities
            Fields (CCK)
        Entities vs. nodes
        The Entity API (contrib module)
        Live example: entities, nodes, and fields used
         to build TextbookMadness.com
{ Drupal      6                { Drupal      7

     {Custom DB table}            {Custom DB table]
     User                         Entity
     Comment                          User
                                       Comment
     Taxonomy
                                       Taxonomy
     Node
                                       Node
         Page                             Page
         Blog post                        Blog post
         {Custom node type}               {Custom node type}
                                       {Custom entity type}

Drupal Data
Entity Types
    Elemental building block for most data in Drupal 7
    Represents a concept or noun
    In core: user, comment, taxonomy, & node
    Stores data differently
        Generally in a single DB table
        Node
             Title, body, created timestamp
        User
             Username, created timestamp, last visit timestamp
    Defined using hook_entity_info() – example later!
Entity
    Instance of an entity type
    In core:
        Users, comments, taxonomies, and nodes are entities
        Eg: the user jdl1234 with uid 12 is an entity
        Eg: the page ‚Drupal rocks‛ with nid 44 is an entity
    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 wrappers that do extra work
             Eg: node_load() and user_load()
Entity Bundles
    Bundle
        Subtype of an entity
        Eg: page & blog post
             Two content types of node entity type
             ‚Bundles‛ of node entity type
        Not all entity types have more than one
             Eg: user
                  Concept stands on its own
    Why bother?
        Each entity type stores some data in a table
             Eg: node
                  Title, body, created timestamp
                  But not all nodes are created equal
                      Page may have data beyond that captured by node
                      Blog posts get tagged
CCK, Fields, & Entities
    Drupal 6 CCK (Content Construction Kit)
         Contrib module
         Store additional data per content (node) type
         Eg: Fivestar
              Contrib module leveraging contrib CCK API
              Allows nodes to be rated (think 1-5 stars)
                   Eg: let users rate pages
    Drupal 7 fields
         Core concept
         Store additional data per entity type
         Applies power of CCK to all entity types
         Eg: Fivestar
              Contrib module leveraging core Field API
              Allows entities to be rated
                   Eg: let users rate pages, users, comments, custom entities
Entities & Nodes
    Similarities
        Represent ‚things‛; store data
        Fieldable
        Types can be defined in code
{ Nodes                        { Entities
     Site building                 Optionally fieldable
     UI to add/edit                Development
      content types, fields,        Lightweight
      and content                   No UI provided
     Versioning built in           Data storage up to
     Data stored in nodes           you
      table and fields              Can be more
                                     performant/scalable

Entities vs. Nodes
Core Entities & Entity API
    Entities are a Drupal 7 core concept
    Core functions and hooks provided to interact with
     entities and entity types
    EntityFieldQuery
        Core API for querying entities
        Can query entity data (‚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()
    The Entity API contrib module builds on core
Entity API
    Makes dealing with entities easier
    Provides full CRUD functionality
        CReate, Update, and Delete
    Allows definition of metadata about entities and
     relationships between them
        Views integration
        Rules integration
        Search API integration
    Optionally make data exportable
    Optional administrative UI for managing entities
    Object-oriented representation of entity types
        Enables extending an entity type’s functionality
Modules Using Entity API
    Profile2
    Organic Groups
    Rules
    Message
    Drupal commerce
    < and lots more!
TextbookMadness.com
    Drupal 7 site
    Cross between Craigslist and Kayak
        Free textbook classified listings
             Private to each college we partner with
             Helps students buy and sell used books with each other
        Online price comparison
             Prices from 30+ online retailers
                  Amazon, Half, Chegg, Textbooks.com, you name it
             New, used, rental, international edition, eBook, and
              buyback prices
    Uses entities and the Entity API
    Demo!
Textbook Madness Data
    Users (John Smith)
    Schools (Rice University)
    Retailers (Amazon.com
    Textbooks (with an ISBN)
    Offers (online prices)
    Listings (student book classifieds)

    Everything above except users are defined as custom
     entity types
School Entity Definition
 /* Implements hook_entity_info(). // Drupal core hook */
 function tbm_entity_info() {
   $entities = array(
     'tbm_school' => array( // Machine name for my entity
       'label' => t('School'),
       'plural label' => t('Schools'),
       'entity class' => 'TbmSchoolEntity', // Advanced. You can put ‘Entity’
       'controller class' => 'TbmSchoolEntityController', // Advanced. You can put ‘EntityAPIController’
       'base table' => 'tbm_school', // Name of the related table defined in hook_schema() in tbm.install
       'fieldable' => FALSE,
       'entity keys' => array(
         'id' => 'school_id', // Primary key as defined in hook_schema()
         'label' => 'name', // Column in hook_schema() to be used to represent the entity in a friendly way
       ),
       'uri callback' => 'entity_class_uri',
       'access callback' => 'tbm_school_access', // Callback defined to return TRUE if the school is enabled
       'module' => 'tbm', // My module name
       'admin ui' => array(
         'path' => 'admin/structure/schools',
         'file' => 'tbm.admin.inc', // Where tbm_school_form() for add/edit form is defined
         'file path' => drupal_get_path('module', 'tbm') . '/pages',
       ),
     ),
   );
   return $entities;
 }
School Schema Definition
/* Implements hook_schema(). */
function tbm_schema() {
 $schema['tbm_school'] = array(
   'fields' => array(
     'school_id' => array('type' => 'serial', 'size' => 'small', 'unsigned' => TRUE, 'not null' => TRUE),
     'machine_name' => array('type' => 'varchar', 'length' => 100, 'not null' => TRUE, 'description' => 'The machine name of the school.'),
     'name' => array('type' => 'varchar', 'length' => 128, 'not null' => TRUE, 'description' => 'The name of the school.'),
     'name_short' => array('type' => 'varchar', 'length' => 64, 'description' => 'The short name of the school.'),
     'name_long' => array('type' => 'varchar', 'length' => 255, 'description' => 'The long name of the school.'),
     'url' => array('type' => 'varchar', 'length' => 1024, 'not null' => TRUE, 'description' => 'The URL of the school's homepage.'),
     'email_domains' => array('type' => 'varchar', 'length' => 1024, 'description' => 'The email domains (comma delimited) valid for the school.'),
     'sponsor1_name' => array('type' => 'varchar', 'length' => 64, 'description' => 'The name of the first sponsor.'),
     'sponsor1_name_short' => array('type' => 'varchar', 'length' => 32, 'description' => 'The short name of the first sponsor.'),
     'sponsor1_name_long' => array('type' => 'varchar', 'length' => 128, 'description' => 'The long name of the first sponsor.'),
     'sponsor1_url' => array('type' => 'varchar', 'length' => 1024, 'description' => 'The URL of the first sponsor's homepage.'),
     'sponsor2_name' => array('type' => 'varchar', 'length' => 64, 'description' => 'The name of the second sponsor.'),
     'sponsor2_name_short' => array('type' => 'varchar', 'length' => 32, 'description' => 'The short name of the second sponsor.'),
     'sponsor2_name_long' => array('type' => 'varchar', 'length' => 128, 'description' => 'The long name of the second sponsor.'),
     'sponsor2_url' => array('type' => 'varchar', 'length' => 1024, 'description' => 'The URL of the second sponsor's homepage.'),
     'enabled' => array('type' => 'int', 'size' => 'tiny', 'unsigned' => TRUE, 'not null' => TRUE, 'description' => 'Whether the school is enabled.'),
     'listed' => array('type' => 'int', 'size' => 'tiny', 'unsigned' => TRUE, 'not null' => TRUE, 'description' => 'Whether the school is listed.'),
   ),
   'primary key' => array('school_id'),
   'unique keys' => array(
     'machine_name' => array('machine_name'),
   ),
 );
 return $schema;
School EntityFieldQuery
      This function shows how to use an EntityFieldQuery
      Users are necessarily associated with a school
      I have a field, field_school, attached to the user entity type, which
       stores this relationship data
      The query finds active users (status == 1) at the given school


 /**
  * Load an array of uids of users at the school with the given ID.
  * @param int $school_id The ID of the school to load users for
  */
 function tbm_school_uids($school_id) {
   $query = new EntityFieldQuery();

     $result = $query
      ->entityCondition('entity_type', 'user')
      ->propertyCondition('status', 1)
      ->fieldCondition('field_school', 'school_id', $school_id)
      ->execute();

     return array_key_exists('user', $result) ? array_keys($result['user']) : array();
 }
The end!

More Related Content

What's hot

Drupal 8 Sample Module
Drupal 8 Sample ModuleDrupal 8 Sample Module
Drupal 8 Sample Moduledrubb
 
Synapseindia object oriented programming in php
Synapseindia object oriented programming in phpSynapseindia object oriented programming in php
Synapseindia object oriented programming in phpSynapseindiappsdevelopment
 
Advanced php
Advanced phpAdvanced php
Advanced phphamfu
 
Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3Fabien Potencier
 
Decouple Your Code For Reusability (International PHP Conference / IPC 2008)
Decouple Your Code For Reusability (International PHP Conference / IPC 2008)Decouple Your Code For Reusability (International PHP Conference / IPC 2008)
Decouple Your Code For Reusability (International PHP Conference / IPC 2008)Fabien Potencier
 
Dependency injection-zendcon-2010
Dependency injection-zendcon-2010Dependency injection-zendcon-2010
Dependency injection-zendcon-2010Fabien Potencier
 
Object oriented javascript
Object oriented javascriptObject oriented javascript
Object oriented javascriptShah Jalal
 
Symfony2 from the Trenches
Symfony2 from the TrenchesSymfony2 from the Trenches
Symfony2 from the TrenchesJonathan Wage
 
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
 
Doctrine MongoDB Object Document Mapper
Doctrine MongoDB Object Document MapperDoctrine MongoDB Object Document Mapper
Doctrine MongoDB Object Document MapperJonathan Wage
 
50 Laravel Tricks in 50 Minutes
50 Laravel Tricks in 50 Minutes50 Laravel Tricks in 50 Minutes
50 Laravel Tricks in 50 MinutesAzim Kurt
 
ZendCon2010 Doctrine MongoDB ODM
ZendCon2010 Doctrine MongoDB ODMZendCon2010 Doctrine MongoDB ODM
ZendCon2010 Doctrine MongoDB ODMJonathan Wage
 
Cleaner, Leaner, Meaner: Refactoring your jQuery
Cleaner, Leaner, Meaner: Refactoring your jQueryCleaner, Leaner, Meaner: Refactoring your jQuery
Cleaner, Leaner, Meaner: Refactoring your jQueryRebecca Murphey
 

What's hot (19)

Drupal 8 Sample Module
Drupal 8 Sample ModuleDrupal 8 Sample Module
Drupal 8 Sample Module
 
Synapseindia object oriented programming in php
Synapseindia object oriented programming in phpSynapseindia object oriented programming in php
Synapseindia object oriented programming in php
 
Your Entity, Your Code
Your Entity, Your CodeYour Entity, Your Code
Your Entity, Your Code
 
Advanced php
Advanced phpAdvanced php
Advanced php
 
J query1
J query1J query1
J query1
 
Field api.From d7 to d8
Field api.From d7 to d8Field api.From d7 to d8
Field api.From d7 to d8
 
J query
J queryJ query
J query
 
Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3
 
Decouple Your Code For Reusability (International PHP Conference / IPC 2008)
Decouple Your Code For Reusability (International PHP Conference / IPC 2008)Decouple Your Code For Reusability (International PHP Conference / IPC 2008)
Decouple Your Code For Reusability (International PHP Conference / IPC 2008)
 
Dependency injection-zendcon-2010
Dependency injection-zendcon-2010Dependency injection-zendcon-2010
Dependency injection-zendcon-2010
 
Object oriented javascript
Object oriented javascriptObject oriented javascript
Object oriented javascript
 
Doctrine and NoSQL
Doctrine and NoSQLDoctrine and NoSQL
Doctrine and NoSQL
 
Doctrine for NoSQL
Doctrine for NoSQLDoctrine for NoSQL
Doctrine for NoSQL
 
Symfony2 from the Trenches
Symfony2 from the TrenchesSymfony2 from the Trenches
Symfony2 from the Trenches
 
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
 
Doctrine MongoDB Object Document Mapper
Doctrine MongoDB Object Document MapperDoctrine MongoDB Object Document Mapper
Doctrine MongoDB Object Document Mapper
 
50 Laravel Tricks in 50 Minutes
50 Laravel Tricks in 50 Minutes50 Laravel Tricks in 50 Minutes
50 Laravel Tricks in 50 Minutes
 
ZendCon2010 Doctrine MongoDB ODM
ZendCon2010 Doctrine MongoDB ODMZendCon2010 Doctrine MongoDB ODM
ZendCon2010 Doctrine MongoDB ODM
 
Cleaner, Leaner, Meaner: Refactoring your jQuery
Cleaner, Leaner, Meaner: Refactoring your jQueryCleaner, Leaner, Meaner: Refactoring your jQuery
Cleaner, Leaner, Meaner: Refactoring your jQuery
 

Viewers also liked

Introduction to Drupal (7) Theming
Introduction to Drupal (7) ThemingIntroduction to Drupal (7) Theming
Introduction to Drupal (7) ThemingRobert Carr
 
jQuery UI Widgets, Drag and Drop, Drupal 7 Javascript
jQuery UI Widgets, Drag and Drop, Drupal 7 JavascriptjQuery UI Widgets, Drag and Drop, Drupal 7 Javascript
jQuery UI Widgets, Drag and Drop, Drupal 7 JavascriptDarren Mothersele
 
JavaScript in Drupal 7: What developers need to know
JavaScript in Drupal 7: What developers need to knowJavaScript in Drupal 7: What developers need to know
JavaScript in Drupal 7: What developers need to knowkatbailey
 
Node Access in Drupal 7 (and Drupal 8)
Node Access in Drupal 7 (and Drupal 8)Node Access in Drupal 7 (and Drupal 8)
Node Access in Drupal 7 (and Drupal 8)nyccamp
 
Introdução ao drupal 7
Introdução ao drupal 7Introdução ao drupal 7
Introdução ao drupal 7Sander Fortuna
 
Making Drupal 7 Simple to Use for Everyone
Making Drupal 7 Simple to Use for EveryoneMaking Drupal 7 Simple to Use for Everyone
Making Drupal 7 Simple to Use for EveryoneAcquia
 
Building and Maintaining a Distribution in Drupal 7 with Features
Building and Maintaining a  Distribution in Drupal 7 with FeaturesBuilding and Maintaining a  Distribution in Drupal 7 with Features
Building and Maintaining a Distribution in Drupal 7 with FeaturesNuvole
 
From Drupal 7 to Drupal 8 - Drupal Intensive Course Overview
From Drupal 7 to Drupal 8 - Drupal Intensive Course OverviewFrom Drupal 7 to Drupal 8 - Drupal Intensive Course Overview
From Drupal 7 to Drupal 8 - Drupal Intensive Course OverviewItalo Mairo
 

Viewers also liked (9)

Introduction to Drupal (7) Theming
Introduction to Drupal (7) ThemingIntroduction to Drupal (7) Theming
Introduction to Drupal (7) Theming
 
jQuery UI Widgets, Drag and Drop, Drupal 7 Javascript
jQuery UI Widgets, Drag and Drop, Drupal 7 JavascriptjQuery UI Widgets, Drag and Drop, Drupal 7 Javascript
jQuery UI Widgets, Drag and Drop, Drupal 7 Javascript
 
JavaScript in Drupal 7: What developers need to know
JavaScript in Drupal 7: What developers need to knowJavaScript in Drupal 7: What developers need to know
JavaScript in Drupal 7: What developers need to know
 
Node Access in Drupal 7 (and Drupal 8)
Node Access in Drupal 7 (and Drupal 8)Node Access in Drupal 7 (and Drupal 8)
Node Access in Drupal 7 (and Drupal 8)
 
Introdução ao drupal 7
Introdução ao drupal 7Introdução ao drupal 7
Introdução ao drupal 7
 
Making Drupal 7 Simple to Use for Everyone
Making Drupal 7 Simple to Use for EveryoneMaking Drupal 7 Simple to Use for Everyone
Making Drupal 7 Simple to Use for Everyone
 
Building and Maintaining a Distribution in Drupal 7 with Features
Building and Maintaining a  Distribution in Drupal 7 with FeaturesBuilding and Maintaining a  Distribution in Drupal 7 with Features
Building and Maintaining a Distribution in Drupal 7 with Features
 
From Drupal 7 to Drupal 8 - Drupal Intensive Course Overview
From Drupal 7 to Drupal 8 - Drupal Intensive Course OverviewFrom Drupal 7 to Drupal 8 - Drupal Intensive Course Overview
From Drupal 7 to Drupal 8 - Drupal Intensive Course Overview
 
Drupal 7
Drupal 7Drupal 7
Drupal 7
 

Similar to Drupal 7 entities & TextbookMadness.com

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
 
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
 
Understanding the Entity API Module
Understanding the Entity API ModuleUnderstanding the Entity API Module
Understanding the Entity API ModuleSergiu Savva
 
D7 entities fields
D7 entities fieldsD7 entities fields
D7 entities fieldscyberswat
 
Entity Framework Database and Code First
Entity Framework Database and Code FirstEntity Framework Database and Code First
Entity Framework Database and Code FirstJames Johnson
 
Ejb3 Struts Tutorial En
Ejb3 Struts Tutorial EnEjb3 Struts Tutorial En
Ejb3 Struts Tutorial EnAnkur Dongre
 
Ejb3 Struts Tutorial En
Ejb3 Struts Tutorial EnEjb3 Struts Tutorial En
Ejb3 Struts Tutorial EnAnkur Dongre
 
The Magic Revealed: Four Real-World Examples of Using the Client Object Model...
The Magic Revealed: Four Real-World Examples of Using the Client Object Model...The Magic Revealed: Four Real-World Examples of Using the Client Object Model...
The Magic Revealed: Four Real-World Examples of Using the Client Object Model...SPTechCon
 
Drupalcon cph
Drupalcon cphDrupalcon cph
Drupalcon cphcyberswat
 
Drupal 8: Entities
Drupal 8: EntitiesDrupal 8: Entities
Drupal 8: Entitiesdrubb
 
one|content : joomla on steroids
one|content : joomla on steroidsone|content : joomla on steroids
one|content : joomla on steroidsPaul Delbar
 
Apache doris (incubating) introduction
Apache doris (incubating) introductionApache doris (incubating) introduction
Apache doris (incubating) introductionleanderlee2
 
Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...
Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...
Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...Bill Buchan
 
Hibernate Training Session1
Hibernate Training Session1Hibernate Training Session1
Hibernate Training Session1Asad Khan
 

Similar to Drupal 7 entities & TextbookMadness.com (20)

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)
 
Drupal 7 Entity & Entity API
Drupal 7 Entity & Entity APIDrupal 7 Entity & Entity API
Drupal 7 Entity & Entity API
 
Drupal 8 Hooks
Drupal 8 HooksDrupal 8 Hooks
Drupal 8 Hooks
 
Your Entity, Your Code
Your Entity, Your CodeYour Entity, Your Code
Your Entity, Your Code
 
Understanding the Entity API Module
Understanding the Entity API ModuleUnderstanding the Entity API Module
Understanding the Entity API Module
 
D7 entities fields
D7 entities fieldsD7 entities fields
D7 entities fields
 
Drupal 8: Fields reborn
Drupal 8: Fields rebornDrupal 8: Fields reborn
Drupal 8: Fields reborn
 
La sql
La sqlLa sql
La sql
 
Entity Framework Database and Code First
Entity Framework Database and Code FirstEntity Framework Database and Code First
Entity Framework Database and Code First
 
Ejb3 Struts Tutorial En
Ejb3 Struts Tutorial EnEjb3 Struts Tutorial En
Ejb3 Struts Tutorial En
 
Ejb3 Struts Tutorial En
Ejb3 Struts Tutorial EnEjb3 Struts Tutorial En
Ejb3 Struts Tutorial En
 
Hibernate II
Hibernate IIHibernate II
Hibernate II
 
The Magic Revealed: Four Real-World Examples of Using the Client Object Model...
The Magic Revealed: Four Real-World Examples of Using the Client Object Model...The Magic Revealed: Four Real-World Examples of Using the Client Object Model...
The Magic Revealed: Four Real-World Examples of Using the Client Object Model...
 
Drupalcon cph
Drupalcon cphDrupalcon cph
Drupalcon cph
 
Drupal 8: Entities
Drupal 8: EntitiesDrupal 8: Entities
Drupal 8: Entities
 
Classes1
Classes1Classes1
Classes1
 
one|content : joomla on steroids
one|content : joomla on steroidsone|content : joomla on steroids
one|content : joomla on steroids
 
Apache doris (incubating) introduction
Apache doris (incubating) introductionApache doris (incubating) introduction
Apache doris (incubating) introduction
 
Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...
Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...
Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...
 
Hibernate Training Session1
Hibernate Training Session1Hibernate Training Session1
Hibernate Training Session1
 

Recently uploaded

Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
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
 
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
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
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
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 
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
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
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
 

Recently uploaded (20)

Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
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)
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
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
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 
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
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
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
 

Drupal 7 entities & TextbookMadness.com

  • 1. Drupal 7 Entities { Entities, nodes, fields, the Entity API & TextbookMadness.com { JD Leonard Freelance Drupal Consultant & Developer Founder, TextbookMadness.com { Berkeley DUG 2/27/12
  • 2. Entities, Entities, Entities Powerful new concept in Drupal 7  Agenda:  How data is represented  Drupal 6 vs. Drupal 7  Entities  Fields (CCK)  Entities vs. nodes  The Entity API (contrib module)  Live example: entities, nodes, and fields used to build TextbookMadness.com
  • 3. { Drupal 6 { Drupal 7  {Custom DB table}  {Custom DB table]  User  Entity  Comment  User  Comment  Taxonomy  Taxonomy  Node  Node  Page  Page  Blog post  Blog post  {Custom node type}  {Custom node type}  {Custom entity type} Drupal Data
  • 4. Entity Types  Elemental building block for most data in Drupal 7  Represents a concept or noun  In core: user, comment, taxonomy, & node  Stores data differently  Generally in a single DB table  Node  Title, body, created timestamp  User  Username, created timestamp, last visit timestamp  Defined using hook_entity_info() – example later!
  • 5. Entity  Instance of an entity type  In core:  Users, comments, taxonomies, and nodes are entities  Eg: the user jdl1234 with uid 12 is an entity  Eg: the page ‚Drupal rocks‛ with nid 44 is an entity  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 wrappers that do extra work  Eg: node_load() and user_load()
  • 6. Entity Bundles  Bundle  Subtype of an entity  Eg: page & blog post  Two content types of node entity type  ‚Bundles‛ of node entity type  Not all entity types have more than one  Eg: user  Concept stands on its own  Why bother?  Each entity type stores some data in a table  Eg: node  Title, body, created timestamp  But not all nodes are created equal  Page may have data beyond that captured by node  Blog posts get tagged
  • 7. CCK, Fields, & Entities  Drupal 6 CCK (Content Construction Kit)  Contrib module  Store additional data per content (node) type  Eg: Fivestar  Contrib module leveraging contrib CCK API  Allows nodes to be rated (think 1-5 stars)  Eg: let users rate pages  Drupal 7 fields  Core concept  Store additional data per entity type  Applies power of CCK to all entity types  Eg: Fivestar  Contrib module leveraging core Field API  Allows entities to be rated  Eg: let users rate pages, users, comments, custom entities
  • 8. Entities & Nodes  Similarities  Represent ‚things‛; store data  Fieldable  Types can be defined in code
  • 9. { Nodes { Entities  Site building  Optionally fieldable  UI to add/edit  Development content types, fields,  Lightweight and content  No UI provided  Versioning built in  Data storage up to  Data stored in nodes you table and fields  Can be more performant/scalable Entities vs. Nodes
  • 10. Core Entities & Entity API  Entities are a Drupal 7 core concept  Core functions and hooks provided to interact with entities and entity types  EntityFieldQuery  Core API for querying entities  Can query entity data (‚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()  The Entity API contrib module builds on core
  • 11. Entity API  Makes dealing with entities easier  Provides full CRUD functionality  CReate, Update, and Delete  Allows definition of metadata about entities and relationships between them  Views integration  Rules integration  Search API integration  Optionally make data exportable  Optional administrative UI for managing entities  Object-oriented representation of entity types  Enables extending an entity type’s functionality
  • 12. Modules Using Entity API  Profile2  Organic Groups  Rules  Message  Drupal commerce  < and lots more!
  • 13. TextbookMadness.com  Drupal 7 site  Cross between Craigslist and Kayak  Free textbook classified listings  Private to each college we partner with  Helps students buy and sell used books with each other  Online price comparison  Prices from 30+ online retailers  Amazon, Half, Chegg, Textbooks.com, you name it  New, used, rental, international edition, eBook, and buyback prices  Uses entities and the Entity API  Demo!
  • 14. Textbook Madness Data  Users (John Smith)  Schools (Rice University)  Retailers (Amazon.com  Textbooks (with an ISBN)  Offers (online prices)  Listings (student book classifieds)  Everything above except users are defined as custom entity types
  • 15. School Entity Definition /* Implements hook_entity_info(). // Drupal core hook */ function tbm_entity_info() { $entities = array( 'tbm_school' => array( // Machine name for my entity 'label' => t('School'), 'plural label' => t('Schools'), 'entity class' => 'TbmSchoolEntity', // Advanced. You can put ‘Entity’ 'controller class' => 'TbmSchoolEntityController', // Advanced. You can put ‘EntityAPIController’ 'base table' => 'tbm_school', // Name of the related table defined in hook_schema() in tbm.install 'fieldable' => FALSE, 'entity keys' => array( 'id' => 'school_id', // Primary key as defined in hook_schema() 'label' => 'name', // Column in hook_schema() to be used to represent the entity in a friendly way ), 'uri callback' => 'entity_class_uri', 'access callback' => 'tbm_school_access', // Callback defined to return TRUE if the school is enabled 'module' => 'tbm', // My module name 'admin ui' => array( 'path' => 'admin/structure/schools', 'file' => 'tbm.admin.inc', // Where tbm_school_form() for add/edit form is defined 'file path' => drupal_get_path('module', 'tbm') . '/pages', ), ), ); return $entities; }
  • 16. School Schema Definition /* Implements hook_schema(). */ function tbm_schema() { $schema['tbm_school'] = array( 'fields' => array( 'school_id' => array('type' => 'serial', 'size' => 'small', 'unsigned' => TRUE, 'not null' => TRUE), 'machine_name' => array('type' => 'varchar', 'length' => 100, 'not null' => TRUE, 'description' => 'The machine name of the school.'), 'name' => array('type' => 'varchar', 'length' => 128, 'not null' => TRUE, 'description' => 'The name of the school.'), 'name_short' => array('type' => 'varchar', 'length' => 64, 'description' => 'The short name of the school.'), 'name_long' => array('type' => 'varchar', 'length' => 255, 'description' => 'The long name of the school.'), 'url' => array('type' => 'varchar', 'length' => 1024, 'not null' => TRUE, 'description' => 'The URL of the school's homepage.'), 'email_domains' => array('type' => 'varchar', 'length' => 1024, 'description' => 'The email domains (comma delimited) valid for the school.'), 'sponsor1_name' => array('type' => 'varchar', 'length' => 64, 'description' => 'The name of the first sponsor.'), 'sponsor1_name_short' => array('type' => 'varchar', 'length' => 32, 'description' => 'The short name of the first sponsor.'), 'sponsor1_name_long' => array('type' => 'varchar', 'length' => 128, 'description' => 'The long name of the first sponsor.'), 'sponsor1_url' => array('type' => 'varchar', 'length' => 1024, 'description' => 'The URL of the first sponsor's homepage.'), 'sponsor2_name' => array('type' => 'varchar', 'length' => 64, 'description' => 'The name of the second sponsor.'), 'sponsor2_name_short' => array('type' => 'varchar', 'length' => 32, 'description' => 'The short name of the second sponsor.'), 'sponsor2_name_long' => array('type' => 'varchar', 'length' => 128, 'description' => 'The long name of the second sponsor.'), 'sponsor2_url' => array('type' => 'varchar', 'length' => 1024, 'description' => 'The URL of the second sponsor's homepage.'), 'enabled' => array('type' => 'int', 'size' => 'tiny', 'unsigned' => TRUE, 'not null' => TRUE, 'description' => 'Whether the school is enabled.'), 'listed' => array('type' => 'int', 'size' => 'tiny', 'unsigned' => TRUE, 'not null' => TRUE, 'description' => 'Whether the school is listed.'), ), 'primary key' => array('school_id'), 'unique keys' => array( 'machine_name' => array('machine_name'), ), ); return $schema;
  • 17. School EntityFieldQuery  This function shows how to use an EntityFieldQuery  Users are necessarily associated with a school  I have a field, field_school, attached to the user entity type, which stores this relationship data  The query finds active users (status == 1) at the given school /** * Load an array of uids of users at the school with the given ID. * @param int $school_id The ID of the school to load users for */ function tbm_school_uids($school_id) { $query = new EntityFieldQuery(); $result = $query ->entityCondition('entity_type', 'user') ->propertyCondition('status', 1) ->fieldCondition('field_school', 'school_id', $school_id) ->execute(); return array_key_exists('user', $result) ? array_keys($result['user']) : array(); }