SlideShare a Scribd company logo
1 of 30
Entity API
Understanding the Entity API Module
Sergiu Savva
Drupal developerwearepropeople.com
Summary
1. An introduction to entities
2. Entity types
3. Bundles
4. Fields
5. Entity
6. Entity API
7. Entity metadata wrapper
8. Getters ,Setters, Multilingual
9. Entity + From API + Field API
An Introduction to Entities
We can build Entity types, which can make
Bundles, to which we can add Fields and
then create Entities.
Entity types > Bundles > Fields > Entities
Entity types
Fieldable entities make Drupal eminently
flexible. An entity type is a useful abstraction
to group together fields.
Entity types
● Nodes (content)
● User profiles
● Taxonomy terms
● Comments
Some examples of entity types:
Entity types
● label - The human-readable name of the type.
● controller class - The name of the class that is used to load the
objects.
● fieldable - Set to TRUE if you want your entity type to accept fields
being attached to it.
● bundles - An array describing all bundles for this object type.
● view modes - An array describing the view modes for the entity
type.
You can also build new kinds of entity types if the
options above don't suit your needs.
Read further about using the hook_entity_info()
Bundles
Bundles are an implementation of an entity type
to which fields can be attached. You can consider
bundles as subtypes of an entity type.
Bundles for entity type node:
Fields
Fields can be added to any of the bundles or
entity types to help organize their data.
Entity
An entity would be one instance of a particular
entity type such as a comment, taxonomy term,
user profile or a bundle such as a blog post, article
or product.
● An entity type is a base class
● A bundle is an extended class
● A field is a class member, property, variable or field
instance (depending on your naming preference)
● An entity is an object or instance of a base or extended
class
Relation Entity - Field
Entity Fields
title
body
taxonomy
some text
number
Bundles
Node Page
News title
body
taxonomy
some text
number
Entity
Entity functions from core:
entity_get_controller Get the entity controller class for an entity type.
entity_get_info Get the entity info array of an entity type.
entity_label Returns the label of an entity.
entity_language Returns the language of an entity.
entity_load Load entities from the database.
entity_load_unchanged Loads the unchanged, i.e. not modified, entity from the
database.
entity_prepare_view Invoke hook_entity_prepare_view().
entity_uri Returns the URI elements of an entity.
Core entities
entity_get_info($entity_type = NULL)
File : common.inc
Get the entity info array of an entity type.
Entity vs. Entity API
Entity API (contrib)
The project Entity API extends the entity API of Drupal
core in order to provide an unified way to deal with
entities and their properties.
Additionally, it provides an entity CRUD* controller, which
helps with simplifying the creation of new entity types.
[ CRUD - Create, read, update and delete ]
Entity API (contrib)
Name Description
entity_access Determines whether the given user has access to an entity.
entity_create Create a new entity object.
entity_delete Permanently delete the given entity.
entity_delete_multiple Permanently delete multiple entities.
entity_export Exports an entity.
entity_import Imports an entity.
entity_load_single A wrapper around entity_load() to load a single entity by name or numeric id.
entity_metadata_wrapper Returns a property wrapper for the given data.
entity_revision_delete Deletes an entity revision.
entity_revision_load Loads an entity revision.
entity_save Permanently save an entity.
entity_theme Implements hook_theme().
entity_type_is_fieldable Checks whether an entity type is fieldable.
entity_view Generate an array for rendering the given entities.
entity_get_property_info Get the entity property info array of an entity type.
Entity API hooks
● hook_entity_view
● hook_entity_insert
● hook_entity_update
● hook_entity_presave
● hook_entity_delete
● hook_entity_load
Entity metadata wrapper
Why use entity metadata wrappers?
● Makes your code more readable
● Provides a standardised way of accessing field values and entities
through an API
● Stops you hard coding the language key into the array lookups
● Stops those nasty PHP warnings when you are trying to access
properties that do not exist
● The wrapper autoloads entities (when used in conjunction with
the ->value() accessor), which allow you to chain the callbacks
Entity metadata wrapper
$wrapper = entity_metadata_wrapper('node', $nid);
$mail = $wrapper-->author---->mail-->value();
$wrapper-->author-->mail-->set('fago@example.com');
$text = $wrapper-->field_text-->value();
$wrapper-->language('de')-->field_text-->value();
$terms = $wrapper-->field_tags-->value();
$wrapper-->field_tags[] = $term;
$options = $wrapper-->field_tags-->optionsList();
$label = $wrapper-->field_tags[0]-->label();
$access = $wrapper-->field_tags-->access('edit');
Entity metadata wrapper
$node = entity_load_single('node',$nid);
$entity_wrapper = entity_metadata_wrapper('node', $node);
$entity_wrapper = entity_metadata_wrapper('node', $nid);
Load
or
Entity metadata wrapper
Getters
$entity_wrapper = entity_metadata_wrapper('node', 1);
dpm($entity_wrapper->field_product->raw());
dpm($entity_wrapper->field_product->value());
Entity metadata wrapper
Getters
$entity_wrapper = entity_metadata_wrapper('node', 1);
dpm($entity_wrapper->value());
Entity metadata wrapper
$wrapper = entity_metadata_wrapper('node', 1);
$mail = $wrapper->author->mail->value();
$wrapper->author->mail->set('foo@mail.com');
$wrapper->author->mail = 'foo@mail.com';
$wrapper->author->save();
Setters
Entity metadata wrapper
Multilingual
$wrapper = entity_metadata_wrapper('node',1);
dpm($wrapper->title_field->value());
dpm($wrapper->language('ro')->title_field->value());
dpm($wrapper->language('ru')->title_field->value());
Entity metadata wrapper in life
// add the items from the 'show_features' taxonomy
$show_features = array();
$features = $series->field_features;
if (is_array($features[LANGUAGE_NONE])) {
foreach($features[LANGUAGE_NONE] as $tids) {
$tid = $tids['tid'];
$term = taxonomy_term_load($tid);
$featureList[] = $term->name;
}
}
Old approach
Entity metadata wrapper in life
// add the items from the 'show_features' taxonomy
$show_features = array();
foreach ($wrapper->field_features->value() => $feature) {
$show_features[] = $feature->name;
}
Entity metadata wrapper approach
Entity + From API + Field API
Entity + Form API + Field API
function myForm($form, $form_state){
if (empty($id)){
$entity = entity_create($entityType, array('type' => $bundleName));
} else {
$entity = entity_load_single($entityType, $id);
}
field_attach_form($entityType, $entity, $form, $form_state);
$from['#submit'][] = 'myCustomSubmit';
return $form;
}
function myCustomSubmit($form, $form_state){
$entity = $form_state['values']['myEntity'];
field_attach_submit($entity_type, $entity, $form, $form_state);
}
Entity + Form API + Field API
function myForm($form, $form_state){
$from['my_data'] = array(
'#type' => 'container',
'#parents' => array('my_data'),
'#tree' => TRUE,
);
field_attach_form($entityType, $entity, $form[my_data], $form_state);
$from['#submit'][] = 'myCustomSubmit';
return $form;
}
function myCustomSubmit($form, $form_state){
$entity = $form_state['values']['my_data']['myEntity'];
field_attach_submit($entity_type, $entity, $form['my_data'], $form_state);
}
Thank you!

