{"id":55745,"date":"2022-11-05T17:08:58","date_gmt":"2022-11-05T11:38:58","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=55745"},"modified":"2026-03-26T15:37:51","modified_gmt":"2026-03-26T10:07:51","slug":"services-and-dependency-injection-in-drupal","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/services-and-dependency-injection-in-drupal\/","title":{"rendered":"Services and Dependency Injection in Drupal"},"content":{"rendered":"<p>Drupal 8+ is built upon Services, forming the backbone of modern <strong>Drupal development services<\/strong>, where much of the system functionality is powered through dependency injection.<\/p>\n<p><strong>Services :<\/strong> Services: A service is any object managed by the services container, and understanding this concept is fundamental for teams offering scalable <strong>Drupal services<\/strong>, or we can say that services is a simple PHP class with some methods. we can access these services and methods in anywhere in the application with the help of service container.<\/p>\n<p>The service can be used the in following way. We use the \\Drupal::service() method to get an instantiated AliasManager object and then use a function in that object to translate the path to the alias.<\/p>\n<p>$aliasManager = \\Drupal::service(&#8216;path_alias.manager&#8217;);<br \/>\n$path = &#8216;\/node\/123&#8217;;<br \/>\n$alias = $aliasManager-&gt;getAliasByPath($path);<\/p>\n<p>Such implementations are commonly leveraged in enterprise-grade <a href=\"https:\/\/www.tothenew.com\/cx\/drupal-services\"><strong>Drupal development services<\/strong><\/a> to ensure modular and reusable architecture.<\/p>\n<p><strong>Accessing Services:<\/strong> Services are all defined in YML files within Drupal. Every module that wants to define a service needs to create a file with the name of\u00a0<em>[module name].services.yml , forming a structured <strong>Drupal services<\/strong> list that helps developers identify and reuse functionalities efficiently. In order to find all the available services in Drupal you can follow the Drupal <a href=\"https:\/\/api.drupal.org\/api\/drupal\/services\">services list<\/a> which acts as a reference for available core and custom services.<\/em><\/p>\n<p><em>So best practice for accessing the services is dependency injection instead of direct calling the global Drupal service container.<\/em><\/p>\n<p><strong>Dependency Injection :\u00a0<\/strong><em> Dependency injection is a design pattern in which an object or function receives the dependencies it needs, a core practice widely adopted in modern <strong>Drupal as a service<\/strong> architectures for building scalable and decoupled systems. It means we can directly inject the services to the class __construct method, and it will load the dependency runtime when instantiate the class.<\/em><\/p>\n<p><strong>Why use DI in Drupal :\u00a0<\/strong><\/p>\n<ol>\n<li>Decoupled functionality<\/li>\n<li>Eases unit testing<\/li>\n<li>Reusable &amp; flexible code<\/li>\n<li>Cleaner code<\/li>\n<li>Retrieving dependencies from the container is better for performance.<\/li>\n<\/ol>\n<p><strong>How to Inject Dependency :\u00a0<\/strong><\/p>\n<p>Before implementing the DI let\u2019s have a look the code without DI. So i am just displaying the current user name in the render function. In the below code line no 13 and 14 i am using the Drupal function to get the username,<\/p>\n<p style=\"text-align: center;\"><strong>Example code without DI.\u00a0<\/strong><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-55739 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/11\/drupal-without-DI.png\" alt=\"\" width=\"669\" height=\"422\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/11\/drupal-without-DI.png 669w, \/blog\/wp-ttn-blog\/uploads\/2022\/11\/drupal-without-DI-300x189.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/11\/drupal-without-DI-624x394.png 624w\" sizes=\"(max-width: 669px) 100vw, 669px\" \/><\/p>\n<p>its also working fine but Drupal recommend to use Dependency Injection everywhere. So let\u2019s start to inject the Dependency Injection on same code base.<\/p>\n<p style=\"text-align: center;\"><strong>Example <\/strong><strong>code With DI : <\/strong><\/p>\n<p><strong>Example 1: <\/strong><strong>DI with in Controller<\/strong><\/p>\n<p>I have created the simple render function within the controller , Here I am using the Dependency Injection to get the current user details based on the current_user services.<\/p>\n<p>There is 2 main function to execute the DI in Drupal __construct() &amp; create().<\/p>\n<p><strong>__<\/strong><strong>Construct Method : <\/strong>With in the construct method we define all the services which we need as a DI.<\/p>\n<p><strong><b>Create Method : <\/b><\/strong>Create() method is a factory method based on the factory design Pattern.<\/p>\n<p>create() method help us to create the instance of the all the services which we pass in the create() method. Return new static() means \u201cReturn a new instance of the current class, using the passed arguments in the class\u2019s constructor\u201d<\/p>\n<p>So technically all the arguments and order of the arguments would be the same in both the create() and construct() function.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-55740 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/11\/DI-drupal-controller.png\" alt=\"DI in Drupal\" width=\"647\" height=\"797\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/11\/DI-drupal-controller.png 647w, \/blog\/wp-ttn-blog\/uploads\/2022\/11\/DI-drupal-controller-244x300.png 244w, \/blog\/wp-ttn-blog\/uploads\/2022\/11\/DI-drupal-controller-624x769.png 624w\" sizes=\"(max-width: 647px) 100vw, 647px\" \/><\/p>\n<p>Same as above we can define the DI in FormBase, configFormBase etc<\/p>\n<p style=\"text-align: center;\"><strong>Example 2 : D<\/strong><strong>ependency Injection<\/strong> <strong>in<\/strong> <strong>s<\/strong><strong>ervices <\/strong><strong>class<\/strong><\/p>\n<p>For services DI we are taking the example of core <a href=\"https:\/\/git.drupalcode.org\/project\/drupal\/blob\/8.5.4\/core\/modules\/node\/node.services.yml#n6\">node.service.yml<\/a><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-55742 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/11\/services_di2.png\" alt=\"services di 2\" width=\"1218\" height=\"262\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/11\/services_di2.png 1218w, \/blog\/wp-ttn-blog\/uploads\/2022\/11\/services_di2-300x65.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/11\/services_di2-1024x220.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/11\/services_di2-768x165.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/11\/services_di2-624x134.png 624w\" sizes=\"(max-width: 1218px) 100vw, 1218px\" \/><\/p>\n<p><span style=\"font-family: arial, helvetica, nimbus sans l, freesans, sans-serif;\">That is saying that for the <\/span><code class=\"western\">node.grant_storage<br \/>\n<\/code><span style=\"font-family: arial, helvetica, nimbus sans l, freesans, sans-serif;\">service, <\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, nimbus sans l, freesans, sans-serif;\">the <\/span><code class=\"western\">Drupal\\node\\NodeGrantDatabaseStorage<br \/>\n<\/code><span style=\"font-family: arial, helvetica, nimbus sans l, freesans, sans-serif;\">class will be used, and three arguments will be passed to it when creating an instance of it. <\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, nimbus sans l, freesans, sans-serif;\">The <\/span><code class=\"western\">@ <\/code><span style=\"font-family: arial, helvetica, nimbus sans l, freesans, sans-serif;\">symbol means that these are instances of other services. An instance of a <\/span><code class=\"western\">database <\/code><span style=\"font-family: arial, helvetica, nimbus sans l, freesans, sans-serif;\">service, a <\/span><code class=\"western\">module_handler <\/code><span style=\"font-family: arial, helvetica, nimbus sans l, freesans, sans-serif;\">service, and a <\/span><code class=\"western\">language_manager <\/code><span style=\"font-family: arial, helvetica, nimbus sans l, freesans, sans-serif;\">service will be provided to this node.grant_storage service. These services are just objects of designated types.<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, nimbus sans l, freesans, sans-serif;\">Here&#8217;s the relevant portion of the <\/span><span style=\"font-family: Arial, Helvetica, Nimbus Sans L, FreeSans, sans-serif;\"><span style=\"color: #f6830f;\"><a href=\"https:\/\/cgit.drupalcode.org\/drupal\/tree\/core\/modules\/node\/src\/NodeGrantDatabaseStorage.php?id=8.5.4\">NodeGrantDatabaseStorge<\/a> <\/span><\/span><span style=\"font-family: arial, helvetica, nimbus sans l, freesans, sans-serif;\">class. I&#8217;ve added line breaks to this and other code samples for readability.<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-55743 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/11\/services-class.png\" alt=\"services class\" width=\"1646\" height=\"1242\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/11\/services-class.png 1646w, \/blog\/wp-ttn-blog\/uploads\/2022\/11\/services-class-300x226.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/11\/services-class-1024x773.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/11\/services-class-768x579.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/11\/services-class-1536x1159.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2022\/11\/services-class-624x471.png 624w\" sizes=\"(max-width: 1646px) 100vw, 1646px\" \/><\/p>\n<p><span style=\"font-family: arial, helvetica, nimbus sans l, freesans, sans-serif;\">The three arguments used in the constructor match the three arguments defined in the services file. The passed Object are then stored as properties of the class. <\/span><span style=\"font-family: arial, helvetica, nimbus sans l, freesans, sans-serif;\">In other word, the three objects (dependencies) were injected into the client class (NodeGrantDatabaseStorage) by the services system.<\/span><a name=\"plugin-example\"><\/a><span style=\"font-family: arial, helvetica, nimbus sans l, freesans, sans-serif;\">Example 3: <\/span><strong><span style=\"font-family: arial, helvetica, nimbus sans l, freesans, sans-serif;\">Dependency Injection Inside Block (Plugin)<\/span><\/strong><\/p>\n<p><strong><span style=\"font-family: arial, helvetica, nimbus sans l, freesans, sans-serif;\">Example 3: <\/span><span style=\"font-family: arial, helvetica, nimbus sans l, freesans, sans-serif;\">Dependency Injection Inside Block (Plugin)<\/span><\/strong><\/p>\n<p><span style=\"font-family: arial, helvetica, nimbus sans l, freesans, sans-serif;\">We can inject our custom\/core service inside our custom block plugin for that we have to use.<\/span><\/p>\n<pre class=\"western\"><code class=\"western\">use Drupal\\Core\\Plugin\\ContainerFactoryPluginInterface;<\/code> <span style=\"font-family: arial, helvetica, nimbus sans l, freesans, sans-serif;\">Suppose you have a custom block with the name <\/span><span style=\"font-family: arial, helvetica, nimbus sans l, freesans, sans-serif;\">'drupalise'<\/span><span style=\"font-family: arial, helvetica, nimbus sans l, freesans, sans-serif;\"> then you inject your custom service<\/span><span style=\"font-family: arial, helvetica, nimbus sans l, freesans, sans-serif;\"> 'drupalise'<\/span><span style=\"font-family: arial, helvetica, nimbus sans l, freesans, sans-serif;\"> inside your custom block code as follows: <img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-55744 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/11\/plugin_di.png\" alt=\"Plugin di\" width=\"2304\" height=\"2534\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/11\/plugin_di.png 2304w, \/blog\/wp-ttn-blog\/uploads\/2022\/11\/plugin_di-273x300.png 273w, \/blog\/wp-ttn-blog\/uploads\/2022\/11\/plugin_di-931x1024.png 931w, \/blog\/wp-ttn-blog\/uploads\/2022\/11\/plugin_di-768x845.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/11\/plugin_di-1397x1536.png 1397w, \/blog\/wp-ttn-blog\/uploads\/2022\/11\/plugin_di-1862x2048.png 1862w, \/blog\/wp-ttn-blog\/uploads\/2022\/11\/plugin_di-624x686.png 624w\" sizes=\"(max-width: 2304px) 100vw, 2304px\" \/><\/span><\/pre>\n<p><span style=\"font-family: arial, helvetica, nimbus sans l, freesans, sans-serif;\">Here drupalise is my custom service which has the function Drupalise().<\/span><\/p>\n<p>That&#8217;s All in Drupal Dependency Injection. Happy Drupaling.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"western\"><span style=\"font-family: arial, helvetica, nimbus sans l, freesans, sans-serif;\">\u00a0<\/span><\/pre>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>Drupal 8+ is built upon Services, forming the backbone of modern Drupal development services, where much of the system functionality is powered through dependency injection. Services : Services: A service is any object managed by the services container, and understanding this concept is fundamental for teams offering scalable Drupal services, or we can say that [&hellip;]<\/p>\n","protected":false},"author":1509,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":637},"categories":[3602],"tags":[5042,4862],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/55745"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1509"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=55745"}],"version-history":[{"count":4,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/55745\/revisions"}],"predecessor-version":[{"id":79216,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/55745\/revisions\/79216"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=55745"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=55745"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=55745"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}