The introduction of Object Oriented Programming in Drupal 8 is an evolution in the world of Drupal which has brought the concept of code reuse and encapsulation, allowing users to build better maintainable and structured code. One of the concepts that contribute to this is Dependency Injection and Services in Drupal
Is there a way for someone to modify the process by which Drupal invokes modules without making changes to the core function in Drupal 7? Or is there a better way to access the global variables without calling them directly in Drupal 7? The answer is “No”. This session aims at understanding the dependency injection and services in Drupal 8, that may be a bit complex but are really usable.
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
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.
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
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
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
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