More Related Content

What's hot

Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to JavascriptAmit Tyagi
 
Object Oriented Programming in Python
Object Oriented Programming in PythonObject Oriented Programming in Python
Object Oriented Programming in PythonSujith Kumar
 
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
 
Beginners Guide to Object Orientation in PHP
Beginners Guide to Object Orientation in PHPBeginners Guide to Object Orientation in PHP
Beginners Guide to Object Orientation in PHPRick Ogden
 
Java core - Detailed Overview
Java  core - Detailed OverviewJava  core - Detailed Overview
Java core - Detailed OverviewBuddha Tree
 
JavaScript - An Introduction
JavaScript - An IntroductionJavaScript - An Introduction
JavaScript - An IntroductionManvendra Singh
 
Drupal 8 entities & felds
Drupal 8 entities & feldsDrupal 8 entities & felds
Drupal 8 entities & feldsAndy Postnikov
 
Python Programming - VIII. Inheritance and Polymorphism
Python Programming - VIII. Inheritance and PolymorphismPython Programming - VIII. Inheritance and Polymorphism
Python Programming - VIII. Inheritance and PolymorphismRanel Padon
 
Lab #2: Introduction to Javascript
Lab #2: Introduction to JavascriptLab #2: Introduction to Javascript
Lab #2: Introduction to JavascriptWalid Ashraf
 
