You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-[Drupal 9: An Introduction To Services And Dependency Injection](https://www.hashbangcode.com/article/drupal-9-introduction-services-and-dependency-injection)
16
+
-[Drupal 9: Extending Drupal Base Classes Without Overriding Constructors](https://www.hashbangcode.com/article/drupal-9-extending-drupal-base-classes-without-overriding-constructors)
- Prints a list of over 600 services in Drupal core.
143
+
- Most are in the form `date.formatter`.
144
+
- Some are in the form `Drupal\Core\Datetime\DateFormatterInterface`, and are used in autoloading.
145
+
146
+
<!--
147
+
The simple version of date.formatter is what is normal in Drupal.
148
+
The interface form is used for autoloading.
149
+
-->
150
+
---
151
+
146
152
## Using A Service
147
153
148
154
- However! Most of the time you don't want to be using `\Drupal::service()`.
149
-
- Drupal will inject the services you need into your service.
150
-
- This is called <strong>dependency injection</strong>.
155
+
* Unless you are running code in a hook you should be injecting services into your own code.
156
+
* Drupal will handle what services are needed to create your needed service.
157
+
* This is called <strong>dependency injection</strong>.
151
158
152
159
---
153
160
@@ -161,9 +168,6 @@ A quick introduction.
161
168
162
169
Dependecy injection sounds complicated, but its just the practice of <strong>injecting the things the object needs</strong>, instead of <strong>baking them into the class</strong>.
163
170
164
-
<!--
165
-
Some OOP wording here. Make sure this is clear.
166
-
-->
167
171
---
168
172
169
173
## Dependency Injection
@@ -179,6 +183,7 @@ class Page {
179
183
}
180
184
$page = new Page();
181
185
```
186
+
182
187
- What happens if you want to change the credentials? Or change the database itself?
183
188
- You would need to edit the class.
184
189
<!--
@@ -205,21 +210,23 @@ $page = new Page($database);
205
210
Dependency Inversion principle.
206
211
-->
207
212
---
213
+
208
214
## Dependency Injection
209
-
- Drupal handles all the dependency creation.
210
-
- It will create services with all of the required objects in place.
215
+
216
+
- Drupal handles all the dependency creation automatically.
217
+
- It will create services with all of the required objects.
211
218
- All we need to do is ask for our service.
219
+
212
220
---
213
221
214
222
## Why Use Dependency Injection In Drupal?
215
223
216
-
Let's try to create the `path_alias.manager` service to translate a path <em>without</em> using Drupal's dependency injection system.
224
+
Let's try to create the `path_alias.manager` service to translate a path <em>without</em> using Drupal's automatic dependency injection system.
217
225
218
226
---
219
227
220
228
The `path_alias.manager` service wraps the `\Drupal\path_alias\AliasManager` class.
221
229
222
-
223
230
```php
224
231
use Drupal\path_alias\AliasManager;
225
232
@@ -302,7 +309,7 @@ use Drupal\Core\Database\Database;
302
309
$connection = Database::getConnection();
303
310
$pathAliasRepository = new AliasRepository($connection);
304
311
305
-
$cid = 'path_alias_whitelist';
312
+
$cid = 'path_alias_prefix_list';
306
313
$pathPrefixes = new AliasPrefixList($cid, $cache, $lock, $state, $alias_repository);
307
314
308
315
$aliasManager = new AliasManager($pathAliasRepository, $pathPrefixes,
@@ -322,7 +329,7 @@ use Drupal\Core\Site\Settings;
322
329
$connection = Database::getConnection();
323
330
$pathAliasRepository = new AliasRepository($connection);
@@ -335,7 +342,6 @@ $aliasManager = new AliasManager($pathAliasRepository, $pathPrefixes,
335
342
$languageManager, $cache, $time);
336
343
```
337
344
<!--
338
-
The `$cache` property is an object of type `\Drupal\Core\Cache\CacheFactoryInterface`, so we can use `\Drupal\Core\Cache\CacheFactory` to create this. We first need to create a `\Drupal\Core\Site\Settings` object to create that.
339
345
340
346
We have now made several assumptions about our settings, the type of cache we will use, where the cache bin is stored.
341
347
This has created very brittle code that is prone to breakage.
@@ -366,7 +372,7 @@ Seems easier, right?
366
372
367
373
## Creating Services
368
374
369
-
-All services are defined in a `[module].services.yml` file in your module directory.
375
+
-Custom services are defined in a `[module].services.yml` file in your module directory.
370
376
371
377
```yml
372
378
services:
@@ -524,15 +530,17 @@ services:
524
530
- Create your class as normal. The interfaces you nominate will be translated into services and automatically injected into your constructor.
525
531
526
532
```php
527
-
<?php
528
-
529
533
namespace Drupal\services_autowire_example;
530
534
531
-
use Drupal\Component\Serialization\SerializationInterface;
535
+
use Drupal\Core\Password\PasswordGeneratorInterface;
532
536
533
537
class AutowireExample implements AutowireExampleInterface {
534
538
535
-
public function __construct(protected SerializationInterface $serializer) {
539
+
public function __construct(protected PasswordGeneratorInterface $passwordGenerator) {
540
+
}
541
+
542
+
public function generate12CharacterPassword():string {
543
+
return $this->passwordGenerator->generate(12);
536
544
}
537
545
}
538
546
```
@@ -546,7 +554,7 @@ class AutowireExample implements AutowireExampleInterface {
546
554
## Controllers And Forms
547
555
548
556
- Some types of Drupal object (especially Controllers and Forms) don't use `*.services.yml` files.
@@ -606,6 +633,7 @@ Plugins work in the same way, but plugins will have additional arguments that ne
606
633
<!--
607
634
This isn't a rug pull. All your normal hooks will work for the time being.
608
635
-->
636
+
609
637
---
610
638
611
639
## Hook Service Classes
@@ -677,7 +705,7 @@ The aim is to allow low level hooks that have no dependencies to operate as they
677
705
## Tagged Services - Events
678
706
679
707
- The simplest tagged service is the event handler.
680
-
- This service will be triggered when .
708
+
- This service will be triggered when events happen.
681
709
682
710
```yml
683
711
services:
@@ -715,7 +743,7 @@ class EventListenerService implements EventSubscriberInterface {
715
743
<!-- _footer: ""-->
716
744
## Tagged Services - Autoconfigure
717
745
718
-
- Use the `autoconfigure: true` directive to automatically tag classes that implement the `Symfony\Component\EventDispatcher\EventSubscriberInterface` interface
746
+
- Use the `autoconfigure: true` directive to automatically tag classes.
use Drupal\Core\Password\DefaultPasswordGenerator;
801
830
802
831
/**
803
-
* Decorates the Json class.
832
+
* Decorates the DefaultPasswordGenerator class.
804
833
*/
805
834
class DecoratedPasswordGenerator extends DefaultPasswordGenerator {
806
835
@@ -815,11 +844,11 @@ class DecoratedPasswordGenerator extends DefaultPasswordGenerator {
815
844
## Altering Services: Altering
816
845
817
846
- Override the serivce completely and replace it with your own.
818
-
-Create a class that has the name `[ModuleName]ServiceProvider`, which extends the class `\Drupal\Core\DependencyInjection\ServiceProviderBase`.
819
-
-Drupal will pick up this class and run the `register()` and `alter()` methods.
847
+
* Create a class that has the name `[ModuleName]ServiceProvider`, which extends the class <p class="small-text">\Drupal\Core\DependencyInjection\ServiceProviderBase</p>
848
+
* Drupal will pick up this class and run the `register()` and `alter()` methods.
820
849
821
850
<!--
822
-
Use register() to register a new service in Drupal. Useful for highly dynamic services where you want to automatically discover your services at run time.
851
+
Use register() to register a new service in Drupal. Useful for highly dynamic services where you want to automatically create your services at run time.
823
852
824
853
Use alter() to alter the service registry and change any registered service in the site.
825
854
-->
@@ -859,13 +888,11 @@ Here, we are altering the joke_api.joke service to replace it with our a stub se
859
888
860
889
There's much more to Drupal services, try looking up
0 commit comments