SlideShare a Scribd company logo
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

PDF
[Srijan Wednesday Webinars] Simplifying Migration to Drupal 8
PPTX
reactJS
PPTX
React js Demo Explanation
PPTX
Introduction to React JS for beginners | Namespace IT
PPTX
Introduction to react js
PPTX
React.js - The Dawn of Virtual DOM
ODP
Introduction to ReactJS
PPTX
React JS part 1
[Srijan Wednesday Webinars] Simplifying Migration to Drupal 8
reactJS
React js Demo Explanation
Introduction to React JS for beginners | Namespace IT
Introduction to react js
React.js - The Dawn of Virtual DOM
Introduction to ReactJS
React JS part 1

What's hot (20)

PPTX
React js
PPTX
Reactjs
PDF
React.js+Redux Workshops
PPTX
ReactJs
PPTX
React JS - A quick introduction tutorial
PPTX
React JS: A Secret Preview
PPTX
Intro to React
PPTX
Introduction to ReactJs & fundamentals
PPTX
React js for beginners
PPTX
ReactJS for Beginners
PDF
Introduction to react
PDF
React and redux
PDF
React JS - Introduction
PPTX
A Brief Introduction to React.js
PPTX
Introduction to React JS for beginners
ODP
Couchbase - Introduction
PDF
Understanding Facebook's React.js
PPTX
[Final] ReactJS presentation
PPTX
React js Online Training
PDF
An Introduction to Redux
React js
Reactjs
React.js+Redux Workshops
ReactJs
React JS - A quick introduction tutorial
React JS: A Secret Preview
Intro to React
Introduction to ReactJs & fundamentals
React js for beginners
ReactJS for Beginners
Introduction to react
React and redux
React JS - Introduction
A Brief Introduction to React.js
Introduction to React JS for beginners
Couchbase - Introduction
Understanding Facebook's React.js
[Final] ReactJS presentation
React js Online Training
An Introduction to Redux
Ad

Similar to Final dependency presentation.odp (20)

PDF
Dependency injection in Drupal 8
PPTX
Services, dependency injection and containers
PPTX
Dependency injection in Drupal 8 : DrupalCon NOLA
PDF
Drupal 8 Services
PPTX
Dependency Injection in Drupal 8
PPT
Future Proofing Your Drupal Skills
PDF
Dependency injection Drupal Camp Wrocław 2014
PDF
[Srijan Wednesday Webinar] Decoupled Demystified: The Present & Future of Dr...
PDF
Drupal 8 Services And Dependency Injection
PDF
The state of hooking into Drupal - DrupalCon Dublin
PDF
Hooks and Events in Drupal 8
PDF
Services in Drupal 8
PPTX
Decoupling Drupal 8.x: Drupal’s Web Services Today and Tomorrow
PPTX
Drupal 7 Web Services Crash Course
PDF
Leveraging the Command Pattern: Enhancing Drupal with Symfony Messenger.pdf
PPTX
Drupal 8 Deep Dive: What It Means for Developers Now that REST Is in Core
PDF
Drupal South 2015: Introduction to Web Services. Services in Drupal 8.
PDF
Drupal 8 - Core and API Changes
ODP
Drupal 8 Routing
PPTX
RESTful application with Drupal 8
Dependency injection in Drupal 8
Services, dependency injection and containers
Dependency injection in Drupal 8 : DrupalCon NOLA
Drupal 8 Services
Dependency Injection in Drupal 8
Future Proofing Your Drupal Skills
Dependency injection Drupal Camp Wrocław 2014
[Srijan Wednesday Webinar] Decoupled Demystified: The Present & Future of Dr...
Drupal 8 Services And Dependency Injection
The state of hooking into Drupal - DrupalCon Dublin
Hooks and Events in Drupal 8
Services in Drupal 8
Decoupling Drupal 8.x: Drupal’s Web Services Today and Tomorrow
Drupal 7 Web Services Crash Course
Leveraging the Command Pattern: Enhancing Drupal with Symfony Messenger.pdf
Drupal 8 Deep Dive: What It Means for Developers Now that REST Is in Core
Drupal South 2015: Introduction to Web Services. Services in Drupal 8.
Drupal 8 - Core and API Changes
Drupal 8 Routing
RESTful application with Drupal 8
Ad

More from Srijan Technologies (20)

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

Recently uploaded (20)

PDF
cuic standard and advanced reporting.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
A Presentation on Artificial Intelligence
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Empathic Computing: Creating Shared Understanding
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Electronic commerce courselecture one. Pdf
cuic standard and advanced reporting.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Chapter 3 Spatial Domain Image Processing.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Network Security Unit 5.pdf for BCA BBA.
Reach Out and Touch Someone: Haptics and Empathic Computing
A Presentation on Artificial Intelligence
MIND Revenue Release Quarter 2 2025 Press Release
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Building Integrated photovoltaic BIPV_UPV.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
Empathic Computing: Creating Shared Understanding
Dropbox Q2 2025 Financial Results & Investor Presentation
“AI and Expert System Decision Support & Business Intelligence Systems”
Encapsulation_ Review paper, used for researhc scholars
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
gpt5_lecture_notes_comprehensive_20250812015547.pdf
NewMind AI Weekly Chronicles - August'25-Week II
Digital-Transformation-Roadmap-for-Companies.pptx
Electronic commerce courselecture one. Pdf

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