JavaScript Tutorial
JavaScript  TutorialJavaScript  Tutorial
JavaScript TutorialBui Kiet
 
Drupal 7 Entity & Entity API
Drupal 7 Entity & Entity APIDrupal 7 Entity & Entity API
Drupal 7 Entity & Entity API均民 戴
 
JavaScript, VBScript, AJAX, CGI
JavaScript, VBScript, AJAX, CGIJavaScript, VBScript, AJAX, CGI
JavaScript, VBScript, AJAX, CGIAashish Jain
 
JavaScript: The Language
JavaScript: The LanguageJavaScript: The Language
JavaScript: The LanguageEngage Software
 

What's hot (19)

Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to Javascript
 
Object Oriented Programming in Python
Object Oriented Programming in PythonObject Oriented Programming in Python
Object Oriented Programming in Python
 
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)
 
Beginners Guide to Object Orientation in PHP
Beginners Guide to Object Orientation in PHPBeginners Guide to Object Orientation in PHP
Beginners Guide to Object Orientation in PHP
 
Object oriented programming in python
Object oriented programming in pythonObject oriented programming in python
Object oriented programming in python
 
Java core - Detailed Overview
Java  core - Detailed OverviewJava  core - Detailed Overview
Java core - Detailed Overview
 
JavaScript - An Introduction
JavaScript - An IntroductionJavaScript - An Introduction
JavaScript - An Introduction
 
Javascript functions
Javascript functionsJavascript functions
Javascript functions
 
Drupal 8 entities & felds
Drupal 8 entities & feldsDrupal 8 entities & felds
Drupal 8 entities & felds
 
Python Programming - VIII. Inheritance and Polymorphism
Python Programming - VIII. Inheritance and PolymorphismPython Programming - VIII. Inheritance and Polymorphism
Python Programming - VIII. Inheritance and Polymorphism
 
Lab #2: Introduction to Javascript
Lab #2: Introduction to JavascriptLab #2: Introduction to Javascript
Lab #2: Introduction to Javascript
 
JavaScript Tutorial
JavaScript  TutorialJavaScript  Tutorial
JavaScript Tutorial
 
Javascript basics
Javascript basicsJavascript basics
Javascript basics
 
Drupal 7 Entity & Entity API
Drupal 7 Entity & Entity APIDrupal 7 Entity & Entity API
Drupal 7 Entity & Entity API
 
Basics of JavaScript
Basics of JavaScriptBasics of JavaScript
Basics of JavaScript
 
Javascript
JavascriptJavascript
Javascript
 
Introduction to JavaScript Basics.
Introduction to JavaScript Basics.Introduction to JavaScript Basics.
Introduction to JavaScript Basics.
 
JavaScript, VBScript, AJAX, CGI
JavaScript, VBScript, AJAX, CGIJavaScript, VBScript, AJAX, CGI
JavaScript, VBScript, AJAX, CGI
 
JavaScript: The Language
JavaScript: The LanguageJavaScript: The Language
JavaScript: The Language
 

Similar to Understanding the Entity API Module

Entities in drupal 7
Entities in drupal 7Entities in drupal 7
Entities in drupal 7Zsolt Tasnadi
 
Drupal 7 entities & TextbookMadness.com
Drupal 7 entities & TextbookMadness.comDrupal 7 entities & TextbookMadness.com
Drupal 7 entities & TextbookMadness.comJD Leonard
 
