SlideShare a Scribd company logo
1 of 31
Download to read offline
Demystifying Dependency Injection
And Services In Drupal 8
Jyoti Singh | Developer| Srijan Technologies
#SrijanWW | @srijan
#SrijanWW | @srijan
● Problems in Drupal 7
● Dependency Injection
● Services and Services Container
● Drupal 8 core services and how to use them.
● Using Services in contributed module.
Objective Of The Session
#SrijanWW | @srijan
Why Dependency Injection
How we want our code to look
like?
#SrijanWW | @srijan
How we want our code to look
like?
#SrijanWW | @srijan
How It Actually Looks
#SrijanWW | @srijan
●
#SrijanWW | @srijan
#SrijanWW | @srijan
Problems In Drupal 7!
#SrijanWW | @srijan
● No way to Reuse the code (not
OVERRIDE)
● - for eg : Copy the callback entirely and
replace it with your code.
#SrijanWW | @srijan
How can we test something?
- Create mock objects? NO.
- Want a simple test class? Bootstrap
Drupal first.
#SrijanWW | @srijan
Lot of Cluttering Happens
- Excessive use of alter hooks
- preprocess for calculations
- PHP in template files
#SrijanWW | @srijan
Let’s take a wrong approach
#SrijanWW | @srijan
Some procedural code
*foo_bar knows about the user object.
*node_make_me_look_cool needs a function from another module.
*You need bootstrapped Drupal to use module_load_include, or at least include the file that
declares it.
*foo_bar assumes some default database connection.
#SrijanWW | @srijan
User class uses sessions to store
language
#SrijanWW | @srijan
What Is Dependency Injection?
#SrijanWW | @srijan
Dependency injection is an advanced software
design pattern. It implements “inversion of
control”, which means that reusable code calls
task specific code.
#SrijanWW | @srijan
What does this mean for us Drupal
developers?
It means we can write more efficient code by
reducing the load. We only load what we need.
Type Of Injections
Type Of Injections
Constructor injection
Setter injection
property injection
Setter injectionSetter injection
class User
{
function setSessionStorage($storage)
{
$this->storage = $storage;
}
}
class User
{
public $sessionStorage;
}
$user->sessionStorage = $storage;
class User
{
function construct($storage) {
$this->storage = $storage;
}
}
#SrijanWW | @srijan
Example !
#SrijanWW | @srijan
Defining the Services :
services:
example.access_checker:
class:
DrupalexampleAccessCustomAccessCheck
arguments: ['@current_user']
tags:
- { name: access_check, applies_to:
_example_access_check }
#SrijanWW | @srijan
namespace DrupalexampleAccess;
use DrupalCoreRoutingAccessAccessInterface;
use DrupalCoreSessionAccountInterface;
class CustomAccessCheck implements AccessInterface {
/**
* A custom access check.
*
* @param DrupalCoreSessionAccountInterface $account
* Run access checks for this account.
*/
public function access(AccountInterface $account) {
return $account->hasPermission('custom_permission’);
}
}
What are services and services
container?
#SrijanWW | @srijan
Service is simply an object, usually with one
instance of each service's class for each
service on a site. For example, Drupal 8 sites
have a service for sending email, for logging
errors, for making HTTP requests, and
dozens of other common tasks.
#SrijanWW | @srijan
Goal : We want to write a code that is :
● Reusable
● Clutter Free
● Testeable
#SrijanWW | @srijan
The less your code knows, the
more reusable it is!
#SrijanWW | @srijan
Core Services in Drupal 8
#SrijanWW | @srijan
Some of the commonly used services in Drupal 8 core are :
● entity.query and entity.manager – Services use to access the
informationa about Drupal entities.
● config.fatory – Loads the Config information out of your Drupal
site
● config.storage - Service that knows where to store variables on
your site
● config.typed - Helps to store different data types in config
object.
● event_dispatcher - helps to "lazy load" classes so loading code
and instantiating objects only happens when the objects are
actually needed.
● current_user – The service that provides data for the drupal
user.
#SrijanWW | @srijan
Creating Services in
Custom Module
#SrijanWW | @srijan
● Create a src Folder inside the custom module.
● Create a EvenSubscriber Class inside the src
Folder
● Implement the EventSubscriberInterface .
● Implement some of the basic functions of
EventSubscriberInterface like onRespond() and
getSubscribedEvents()
● Create a services.yml file inside the root folder of
the custom module.
#SrijanWW | @srijan
This is how the EvenSubscriber Class looks like :
class ZipangSubscriber implements EventSubscriberInterface {
public function onRespond(FilterResponseEvent $event) {
$response = $event->getResponse();
$response->headers->set('X-Custom-Header', '******');
}
public static function getSubscribedEvents() {
$events[KernelEvents::RESPONSE][] = array('onRespond');
return $events;
}
}
#SrijanWW | @srijan
Service Tags
Tags are used to define a group of related services,
or to specify some aspect of how the service
behaves.
Some of the tags used are :
● event_subscriber: Indicates an event subscriber
service.
● access_check: Indicates a route access checking
service;
● cache.bin: Indicates a cache bin service,etc
#SrijanWW | @srijan
Questions
#SrijanWW | @srijan

More Related Content

What's hot

React JS - A quick introduction tutorial
React JS - A quick introduction tutorialReact JS - A quick introduction tutorial
React JS - A quick introduction tutorialMohammed Fazuluddin
 
React JS: A Secret Preview
React JS: A Secret PreviewReact JS: A Secret Preview
React JS: A Secret Previewvaluebound
 
Introduction to ReactJs & fundamentals
Introduction to ReactJs & fundamentalsIntroduction to ReactJs & fundamentals
Introduction to ReactJs & fundamentalswebsyndicate
 
Introduction to react
Introduction to reactIntroduction to react
Introduction to reactkiranabburi
 
A Brief Introduction to React.js
A Brief Introduction to React.jsA Brief Introduction to React.js
A Brief Introduction to React.jsDoug Neiner
 
Introduction to React JS for beginners
Introduction to React JS for beginners Introduction to React JS for beginners
Introduction to React JS for beginners Varun Raj
 
Couchbase - Introduction
Couchbase - IntroductionCouchbase - Introduction
Couchbase - IntroductionKnoldus Inc.
 
Understanding Facebook's React.js
Understanding Facebook's React.jsUnderstanding Facebook's React.js
Understanding Facebook's React.jsFederico Torre
 
[Final] ReactJS presentation
[Final] ReactJS presentation[Final] ReactJS presentation
[Final] ReactJS presentation洪 鹏发
 
React js Online Training
React js Online TrainingReact js Online Training
React js Online TrainingLearntek1
 

What's hot (20)

React js
React jsReact js
React js
 
Reactjs
Reactjs Reactjs
Reactjs
 
React.js+Redux Workshops
React.js+Redux WorkshopsReact.js+Redux Workshops
React.js+Redux Workshops
 
ReactJs
ReactJsReactJs
ReactJs
 
React JS - A quick introduction tutorial
React JS - A quick introduction tutorialReact JS - A quick introduction tutorial
React JS - A quick introduction tutorial
 
React JS: A Secret Preview
React JS: A Secret PreviewReact JS: A Secret Preview
React JS: A Secret Preview
 
Intro to React
Intro to ReactIntro to React
Intro to React
 
Introduction to ReactJs & fundamentals
Introduction to ReactJs & fundamentalsIntroduction to ReactJs & fundamentals
Introduction to ReactJs & fundamentals
 
React js for beginners
React js for beginnersReact js for beginners
React js for beginners
 
ReactJS for Beginners
ReactJS for BeginnersReactJS for Beginners
ReactJS for Beginners
 
Introduction to react
Introduction to reactIntroduction to react
Introduction to react
 
React and redux
React and reduxReact and redux
React and redux
 
React JS - Introduction
React JS - IntroductionReact JS - Introduction
React JS - Introduction
 
A Brief Introduction to React.js
A Brief Introduction to React.jsA Brief Introduction to React.js
A Brief Introduction to React.js
 
Introduction to React JS for beginners
Introduction to React JS for beginners Introduction to React JS for beginners
Introduction to React JS for beginners
 
Couchbase - Introduction
Couchbase - IntroductionCouchbase - Introduction
Couchbase - Introduction
 
Understanding Facebook's React.js
Understanding Facebook's React.jsUnderstanding Facebook's React.js
Understanding Facebook's React.js
 
[Final] ReactJS presentation
[Final] ReactJS presentation[Final] ReactJS presentation
[Final] ReactJS presentation
 
React js Online Training
React js Online TrainingReact js Online Training
React js Online Training
 
An Introduction to Redux
An Introduction to ReduxAn Introduction to Redux
An Introduction to Redux
 

Similar to Final dependency presentation.odp

[Srijan Wednesday Webinars] New Recipe of Decoupling: Drupal 8, Symfony and S...
[Srijan Wednesday Webinars] New Recipe of Decoupling: Drupal 8, Symfony and S...[Srijan Wednesday Webinars] New Recipe of Decoupling: Drupal 8, Symfony and S...
[Srijan Wednesday Webinars] New Recipe of Decoupling: Drupal 8, Symfony and S...Srijan Technologies
 
Angular.js опыт использования, проблемы и решения
Angular.js опыт использования, проблемы и решенияAngular.js опыт использования, проблемы и решения
Angular.js опыт использования, проблемы и решенияOlga Lavrentieva
 
JavaScripters Event Oct 22, 2016 · 2:00 PM: Common Mistakes made by Angular D...
JavaScripters Event Oct 22, 2016 · 2:00 PM: Common Mistakes made by Angular D...JavaScripters Event Oct 22, 2016 · 2:00 PM: Common Mistakes made by Angular D...
JavaScripters Event Oct 22, 2016 · 2:00 PM: Common Mistakes made by Angular D...JavaScripters Community
 
[Srijan Wednesday Webinar] Decoupled Demystified: The Present & Future of Dr...
 [Srijan Wednesday Webinar] Decoupled Demystified: The Present & Future of Dr... [Srijan Wednesday Webinar] Decoupled Demystified: The Present & Future of Dr...
[Srijan Wednesday Webinar] Decoupled Demystified: The Present & Future of Dr...Srijan Technologies
 
SharePoint Saturday Atlanta 2015
SharePoint Saturday Atlanta 2015SharePoint Saturday Atlanta 2015
SharePoint Saturday Atlanta 2015Pushkar Chivate
 
Dependency injection Drupal Camp Wrocław 2014
Dependency injection Drupal Camp Wrocław 2014Dependency injection Drupal Camp Wrocław 2014
Dependency injection Drupal Camp Wrocław 2014Greg Szczotka
 
jquery summit presentation for large scale javascript applications
jquery summit  presentation for large scale javascript applicationsjquery summit  presentation for large scale javascript applications
jquery summit presentation for large scale javascript applicationsDivyanshGupta922023
 
Angular workshop - Full Development Guide
Angular workshop - Full Development GuideAngular workshop - Full Development Guide
Angular workshop - Full Development GuideNitin Giri
 
Step by Step - AngularJS
Step by Step - AngularJSStep by Step - AngularJS
Step by Step - AngularJSInfragistics
 
Dependency Injection in Apache Spark Applications
Dependency Injection in Apache Spark ApplicationsDependency Injection in Apache Spark Applications
Dependency Injection in Apache Spark ApplicationsDatabricks
 
2014 11 20 Drupal 7 -> 8 test migratie
2014 11 20 Drupal 7 -> 8 test migratie2014 11 20 Drupal 7 -> 8 test migratie
2014 11 20 Drupal 7 -> 8 test migratiehcderaad
 
Grails & Angular: unleashing the dynamic duo
Grails & Angular: unleashing the dynamic duoGrails & Angular: unleashing the dynamic duo
Grails & Angular: unleashing the dynamic duoRubén Mondéjar Andreu
 
7\9 SSIS 2008R2_Training - Script Task
7\9 SSIS 2008R2_Training - Script Task7\9 SSIS 2008R2_Training - Script Task
7\9 SSIS 2008R2_Training - Script TaskPramod Singla
 
Slides for Automation Testing or End to End testing
Slides for Automation Testing or End to End testingSlides for Automation Testing or End to End testing
Slides for Automation Testing or End to End testingSwapnilNarayan
 
Angular Intermediate
Angular IntermediateAngular Intermediate
Angular IntermediateLinkMe Srl
 
BDD Selenium for Agile Teams - User Stories
BDD Selenium for Agile Teams - User StoriesBDD Selenium for Agile Teams - User Stories
BDD Selenium for Agile Teams - User StoriesSauce Labs
 

Similar to Final dependency presentation.odp (20)

[Srijan Wednesday Webinars] New Recipe of Decoupling: Drupal 8, Symfony and S...
[Srijan Wednesday Webinars] New Recipe of Decoupling: Drupal 8, Symfony and S...[Srijan Wednesday Webinars] New Recipe of Decoupling: Drupal 8, Symfony and S...
[Srijan Wednesday Webinars] New Recipe of Decoupling: Drupal 8, Symfony and S...
 
Angular js
Angular jsAngular js
Angular js
 
Angular.js опыт использования, проблемы и решения
Angular.js опыт использования, проблемы и решенияAngular.js опыт использования, проблемы и решения
Angular.js опыт использования, проблемы и решения
 
JavaScripters Event Oct 22, 2016 · 2:00 PM: Common Mistakes made by Angular D...
JavaScripters Event Oct 22, 2016 · 2:00 PM: Common Mistakes made by Angular D...JavaScripters Event Oct 22, 2016 · 2:00 PM: Common Mistakes made by Angular D...
JavaScripters Event Oct 22, 2016 · 2:00 PM: Common Mistakes made by Angular D...
 
Spring boot
Spring bootSpring boot
Spring boot
 
[Srijan Wednesday Webinar] Decoupled Demystified: The Present & Future of Dr...
 [Srijan Wednesday Webinar] Decoupled Demystified: The Present & Future of Dr... [Srijan Wednesday Webinar] Decoupled Demystified: The Present & Future of Dr...
[Srijan Wednesday Webinar] Decoupled Demystified: The Present & Future of Dr...
 
Introduction to AngularJs
Introduction to AngularJsIntroduction to AngularJs
Introduction to AngularJs
 
SharePoint Saturday Atlanta 2015
SharePoint Saturday Atlanta 2015SharePoint Saturday Atlanta 2015
SharePoint Saturday Atlanta 2015
 
Dependency injection Drupal Camp Wrocław 2014
Dependency injection Drupal Camp Wrocław 2014Dependency injection Drupal Camp Wrocław 2014
Dependency injection Drupal Camp Wrocław 2014
 
jquery summit presentation for large scale javascript applications
jquery summit  presentation for large scale javascript applicationsjquery summit  presentation for large scale javascript applications
jquery summit presentation for large scale javascript applications
 
Angular workshop - Full Development Guide
Angular workshop - Full Development GuideAngular workshop - Full Development Guide
Angular workshop - Full Development Guide
 
Step by Step - AngularJS
Step by Step - AngularJSStep by Step - AngularJS
Step by Step - AngularJS
 
Dependency Injection in Apache Spark Applications
Dependency Injection in Apache Spark ApplicationsDependency Injection in Apache Spark Applications
Dependency Injection in Apache Spark Applications
 
2014 11 20 Drupal 7 -> 8 test migratie
2014 11 20 Drupal 7 -> 8 test migratie2014 11 20 Drupal 7 -> 8 test migratie
2014 11 20 Drupal 7 -> 8 test migratie
 
Grails & Angular: unleashing the dynamic duo
Grails & Angular: unleashing the dynamic duoGrails & Angular: unleashing the dynamic duo
Grails & Angular: unleashing the dynamic duo
 
7\9 SSIS 2008R2_Training - Script Task
7\9 SSIS 2008R2_Training - Script Task7\9 SSIS 2008R2_Training - Script Task
7\9 SSIS 2008R2_Training - Script Task
 
Slides for Automation Testing or End to End testing
Slides for Automation Testing or End to End testingSlides for Automation Testing or End to End testing
Slides for Automation Testing or End to End testing
 
Angular Intermediate
Angular IntermediateAngular Intermediate
Angular Intermediate
 
Angular Seminar-js
Angular Seminar-jsAngular Seminar-js
Angular Seminar-js
 
BDD Selenium for Agile Teams - User Stories
BDD Selenium for Agile Teams - User StoriesBDD Selenium for Agile Teams - User Stories
BDD Selenium for Agile Teams - User Stories
 

More from Srijan Technologies

[Srijan Wednesday Webinar] How to Run Stateless and Stateful Services on K8S ...
[Srijan Wednesday Webinar] How to Run Stateless and Stateful Services on K8S ...[Srijan Wednesday Webinar] How to Run Stateless and Stateful Services on K8S ...
[Srijan Wednesday Webinar] How to Run Stateless and Stateful Services on K8S ...Srijan Technologies
 
[Srijan Wednesday Webinars] How to Set Up a Node.js Microservices Architectur...
[Srijan Wednesday Webinars] How to Set Up a Node.js Microservices Architectur...[Srijan Wednesday Webinars] How to Set Up a Node.js Microservices Architectur...
[Srijan Wednesday Webinars] How to Set Up a Node.js Microservices Architectur...Srijan Technologies
 
[Srijan Wednesday Webinars] How to Build a Cloud Native Platform for Enterpri...
[Srijan Wednesday Webinars] How to Build a Cloud Native Platform for Enterpri...[Srijan Wednesday Webinars] How to Build a Cloud Native Platform for Enterpri...
[Srijan Wednesday Webinars] How to Build a Cloud Native Platform for Enterpri...Srijan Technologies
 
[Srijan Wednesday Webinars] Using Drupal as Data Pipeline for Digital Signage
[Srijan Wednesday Webinars] Using Drupal as Data Pipeline for Digital Signage[Srijan Wednesday Webinars] Using Drupal as Data Pipeline for Digital Signage
[Srijan Wednesday Webinars] Using Drupal as Data Pipeline for Digital SignageSrijan Technologies
 
[Srijan Wednesday Webinars] Let’s Take the Best Route - Exploring Drupal 8 Ro...
[Srijan Wednesday Webinars] Let’s Take the Best Route - Exploring Drupal 8 Ro...[Srijan Wednesday Webinars] Let’s Take the Best Route - Exploring Drupal 8 Ro...
[Srijan Wednesday Webinars] Let’s Take the Best Route - Exploring Drupal 8 Ro...Srijan Technologies
 
[Srijan Wednesday Webinars] Is Your Business Ready for GDPR
[Srijan Wednesday Webinars] Is Your Business Ready for GDPR[Srijan Wednesday Webinars] Is Your Business Ready for GDPR
[Srijan Wednesday Webinars] Is Your Business Ready for GDPRSrijan Technologies
 
[Srijan Wednesday Webinars] Artificial Intelligence & the Future of Business
[Srijan Wednesday Webinars] Artificial Intelligence & the Future of Business[Srijan Wednesday Webinars] Artificial Intelligence & the Future of Business
[Srijan Wednesday Webinars] Artificial Intelligence & the Future of BusinessSrijan Technologies
 
[Srijan Wednesday Webinars] How to Design a Chatbot that Works
[Srijan Wednesday Webinars] How to Design a Chatbot that Works[Srijan Wednesday Webinars] How to Design a Chatbot that Works
[Srijan Wednesday Webinars] How to Design a Chatbot that WorksSrijan Technologies
 
[Srijan Wednesday Webinar] Leveraging the OGD Platform and Visualization Engine
[Srijan Wednesday Webinar] Leveraging the OGD Platform and Visualization Engine[Srijan Wednesday Webinar] Leveraging the OGD Platform and Visualization Engine
[Srijan Wednesday Webinar] Leveraging the OGD Platform and Visualization EngineSrijan Technologies
 
[Srijan Wednesday Webinars] Why Adopt Analytics Driven Testing
[Srijan Wednesday Webinars] Why Adopt Analytics Driven Testing [Srijan Wednesday Webinars] Why Adopt Analytics Driven Testing
[Srijan Wednesday Webinars] Why Adopt Analytics Driven Testing Srijan Technologies
 
[Srijan Wednesday Webinar] Key ingredients of a Powerful Test Automation System
[Srijan Wednesday Webinar] Key ingredients of a Powerful Test Automation System[Srijan Wednesday Webinar] Key ingredients of a Powerful Test Automation System
[Srijan Wednesday Webinar] Key ingredients of a Powerful Test Automation SystemSrijan Technologies
 
[Srijan Wednesday Webinar] Building BPMN Web Portals with Camunda and Drupal
[Srijan Wednesday Webinar] Building BPMN Web Portals with Camunda and Drupal[Srijan Wednesday Webinar] Building BPMN Web Portals with Camunda and Drupal
[Srijan Wednesday Webinar] Building BPMN Web Portals with Camunda and DrupalSrijan Technologies
 
[Srijan Wednesday Webinars] Automating Visual Regression using ‘Galen’
[Srijan Wednesday Webinars] Automating Visual Regression using ‘Galen’[Srijan Wednesday Webinars] Automating Visual Regression using ‘Galen’
[Srijan Wednesday Webinars] Automating Visual Regression using ‘Galen’Srijan Technologies
 
[Srijan Wednesday Webinars] NASA, Netflix, Tinder: Digital Transformation and...
[Srijan Wednesday Webinars] NASA, Netflix, Tinder: Digital Transformation and...[Srijan Wednesday Webinars] NASA, Netflix, Tinder: Digital Transformation and...
[Srijan Wednesday Webinars] NASA, Netflix, Tinder: Digital Transformation and...Srijan Technologies
 
[Srijan Wednesday Webinars] Building a High Performance QA Team
[Srijan Wednesday Webinars] Building a High Performance QA Team[Srijan Wednesday Webinars] Building a High Performance QA Team
[Srijan Wednesday Webinars] Building a High Performance QA TeamSrijan Technologies
 
[Srijan Wednesday Webinar] Mastering Mobile Test Automation with Appium
[Srijan Wednesday Webinar] Mastering Mobile Test Automation with Appium[Srijan Wednesday Webinar] Mastering Mobile Test Automation with Appium
[Srijan Wednesday Webinar] Mastering Mobile Test Automation with AppiumSrijan Technologies
 
[Srijan Wednesday Webinars] Transitioning to an Organization-wide Agile Culture
[Srijan Wednesday Webinars] Transitioning to an Organization-wide Agile Culture[Srijan Wednesday Webinars] Transitioning to an Organization-wide Agile Culture
[Srijan Wednesday Webinars] Transitioning to an Organization-wide Agile CultureSrijan Technologies
 
[Srijan Wednesday Webinars] Opportunities and Challenges in Enterprise UX Design
[Srijan Wednesday Webinars] Opportunities and Challenges in Enterprise UX Design[Srijan Wednesday Webinars] Opportunities and Challenges in Enterprise UX Design
[Srijan Wednesday Webinars] Opportunities and Challenges in Enterprise UX DesignSrijan Technologies
 
[Srijan Wednesday Webinars] Why Enterprises Should Embrace Distributed Agile ...
[Srijan Wednesday Webinars] Why Enterprises Should Embrace Distributed Agile ...[Srijan Wednesday Webinars] Why Enterprises Should Embrace Distributed Agile ...
[Srijan Wednesday Webinars] Why Enterprises Should Embrace Distributed Agile ...Srijan Technologies
 
[Srijan Wednesday Webinar] Easy Performance Wins for Your Rails App
[Srijan Wednesday Webinar] Easy Performance Wins for Your Rails App[Srijan Wednesday Webinar] Easy Performance Wins for Your Rails App
[Srijan Wednesday Webinar] Easy Performance Wins for Your Rails AppSrijan Technologies
 

More from Srijan Technologies (20)

[Srijan Wednesday Webinar] How to Run Stateless and Stateful Services on K8S ...
[Srijan Wednesday Webinar] How to Run Stateless and Stateful Services on K8S ...[Srijan Wednesday Webinar] How to Run Stateless and Stateful Services on K8S ...
[Srijan Wednesday Webinar] How to Run Stateless and Stateful Services on K8S ...
 
[Srijan Wednesday Webinars] How to Set Up a Node.js Microservices Architectur...
[Srijan Wednesday Webinars] How to Set Up a Node.js Microservices Architectur...[Srijan Wednesday Webinars] How to Set Up a Node.js Microservices Architectur...
[Srijan Wednesday Webinars] How to Set Up a Node.js Microservices Architectur...
 
[Srijan Wednesday Webinars] How to Build a Cloud Native Platform for Enterpri...
[Srijan Wednesday Webinars] How to Build a Cloud Native Platform for Enterpri...[Srijan Wednesday Webinars] How to Build a Cloud Native Platform for Enterpri...
[Srijan Wednesday Webinars] How to Build a Cloud Native Platform for Enterpri...
 
[Srijan Wednesday Webinars] Using Drupal as Data Pipeline for Digital Signage
[Srijan Wednesday Webinars] Using Drupal as Data Pipeline for Digital Signage[Srijan Wednesday Webinars] Using Drupal as Data Pipeline for Digital Signage
[Srijan Wednesday Webinars] Using Drupal as Data Pipeline for Digital Signage
 
[Srijan Wednesday Webinars] Let’s Take the Best Route - Exploring Drupal 8 Ro...
[Srijan Wednesday Webinars] Let’s Take the Best Route - Exploring Drupal 8 Ro...[Srijan Wednesday Webinars] Let’s Take the Best Route - Exploring Drupal 8 Ro...
[Srijan Wednesday Webinars] Let’s Take the Best Route - Exploring Drupal 8 Ro...
 
[Srijan Wednesday Webinars] Is Your Business Ready for GDPR
[Srijan Wednesday Webinars] Is Your Business Ready for GDPR[Srijan Wednesday Webinars] Is Your Business Ready for GDPR
[Srijan Wednesday Webinars] Is Your Business Ready for GDPR
 
[Srijan Wednesday Webinars] Artificial Intelligence & the Future of Business
[Srijan Wednesday Webinars] Artificial Intelligence & the Future of Business[Srijan Wednesday Webinars] Artificial Intelligence & the Future of Business
[Srijan Wednesday Webinars] Artificial Intelligence & the Future of Business
 
[Srijan Wednesday Webinars] How to Design a Chatbot that Works
[Srijan Wednesday Webinars] How to Design a Chatbot that Works[Srijan Wednesday Webinars] How to Design a Chatbot that Works
[Srijan Wednesday Webinars] How to Design a Chatbot that Works
 
[Srijan Wednesday Webinar] Leveraging the OGD Platform and Visualization Engine
[Srijan Wednesday Webinar] Leveraging the OGD Platform and Visualization Engine[Srijan Wednesday Webinar] Leveraging the OGD Platform and Visualization Engine
[Srijan Wednesday Webinar] Leveraging the OGD Platform and Visualization Engine
 
[Srijan Wednesday Webinars] Why Adopt Analytics Driven Testing
[Srijan Wednesday Webinars] Why Adopt Analytics Driven Testing [Srijan Wednesday Webinars] Why Adopt Analytics Driven Testing
[Srijan Wednesday Webinars] Why Adopt Analytics Driven Testing
 
[Srijan Wednesday Webinar] Key ingredients of a Powerful Test Automation System
[Srijan Wednesday Webinar] Key ingredients of a Powerful Test Automation System[Srijan Wednesday Webinar] Key ingredients of a Powerful Test Automation System
[Srijan Wednesday Webinar] Key ingredients of a Powerful Test Automation System
 
[Srijan Wednesday Webinar] Building BPMN Web Portals with Camunda and Drupal
[Srijan Wednesday Webinar] Building BPMN Web Portals with Camunda and Drupal[Srijan Wednesday Webinar] Building BPMN Web Portals with Camunda and Drupal
[Srijan Wednesday Webinar] Building BPMN Web Portals with Camunda and Drupal
 
[Srijan Wednesday Webinars] Automating Visual Regression using ‘Galen’
[Srijan Wednesday Webinars] Automating Visual Regression using ‘Galen’[Srijan Wednesday Webinars] Automating Visual Regression using ‘Galen’
[Srijan Wednesday Webinars] Automating Visual Regression using ‘Galen’
 
[Srijan Wednesday Webinars] NASA, Netflix, Tinder: Digital Transformation and...
[Srijan Wednesday Webinars] NASA, Netflix, Tinder: Digital Transformation and...[Srijan Wednesday Webinars] NASA, Netflix, Tinder: Digital Transformation and...
[Srijan Wednesday Webinars] NASA, Netflix, Tinder: Digital Transformation and...
 
[Srijan Wednesday Webinars] Building a High Performance QA Team
[Srijan Wednesday Webinars] Building a High Performance QA Team[Srijan Wednesday Webinars] Building a High Performance QA Team
[Srijan Wednesday Webinars] Building a High Performance QA Team
 
[Srijan Wednesday Webinar] Mastering Mobile Test Automation with Appium
[Srijan Wednesday Webinar] Mastering Mobile Test Automation with Appium[Srijan Wednesday Webinar] Mastering Mobile Test Automation with Appium
[Srijan Wednesday Webinar] Mastering Mobile Test Automation with Appium
 
[Srijan Wednesday Webinars] Transitioning to an Organization-wide Agile Culture
[Srijan Wednesday Webinars] Transitioning to an Organization-wide Agile Culture[Srijan Wednesday Webinars] Transitioning to an Organization-wide Agile Culture
[Srijan Wednesday Webinars] Transitioning to an Organization-wide Agile Culture
 
[Srijan Wednesday Webinars] Opportunities and Challenges in Enterprise UX Design
[Srijan Wednesday Webinars] Opportunities and Challenges in Enterprise UX Design[Srijan Wednesday Webinars] Opportunities and Challenges in Enterprise UX Design
[Srijan Wednesday Webinars] Opportunities and Challenges in Enterprise UX Design
 
[Srijan Wednesday Webinars] Why Enterprises Should Embrace Distributed Agile ...
[Srijan Wednesday Webinars] Why Enterprises Should Embrace Distributed Agile ...[Srijan Wednesday Webinars] Why Enterprises Should Embrace Distributed Agile ...
[Srijan Wednesday Webinars] Why Enterprises Should Embrace Distributed Agile ...
 
[Srijan Wednesday Webinar] Easy Performance Wins for Your Rails App
[Srijan Wednesday Webinar] Easy Performance Wins for Your Rails App[Srijan Wednesday Webinar] Easy Performance Wins for Your Rails App
[Srijan Wednesday Webinar] Easy Performance Wins for Your Rails App
 

Recently uploaded

Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
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
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
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
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
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
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 

Recently uploaded (20)

Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 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...
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
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...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 

Final dependency presentation.odp

