 Welcome to Drupal con I Hope today your day was good. You're having nice day Yeah Had too much lunch Okay So the talk is all about dependency injection and trooper late I'm Ashwin Kumar So I'm from India. I work with region technology as a technical architect In terms of Drupal, I'm with Drupal from last five to six years Started my journey from Drupal six triple seven and not Drupal eight in terms of contribution I've started my journey in contribution by organizing certain small Drupal camps Drupal code sprints hackathons and helping new beginners to getting involved in the community now I'm more into the initiative back in India for Starting Drupal into Colleges schools which we have in their courses are also helping new beginners Who are coming out from the college? Getting jobs in Drupal Apart from that recent my contribution lies in Drupal console In translating Drupal console and some of the core commands translating Drupal console in Hindi specifically So we have translated Drupal console in Hindi About 97 point something percentage as of now, and I think that's the highest translatable language we have as of now and Yeah, and we did that before last Drupal con Mumbai. So that was kind of achievement for us Have you guys tried out Drupal console? Did you like it? Okay, and yeah, I also A Kind of a pilot train training. I'm taking a training of a helicopter. So, yeah That's that's what the picture so let's The agenda of the session first we're gonna discover What's the mystery about DI? So the story of the session goes like first we're gonna understand the conceptually what exactly dependence injection mean and Why we should learn dependence injection first place and then we're gonna see Some DI concepts in symphony and what is there in DI and to polite there'll be a couple of examples and architecture kind of a diagrams where we can easily understand the concept and How actually we can plug DI into to polite custom of contributed model development? So that's what the agenda will be all about of the session So there's this Joke which I heard why to give my two cents to hunt here So the joke is dependency injection is a concept of $25 where you have to put your two cents to learn So before going with the dependency injection, I would like to share one of my Real experience how I Come to know and get involved using DI in my contributed module sporting. So A year back I worked with one of my Contributed module porting and I did I use the Drupal upgrade module and it actually gave me what function is Deplicated and what things we have to change and there are certain brush command which actually change your Drupal 7 module Directed structure with the standard of Drupal 8. So I did that The module was done. I shifted a couple of business logic code into certain files as per the Drupal 8 module development standard and I thought let's get review from my mentor. So I Checked out my mentor and he said what you have done So I was like I contributed module and I ported successfully the model is running. You can install it But he said what we have done what you have done You have just shifted a couple of code what is already wrote in Drupal 7 and you have just followed the directive structure But you haven't used any of the power of Drupal 8 and we have seen that Drupal 8 is we are hearing many things about Drupal 8 like Drupal 8 have 200 features But the quick question which come into my mind. Can we learn 200 features quick? If a person coming from D7 to D8, what will the learning curve or if a person coming directly to Drupal 8 What will the learning curve how the 200 features can be used in module or site development or theming or Maybe a command line tool like Drupal console how you can use that? At that time he introduced me to the concept of dependency injection Which I'm still now super happy because it actually helped me and made my life easier in terms of Coding and in terms of creating modules or porting modules in Drupal 8. So well as it says the literal meaning is so before before just a step back, I'm assuming that all over here the Attendee are familiar with their OAPS concepts right, okay And how many of you have worked with Drupal or how many of you with symphony? Drupal oh Thank You symphony Okay, great So yeah, let's go ahead. So dependency injection what exactly it means It's basically a concept where you define your dependency in a class definition rather than instantiating them into class itself so as the site as the Statements is the little many injecting dependency into a class that what we understand about dependency injection as a word But what exactly that mean? So let's go to ground zero and let's first understand what it means by dependency so let's say you have a class that uses another object and performing some functions and Doing something with your business logic. So we can say that your class have dependency of certain object so for example if you have a model class that fetches something from the database, maybe a database query and or Performing some there may be a select query or an update query or SQL query anything so we can say that your model class have dependency of database object and That's what dependency is so now let's understand the Meaning of what it means injecting dependencies Injecting dependencies is like pushing dependency into your class from out of the box What it means that when you are declaring your dependencies into your class instead of using a new operator You can pass your dependency into a constructor as a parameter and that's it That's all means the dependency injection. You really do not need any of the concept of service container DIC or pimple of course they can make our life easier, but technically you don't need that So there are certain forms of dependency injection constructor setup property These are the mostly or widely used in different different frameworks Based on symphony. I guess a PHP So first let's take a look to constructor injection. What exactly it is. So constructor injection is mostly usable injection and frameworks in Drupal it as well. So So in constructor injection we pass our dependency into a parameter of a constructor of a class and it actually The dependency get present till the time your constructor Lifetime or basically your class lifetime. So it will always be the dependency will always be available throughout your class The disadvantage of this injection is so let's take an example you have a class which have a requirement of certain dependency and You have to use that without dependency your class will not work at that time constructor injection works very well So in this example as the newsletter manager It's a class and now I have to send a mail about my newsletter So my class cannot work with the dependency, right? So that's what the constructor injection is and you have to just pass it as a parameter to your constructor set injection Set injection is most likely a scenario where you have optional dependency Cases so you might need a dependency into your class or you may need, right? So you can actually set your dependency by using a set a method and pass it into the parameter Whatever dependency you have and you can actually declare all the set a matter is Set a method in many times in your class So for example, you have the first dependency mailer so you can declare set mailer and Here's my dependency. Likewise, you can specify many set a method into your class The disadvantage of this injection is you will not be able to Handle where your dependency actually lost control into your class So you have let's say a four or five dependency you have set in your class But let's say if the lines of the code are huge like thousand So you will not be able to figure it out. What dependency is available right now? Property injection property injection is kind of a similar to set a injection you can use the set a function and Same way you can call the dependency into your class and using the public Keyword you can actually specify the fields in your class The Problem with this Injection is you will not again the same problem which we have with the set a you will not able to control the dependencies Lifetime into your class So quick question which actually comes into my mind when I'm just going back into the scenario which I Introduce you when my mentor told me that you should use di into your code So I was like why did I because my code works without di as well? Why I have to forcefully use see I so a quick answer was that you really do not have to use dependency injection every and Every time whenever you are contributed or in a custom module. It basically depends if you have lots of dependency in your Contrib or custom module or specifically in your class at that time you should use di But you should not always use di principle whenever you are developing a triple eight module But why do I what exactly it gives me power? So let's take a example, so let's say we have a robot which builds a house and it started with building some piles of House and when it comes to a doorway We had two options either construct a door from starting taking a raw materials and building the door and then put it in a doorway or We can just get it from some supplier at the ready mentor and we'll just fix it in a doorway So what you guys think what will the efficient way? or quick way Supplying right That's what I think actually and that's what the concept is all about So you actually take a supplying a door and put it in your door. That's what in dependency injection is So what it comes to it comes to a dependency in version principle We're class so specifically in PHP. It works with interface So we're class have lots of dependency So you actually inject them with an interface and your class actually calls the interface to get all the Dependencies so that you can use a lot of functions which is available into your dependency class So let's take another example by understanding into a code point of view What exactly we will get if we use dependency injection and if we do not use it So first, let's take an example without dependency injection so let's say we have a two class Google Maps and OpenStreetMap and in our class we actually want to use the these classes functions or Let's say we create we're creating a store service and we have to get the address by using a Google map class or service So traditionally this is how we're gonna write we're gonna use a new operator Call the Google class and then we're gonna use The variable and get coordinates from address and we're gonna pass something our store address to it And maybe we get a geo coordinate or anything we would like to get But with dependency injection, what do you guys think what's changed? So here we are actually passing our Dependency into a constructor Parameter you see geo location service and we are just passing that into a constructor and anywhere in class I will just call that interface or dependency function anywhere in my class I can use that But what is geo location service? so we create an interface and we have function inside that and both the class Google Maps and OpenStreetMap implements the geo location service interface So our code is cleaner our code is Decompiled so our class is decoupled from the dependency so in this case if I want to change my dependency or function I want to rewrite this code But over here. I do not have to do anything. I'll just change the dependency what I'm passing So what we also get from this that our code is much more cleaner and modular Reusable yes the less your function knows the more usable it is so that's what the Outcome which we get from TI and it's flexible. Yes, your class is decoupled from your Dependency and you can anytime change the dependency where you have mapped with that It's easy to test and the abstraction is decoupled So while working with DI you often face these jargons dependency injector DIC inversion of control service container basically these are all same it just the Certain names which we use in different different frameworks More specifically in Drupal 8 you will face the service container That what it is and how we can use service container in DI so we're gonna talk about that in a moment. Oh Oh So service container basically deals by deals with the mapping of your dependency At one place and you can call that service container into your class So next time if I want to change any dependency of my class or let's say if I have a three dependency And next time I don't want to use a two dependency or I want to add any other dependency So I'll just add it in my service container Over here. I don't want you to think about just forget about the services module or web services It's not that so it's just a mapping of your dependency Which your module based on we're gonna see where and how it actually Happen and jupal it Before that let's first take a look to symphony's dependency injection component So there are a lot of concepts and symphony DI They are event dispatcher. They are compilers Which actually works behind that when you are using DI? But I'm not gonna take you in that deep instead I'm gonna show you the overall concept and how we can actually use it when we will contribute to Drupal so in symphony DI by calling any service they use the string Stuff and you can actually just call your dependency With a code and let's say some service second the same thing which I Mentioned that it actually allowed you to inject your dependency into class definition Instead of declaring them into the class itself the default scope is container and it can be configured in PHP or XML or YML and It can be compiled down to PHP as well So you can actually write all the service condensed stuff in PHP as well And yes, you can actually define your service container of the mapping into YML or XML or PHP So in Drupal 8 most of the stuff the YML has been introduced So all the service container the mapping has been lies with the YML So now let's take a look to dependency injection and Drupal 8 So while working in Drupal 8 services you probably be using These services the database and module handler and request so when I'm saying services you can also compare the hooks which we used to have in Drupal 8 and the core services which is introduced in Sorry the hooks which we used to have in Drupal 7 and the services which we have in Drupal 8 So hooks are deprecated in Drupal 8, right? And now we use services to actually alter them or Reuse them what we have in core. So two ways we can actually use core services One is the procedure code way you can actually define Drupal and service and just pass out some service which we need or write object-oriented code which we have just seen by using the DI principle or Injecting them by a service container But in Drupal 8 we really do not come to know where and exactly it happens in core. So there is a Drupal kernel file which actually deals with the auto loading and auto rewriting of Services, so when you define your services in a service container, so it makes sure that these things actually available to the class where you have called your where you have injecting your dependency and All the core services are defined in code.services.ym You can see the yml structure which they have been used and same structure we have to use in our module If you are if you have any compiler passes which needs to be Used in DI then those are actually Dilt by core bundle.php which lies in core slash lib slash Drupal code And there's the dependency injection component which is symphony component in Drupal 8. So all the things related to dependency injection lies in Drupal code dependency injection slash compiler. So yeah, so this Situation we get it. How do I use it in my module? So I'm Creating my custom module or I'm porting my Contributed module. Actually many of the contrary module You will not see the concept of DI has been used actually in search module. We do have I've seen that now people are actually Plugging all these features of Drupal 8 into their contributed modules But it was not there when the contrary module porting initially start started So let's say we are building our own module my module I would say and we have to define our module.services.yml and We will define all these service into this file and we're going to see how we can define that The next thing if where my class lies will be it will be in module slash lib slash Drupal slash my module slash my module bundle.php And that's where all the classes will lies and it will all the injection will happen in my class from there So now let's take a look to the start.services.yml what we have In this file what magic do we have? Because whatever we defined here any dependency it will get auto-loaded and it will available to our module So let me just Anyway, can you help me out? No, okay The presentation is stuck. Okay. We're not gonna go back. We're not gonna go ahead Sorry Yeah, we were here So there's this file are you able to see? No, at the back. No How do I increase these strings? Kill the lights Command plus plus plus plus. Thanks. Thanks Now great So there's this file which you can find out in core code.services.yml So you will find a lot of things in this file Which you will not able to understand if you're not familiar with it So I'm gonna directly take you to service definition example Yeah So this is how we actually define our services and services.yml So the path is a module and we define what services we need What class that services Should get so in that case over here Ally allies whitelist and tag is more like if my dependent class is also have certain dependency and Argument you can pass as many as dependency you want In your definition. So in this case, we have path alias whitelist, cache, default, log, state and all those things So this is how you're gonna define of when you create your module.services.yml And you have a lot of examples just same stuff. You have to just maybe a copy paste from here or Change whatever service you want. Should I click play? Now, let's take an example. If you are creating a form in Drupalit, how are we gonna actually use dependency injection and form a sample code first? Are you able to see it back? Thank you So we have an example form. So this is a piece of code which I actually took it from Drupal.org website Which actually states that how you can use dependency injection in when you're building your custom form So first we define all the namespaces all the interfaces which we're gonna use Into our class. So in that case form base, form state interface are all required interfaces There's an account interface and the dependency injector container interface we have to define and Now we're gonna extend the form base class And in constructor we have passed the account interface But where does actually dependency injection is happening? It's here So we are calling our current user a service Into our form and we're gonna use as we would like We're gonna maybe I take our current user profile or UID or name anything we can get from here And that's it. That's how you're gonna use it You have to just use the function and you have to just pass your services as a string The challenge with the Drupal 8 is there are a lot of things in it and how do I? Find which service I would need in my custom or contributed module How do I actually find where it actually lies and what all services has been included in Drupal 8 So there's this website api.drupal.org I actually forgot who built that But it was a great initiative That was taken when Drupal 8 by initially introduced you can actually find anything over here What APIs and what functions of file or where it actually lies a class with tag You can find out everything. It's just like a form or any API we used to have in Drupal 7 So there's certain form API you will have a certain URL and you see all the functions which is there in form API Just like that. You can actually search by name or content. Let me just try to Open this Are you all using by fair trying to look at use only one device, please Okay, let's go here search for services APIs Which we're going to find out the process Drupal services Transferring data Drupal ConWiFi So This is the place where you can actually search for services, which you can Use in your module or which is actually there in Drupal code Maybe Let's say search for more to handle It just load because of the Wi-Fi Yeah, so yeah, so This is a code which I actually believe and I always use this thing whenever I learn and Any new technology or any new part of a thing you have to start somewhere, right? even if you are a symphony guy and Now thinking of Drupal 8 you have to start somewhere, right? So start small and yeah, then work your way up. I don't know who is the person who quoted this But I have read somewhere long back Questions. Yes You can actually use DI in plugins as well because plugins are also you you write your class in plugins, right? So if you are writing a class, you can actually use interface in that. Are you sure about this? Okay I'm not sure whether the plugin class Okay, let me just think about it I think it can also implement the factory class which is not a DI way But factory class is also service which is provided by Drupal 8 and Yeah That it is yeah, yeah with plug-in. Yes, that that's the way to do it. What do you mean by reflections now? Okay, but does that answer your question? Maybe you can also try to use factory classes in plug-in. Yeah, any other question? Yeah Okay So Probably will not find exact What I have mentioned there But the objective of showing that you can pass the The geolocation service as an interface and you can plug your independence into this one The form example, okay. Yeah, it's actually calling from here Yeah, that's where you define and you just plug it As a parameter. Yes. Yes. Yes Yeah, so get it just a function which by using you can actually call the service of which you have defined in your interface Any other question? Okay Thanks for listening and yes, please do evaluate the session In New Orleans website the event start or maybe just a link where you can actually download the presentation of DI and I do have certain stickers and badge with me. So if you need you can collect it from me Thank you. Thanks for coming