.NET Core, ASP.NET Core Course, Session 13
.NET Core, ASP.NET Core Course, Session 13.NET Core, ASP.NET Core Course, Session 13
.NET Core, ASP.NET Core Course, Session 13aminmesbahi
 
Entities, Bundles, and Fields: You need to understand this!
Entities, Bundles, and Fields: You need to understand this!Entities, Bundles, and Fields: You need to understand this!
Entities, Bundles, and Fields: You need to understand this!tedbow
 
Working with oro crm entities
Working with oro crm entitiesWorking with oro crm entities
Working with oro crm entitiesOro Inc.
 
Object-oriented programming
Object-oriented programmingObject-oriented programming
Object-oriented programmingNeelesh Shukla
 
Hibernate Training Session1
Hibernate Training Session1Hibernate Training Session1
Hibernate Training Session1Asad Khan
 
Создание собственных сущностей с использованием Entity API
Создание собственных сущностей с использованием Entity APIСоздание собственных сущностей с использованием Entity API
Создание собственных сущностей с использованием Entity APIDrupalForumZP2012
 
ActionScript 3.0 Fundamentals
ActionScript 3.0 FundamentalsActionScript 3.0 Fundamentals
ActionScript 3.0 FundamentalsSaurabh Narula
 
Linq in C# 3.0: An Overview
Linq in C# 3.0: An OverviewLinq in C# 3.0: An Overview
Linq in C# 3.0: An Overviewpradeepkothiyal
 
.NET Portfolio
.NET Portfolio.NET Portfolio
.NET Portfoliomwillmer
 
Batch Scripting with Drupal (Featuring the EntityFieldQuery API)
Batch Scripting with Drupal (Featuring the EntityFieldQuery API)Batch Scripting with Drupal (Featuring the EntityFieldQuery API)
Batch Scripting with Drupal (Featuring the EntityFieldQuery API)Ranel Padon
 

Similar to Understanding the Entity API Module (20)

Entities in drupal 7
Entities in drupal 7Entities in drupal 7
Entities in drupal 7
 
Drupal 7 entities & TextbookMadness.com
Drupal 7 entities & TextbookMadness.comDrupal 7 entities & TextbookMadness.com
Drupal 7 entities & TextbookMadness.com
 
Entity api
Entity apiEntity api
Entity api
 
Drupal 7 field API
Drupal 7 field APIDrupal 7 field API
Drupal 7 field API
 
.NET Core, ASP.NET Core Course, Session 13
.NET Core, ASP.NET Core Course, Session 13.NET Core, ASP.NET Core Course, Session 13
.NET Core, ASP.NET Core Course, Session 13
 
Entities, Bundles, and Fields: You need to understand this!
Entities, Bundles, and Fields: You need to understand this!Entities, Bundles, and Fields: You need to understand this!
Entities, Bundles, and Fields: You need to understand this!
 
Linq
LinqLinq
Linq
 
Linq
LinqLinq
Linq
 
Working with oro crm entities
Working with oro crm entitiesWorking with oro crm entities
Working with oro crm entities
 
Object-oriented programming
Object-oriented programmingObject-oriented programming
Object-oriented programming
 
Hibernate Training Session1
Hibernate Training Session1Hibernate Training Session1
Hibernate Training Session1
 
Entity Framework 4
Entity Framework 4Entity Framework 4
Entity Framework 4
 
Создание собственных сущностей с использованием Entity API
Создание собственных сущностей с использованием Entity APIСоздание собственных сущностей с использованием Entity API
Создание собственных сущностей с использованием Entity API
 
iOS Application Development
iOS Application DevelopmentiOS Application Development
iOS Application Development
 
ActionScript 3.0 Fundamentals
ActionScript 3.0 FundamentalsActionScript 3.0 Fundamentals
ActionScript 3.0 Fundamentals
 
Introduction to Datastore
Introduction to DatastoreIntroduction to Datastore
Introduction to Datastore
 