  • 1. Demystifying Dependency Injection And Services In Drupal 8 Jyoti Singh | Developer| Srijan Technologies #SrijanWW | @srijan
  • 2. #SrijanWW | @srijan ● Problems in Drupal 7 ● Dependency Injection ● Services and Services Container ● Drupal 8 core services and how to use them. ● Using Services in contributed module. Objective Of The Session
  • 3. #SrijanWW | @srijan Why Dependency Injection
  • 4. How we want our code to look like? #SrijanWW | @srijan
  • 5. How we want our code to look like? #SrijanWW | @srijan
  • 6. How It Actually Looks #SrijanWW | @srijan
  • 9. #SrijanWW | @srijan ● No way to Reuse the code (not OVERRIDE) ● - for eg : Copy the callback entirely and replace it with your code.
  • 10. #SrijanWW | @srijan How can we test something? - Create mock objects? NO. - Want a simple test class? Bootstrap Drupal first.
  • 11. #SrijanWW | @srijan Lot of Cluttering Happens - Excessive use of alter hooks - preprocess for calculations - PHP in template files
  • 12. #SrijanWW | @srijan Let’s take a wrong approach
  • 13. #SrijanWW | @srijan Some procedural code *foo_bar knows about the user object. *node_make_me_look_cool needs a function from another module. *You need bootstrapped Drupal to use module_load_include, or at least include the file that declares it. *foo_bar assumes some default database connection.
  • 14. #SrijanWW | @srijan User class uses sessions to store language
  • 15. #SrijanWW | @srijan What Is Dependency Injection?
  • 16. #SrijanWW | @srijan Dependency injection is an advanced software design pattern. It implements “inversion of control”, which means that reusable code calls task specific code.
  • 17. #SrijanWW | @srijan What does this mean for us Drupal developers? It means we can write more efficient code by reducing the load. We only load what we need. Type Of Injections
  • 18. Type Of Injections Constructor injection Setter injection property injection Setter injectionSetter injection class User { function setSessionStorage($storage) { $this->storage = $storage; } } class User { public $sessionStorage; } $user->sessionStorage = $storage; class User { function construct($storage) { $this->storage = $storage; } } #SrijanWW | @srijan
  • 20. Defining the Services : services: example.access_checker: class: DrupalexampleAccessCustomAccessCheck arguments: ['@current_user'] tags: - { name: access_check, applies_to: _example_access_check } #SrijanWW | @srijan namespace DrupalexampleAccess; use DrupalCoreRoutingAccessAccessInterface; use DrupalCoreSessionAccountInterface; class CustomAccessCheck implements AccessInterface { /** * A custom access check. * * @param DrupalCoreSessionAccountInterface $account * Run access checks for this account. */ public function access(AccountInterface $account) { return $account->hasPermission('custom_permission’); } }
  • 21. What are services and services container? #SrijanWW | @srijan
  • 22. Service is simply an object, usually with one instance of each service's class for each service on a site. For example, Drupal 8 sites have a service for sending email, for logging errors, for making HTTP requests, and dozens of other common tasks. #SrijanWW | @srijan
  • 23. Goal : We want to write a code that is : ● Reusable ● Clutter Free ● Testeable #SrijanWW | @srijan
  • 24. The less your code knows, the more reusable it is! #SrijanWW | @srijan
  • 25. Core Services in Drupal 8 #SrijanWW | @srijan
  • 26. Some of the commonly used services in Drupal 8 core are : ● entity.query and entity.manager – Services use to access the informationa about Drupal entities. ● config.fatory – Loads the Config information out of your Drupal site ● config.storage - Service that knows where to store variables on your site ● config.typed - Helps to store different data types in config object. ● event_dispatcher - helps to "lazy load" classes so loading code and instantiating objects only happens when the objects are actually needed. ● current_user – The service that provides data for the drupal user. #SrijanWW | @srijan
  • 27. Creating Services in Custom Module #SrijanWW | @srijan
  • 28. ● Create a src Folder inside the custom module. ● Create a EvenSubscriber Class inside the src Folder ● Implement the EventSubscriberInterface . ● Implement some of the basic functions of EventSubscriberInterface like onRespond() and getSubscribedEvents() ● Create a services.yml file inside the root folder of the custom module. #SrijanWW | @srijan
  • 29. This is how the EvenSubscriber Class looks like : class ZipangSubscriber implements EventSubscriberInterface { public function onRespond(FilterResponseEvent $event) { $response = $event->getResponse(); $response->headers->set('X-Custom-Header', '******'); } public static function getSubscribedEvents() { $events[KernelEvents::RESPONSE][] = array('onRespond'); return $events; } } #SrijanWW | @srijan
  • 30. Service Tags Tags are used to define a group of related services, or to specify some aspect of how the service behaves. Some of the tags used are : ● event_subscriber: Indicates an event subscriber service. ● access_check: Indicates a route access checking service; ● cache.bin: Indicates a cache bin service,etc #SrijanWW | @srijan