Add daily email
This commit is contained in:
parent
3c04dfd710
commit
dafc7430b9
3 changed files with 150 additions and 0 deletions
|
@ -7074,5 +7074,12 @@
|
||||||
],
|
],
|
||||||
"path_alias.dd4cf5d9-eed7-4cab-b865-9fe0f927255c": [
|
"path_alias.dd4cf5d9-eed7-4cab-b865-9fe0f927255c": [
|
||||||
"node.d1fb428c-7494-4865-8da0-99f4549ffee1"
|
"node.d1fb428c-7494-4865-8da0-99f4549ffee1"
|
||||||
|
],
|
||||||
|
"node.22301027-1726-463c-bdfc-a21deca00f34": [
|
||||||
|
"user.b8966985-d4b2-42a7-a319-2e94ccfbb849",
|
||||||
|
"node.e3f6c728-7855-4804-8614-e2a0c08c368f"
|
||||||
|
],
|
||||||
|
"path_alias.a77f12b4-c1c3-43d4-9e22-8a7500e30c0c": [
|
||||||
|
"node.22301027-1726-463c-bdfc-a21deca00f34"
|
||||||
]
|
]
|
||||||
}
|
}
|
133
content/node.22301027-1726-463c-bdfc-a21deca00f34.yml
Normal file
133
content/node.22301027-1726-463c-bdfc-a21deca00f34.yml
Normal file
|
@ -0,0 +1,133 @@
|
||||||
|
uuid:
|
||||||
|
- value: 22301027-1726-463c-bdfc-a21deca00f34
|
||||||
|
langcode:
|
||||||
|
- value: en
|
||||||
|
type:
|
||||||
|
- target_id: daily_email
|
||||||
|
target_type: node_type
|
||||||
|
target_uuid: 8bde1f2f-eef9-4f2d-ae9c-96921f8193d7
|
||||||
|
revision_timestamp:
|
||||||
|
- value: '2025-07-14T18:15:51+00:00'
|
||||||
|
revision_uid:
|
||||||
|
- target_type: user
|
||||||
|
target_uuid: b8966985-d4b2-42a7-a319-2e94ccfbb849
|
||||||
|
revision_log: { }
|
||||||
|
status:
|
||||||
|
- value: true
|
||||||
|
uid:
|
||||||
|
- target_type: user
|
||||||
|
target_uuid: b8966985-d4b2-42a7-a319-2e94ccfbb849
|
||||||
|
title:
|
||||||
|
- value: 'Easier dependency injection with autowiring'
|
||||||
|
created:
|
||||||
|
- value: '2025-07-11T18:12:23+00:00'
|
||||||
|
changed:
|
||||||
|
- value: '2025-07-14T18:15:51+00:00'
|
||||||
|
promote:
|
||||||
|
- value: false
|
||||||
|
sticky:
|
||||||
|
- value: false
|
||||||
|
default_langcode:
|
||||||
|
- value: true
|
||||||
|
revision_translation_affected:
|
||||||
|
- value: true
|
||||||
|
path:
|
||||||
|
- alias: /daily/2025/07/11/easier-dependency-injection-autowiring
|
||||||
|
langcode: en
|
||||||
|
body:
|
||||||
|
- value: |-
|
||||||
|
Dependency injection is, as the name suggests, the approach of injecting dependencies into a class - usually within a `__construct` method.
|
||||||
|
|
||||||
|
This makes code less coupled and easier to test.
|
||||||
|
|
||||||
|
In a Drupal application, that usually means not using the static methods on `\Drupal` when fetching dependencies.
|
||||||
|
|
||||||
|
This is an example from my website:
|
||||||
|
|
||||||
|
```php
|
||||||
|
<?php
|
||||||
|
|
||||||
|
readonly final class PodcastNodeRepository {
|
||||||
|
|
||||||
|
private NodeStorageInterface $nodeStorage;
|
||||||
|
|
||||||
|
public function __construct(EntityTypeManagerInterface $entityTypeManager) {
|
||||||
|
$this->nodeStorage = $entityTypeManager->getStorage('node');
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
The `EntityTypeManagerInterface` dependency is injected and used to get the node storage class, which is used in another method to find the podcast episode nodes.
|
||||||
|
|
||||||
|
For this to work with the dependency injection container, I need to add the class as a service, including its arguments so they can be injected:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
# opd_podcast.services.yml
|
||||||
|
|
||||||
|
services:
|
||||||
|
Drupal\opd_podcast\Action\GetNextPodcastEpisodeNumber:
|
||||||
|
arguments:
|
||||||
|
- '@entity_type.manager'
|
||||||
|
```
|
||||||
|
|
||||||
|
But, this means you need to update this file if any dependencies change.
|
||||||
|
|
||||||
|
Wouldn't it be better to do this automatically?
|
||||||
|
|
||||||
|
Usually, it can with autowiring.
|
||||||
|
|
||||||
|
Instead of `arguments`, add `autowire: true` and Drupal will try to automatically inject the dependencies for you:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
# opd_podcast.services.yml
|
||||||
|
|
||||||
|
services:
|
||||||
|
Drupal\opd_podcast\Action\GetNextPodcastEpisodeNumber:
|
||||||
|
autowire: true
|
||||||
|
Drupal\opd_podcast\Repository\PodcastNodeRepository:
|
||||||
|
autowire: true
|
||||||
|
```
|
||||||
|
|
||||||
|
Now, if dependencies change, you don't need to update the services file - making it easier and quicker.
|
||||||
|
format: markdown
|
||||||
|
processed: |
|
||||||
|
<p>Dependency injection is, as the name suggests, the approach of injecting dependencies into a class - usually within a <code>__construct</code> method.</p>
|
||||||
|
<p>This makes code less coupled and easier to test.</p>
|
||||||
|
<p>In a Drupal application, that usually means not using the static methods on <code>\Drupal</code> when fetching dependencies.</p>
|
||||||
|
<p>This is an example from my website:</p>
|
||||||
|
<pre><code><?php
|
||||||
|
|
||||||
|
readonly final class PodcastNodeRepository {
|
||||||
|
|
||||||
|
private NodeStorageInterface $nodeStorage;
|
||||||
|
|
||||||
|
public function __construct(EntityTypeManagerInterface $entityTypeManager) {
|
||||||
|
$this->nodeStorage = $entityTypeManager->getStorage('node');
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
</code></pre><p>The <code>EntityTypeManagerInterface</code> dependency is injected and used to get the node storage class, which is used in another method to find the podcast episode nodes.</p>
|
||||||
|
<p>For this to work with the dependency injection container, I need to add the class as a service, including its arguments so they can be injected:</p>
|
||||||
|
<pre><code># opd_podcast.services.yml
|
||||||
|
|
||||||
|
services:
|
||||||
|
Drupal\opd_podcast\Action\GetNextPodcastEpisodeNumber:
|
||||||
|
arguments:
|
||||||
|
- '@entity_type.manager'
|
||||||
|
</code></pre><p>But, this means you need to update this file if any dependencies change.</p>
|
||||||
|
<p>Wouldn't it be better to do this automatically?</p>
|
||||||
|
<p>Usually, it can with autowiring.</p>
|
||||||
|
<p>Instead of <code>arguments</code>, add <code>autowire: true</code> and Drupal will try to automatically inject the dependencies for you:</p>
|
||||||
|
<pre><code># opd_podcast.services.yml
|
||||||
|
|
||||||
|
services:
|
||||||
|
Drupal\opd_podcast\Action\GetNextPodcastEpisodeNumber:
|
||||||
|
autowire: true
|
||||||
|
Drupal\opd_podcast\Repository\PodcastNodeRepository:
|
||||||
|
autowire: true
|
||||||
|
</code></pre><p>Now, if dependencies change, you don't need to update the services file - making it easier and quicker.</p>
|
||||||
|
summary: ''
|
||||||
|
field_daily_email_cta:
|
||||||
|
- target_type: node
|
||||||
|
target_uuid: e3f6c728-7855-4804-8614-e2a0c08c368f
|
10
content/path_alias.a77f12b4-c1c3-43d4-9e22-8a7500e30c0c.yml
Normal file
10
content/path_alias.a77f12b4-c1c3-43d4-9e22-8a7500e30c0c.yml
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
uuid:
|
||||||
|
- value: a77f12b4-c1c3-43d4-9e22-8a7500e30c0c
|
||||||
|
langcode:
|
||||||
|
- value: en
|
||||||
|
path:
|
||||||
|
- value: /node/22301027-1726-463c-bdfc-a21deca00f34
|
||||||
|
alias:
|
||||||
|
- value: /daily/2025/07/11/easier-dependency-injection-autowiring
|
||||||
|
status:
|
||||||
|
- value: true
|
Loading…
Add table
Add a link
Reference in a new issue