Linq in C# 3.0: An Overview
Linq in C# 3.0: An OverviewLinq in C# 3.0: An Overview
Linq in C# 3.0: An Overview
 
.NET Portfolio
.NET Portfolio.NET Portfolio
.NET Portfolio
 
My c++
My c++My c++
My c++
 
Batch Scripting with Drupal (Featuring the EntityFieldQuery API)
Batch Scripting with Drupal (Featuring the EntityFieldQuery API)Batch Scripting with Drupal (Featuring the EntityFieldQuery API)
Batch Scripting with Drupal (Featuring the EntityFieldQuery API)
 

Recently uploaded

Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 

Recently uploaded (20)

Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 

Understanding the Entity API Module

  • 1. Entity API Understanding the Entity API Module Sergiu Savva Drupal developerwearepropeople.com
  • 2. Summary 1. An introduction to entities 2. Entity types 3. Bundles 4. Fields 5. Entity 6. Entity API 7. Entity metadata wrapper 8. Getters ,Setters, Multilingual 9. Entity + From API + Field API
  • 3. An Introduction to Entities We can build Entity types, which can make Bundles, to which we can add Fields and then create Entities. Entity types > Bundles > Fields > Entities
  • 4. Entity types Fieldable entities make Drupal eminently flexible. An entity type is a useful abstraction to group together fields.
  • 5. Entity types ● Nodes (content) ● User profiles ● Taxonomy terms ● Comments Some examples of entity types:
  • 6. Entity types ● label - The human-readable name of the type. ● controller class - The name of the class that is used to load the objects. ● fieldable - Set to TRUE if you want your entity type to accept fields being attached to it. ● bundles - An array describing all bundles for this object type. ● view modes - An array describing the view modes for the entity type. You can also build new kinds of entity types if the options above don't suit your needs. Read further about using the hook_entity_info()
  • 7. Bundles Bundles are an implementation of an entity type to which fields can be attached. You can consider bundles as subtypes of an entity type. Bundles for entity type node:
  • 8. Fields Fields can be added to any of the bundles or entity types to help organize their data.
  • 9. Entity An entity would be one instance of a particular entity type such as a comment, taxonomy term, user profile or a bundle such as a blog post, article or product. ● An entity type is a base class ● A bundle is an extended class ● A field is a class member, property, variable or field instance (depending on your naming preference) ● An entity is an object or instance of a base or extended class
  • 10. Relation Entity - Field Entity Fields title body taxonomy some text number Bundles Node Page News title body taxonomy some text number
  • 11. Entity Entity functions from core: entity_get_controller Get the entity controller class for an entity type. entity_get_info Get the entity info array of an entity type. entity_label Returns the label of an entity. entity_language Returns the language of an entity. entity_load Load entities from the database. entity_load_unchanged Loads the unchanged, i.e. not modified, entity from the database. entity_prepare_view Invoke hook_entity_prepare_view(). entity_uri Returns the URI elements of an entity.
  • 12. Core entities entity_get_info($entity_type = NULL) File : common.inc Get the entity info array of an entity type.
  • 14. Entity API (contrib) The project Entity API extends the entity API of Drupal core in order to provide an unified way to deal with entities and their properties. Additionally, it provides an entity CRUD* controller, which helps with simplifying the creation of new entity types. [ CRUD - Create, read, update and delete ]
  • 15. Entity API (contrib) Name Description entity_access Determines whether the given user has access to an entity. entity_create Create a new entity object. entity_delete Permanently delete the given entity. entity_delete_multiple Permanently delete multiple entities. entity_export Exports an entity. entity_import Imports an entity. entity_load_single A wrapper around entity_load() to load a single entity by name or numeric id. entity_metadata_wrapper Returns a property wrapper for the given data. entity_revision_delete Deletes an entity revision. entity_revision_load Loads an entity revision. entity_save Permanently save an entity. entity_theme Implements hook_theme(). entity_type_is_fieldable Checks whether an entity type is fieldable. entity_view Generate an array for rendering the given entities. entity_get_property_info Get the entity property info array of an entity type.
  • 16. Entity API hooks ● hook_entity_view ● hook_entity_insert ● hook_entity_update ● hook_entity_presave ● hook_entity_delete ● hook_entity_load
  • 18. Why use entity metadata wrappers? ● Makes your code more readable ● Provides a standardised way of accessing field values and entities through an API ● Stops you hard coding the language key into the array lookups ● Stops those nasty PHP warnings when you are trying to access properties that do not exist ● The wrapper autoloads entities (when used in conjunction with the ->value() accessor), which allow you to chain the callbacks
  • 19. Entity metadata wrapper $wrapper = entity_metadata_wrapper('node', $nid); $mail = $wrapper-->author---->mail-->value(); $wrapper-->author-->mail-->set('fago@example.com'); $text = $wrapper-->field_text-->value(); $wrapper-->language('de')-->field_text-->value(); $terms = $wrapper-->field_tags-->value(); $wrapper-->field_tags[] = $term; $options = $wrapper-->field_tags-->optionsList(); $label = $wrapper-->field_tags[0]-->label(); $access = $wrapper-->field_tags-->access('edit');
  • 20. Entity metadata wrapper $node = entity_load_single('node',$nid); $entity_wrapper = entity_metadata_wrapper('node', $node); $entity_wrapper = entity_metadata_wrapper('node', $nid); Load or
  • 21. Entity metadata wrapper Getters $entity_wrapper = entity_metadata_wrapper('node', 1); dpm($entity_wrapper->field_product->raw()); dpm($entity_wrapper->field_product->value());
  • 22. Entity metadata wrapper Getters $entity_wrapper = entity_metadata_wrapper('node', 1); dpm($entity_wrapper->value());
  • 23. Entity metadata wrapper $wrapper = entity_metadata_wrapper('node', 1); $mail = $wrapper->author->mail->value(); $wrapper->author->mail->set('foo@mail.com'); $wrapper->author->mail = 'foo@mail.com'; $wrapper->author->save(); Setters
  • 24. Entity metadata wrapper Multilingual $wrapper = entity_metadata_wrapper('node',1); dpm($wrapper->title_field->value()); dpm($wrapper->language('ro')->title_field->value()); dpm($wrapper->language('ru')->title_field->value());
  • 25. Entity metadata wrapper in life // add the items from the 'show_features' taxonomy $show_features = array(); $features = $series->field_features; if (is_array($features[LANGUAGE_NONE])) { foreach($features[LANGUAGE_NONE] as $tids) { $tid = $tids['tid']; $term = taxonomy_term_load($tid); $featureList[] = $term->name; } } Old approach
  • 26. Entity metadata wrapper in life // add the items from the 'show_features' taxonomy $show_features = array(); foreach ($wrapper->field_features->value() => $feature) { $show_features[] = $feature->name; } Entity metadata wrapper approach
  • 27. Entity + From API + Field API
  • 28. Entity + Form API + Field API function myForm($form, $form_state){ if (empty($id)){ $entity = entity_create($entityType, array('type' => $bundleName)); } else { $entity = entity_load_single($entityType, $id); } field_attach_form($entityType, $entity, $form, $form_state); $from['#submit'][] = 'myCustomSubmit'; return $form; } function myCustomSubmit($form, $form_state){ $entity = $form_state['values']['myEntity']; field_attach_submit($entity_type, $entity, $form, $form_state); }
  • 29. Entity + Form API + Field API function myForm($form, $form_state){ $from['my_data'] = array( '#type' => 'container', '#parents' => array('my_data'), '#tree' => TRUE, ); field_attach_form($entityType, $entity, $form[my_data], $form_state); $from['#submit'][] = 'myCustomSubmit'; return $form; } function myCustomSubmit($form, $form_state){ $entity = $form_state['values']['my_data']['myEntity']; field_attach_submit($entity_type, $entity, $form['my_data'], $form_state); }