Skip to content

Commit 4296168

Browse files
ROBJkEIsengo1989Copilot
authored
feat: describe how to set mail data via subscriber (shopware#1887)
* feat: decribe how to set mail data via subscriber * fix typo * add newlines * add inline comments, remove setTemplateData() part * fix: twig-inline-code-and-xml * Update guides/plugins/plugins/content/mail/add-data-to-mails.md Co-authored-by: Copilot <[email protected]> * Update guides/plugins/plugins/content/mail/add-data-to-mails.md Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Micha <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent 4b2c925 commit 4296168

File tree

1 file changed

+51
-5
lines changed

1 file changed

+51
-5
lines changed

guides/plugins/plugins/content/mail/add-data-to-mails.md

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ To be precise, you have to extend the `send` method, whose last parameter is the
2727

2828
So let's do that, here's an example of a decorated mail service:
2929

30-
```php
31-
// <plugin root>/src/Service/AddDataToMails.php
30+
::: code-group
31+
32+
```php [PLUGIN_ROOT/src/Service/AddDataToMails.php]
3233
<?php declare(strict_types=1);
3334

3435
namespace Swag\BasicExample\Service;
@@ -63,22 +64,25 @@ class AddDataToMails extends AbstractMailService
6364
}
6465
```
6566

67+
:::
68+
6669
If you don't recognise the decoration pattern used here, make sure to have a look at our guide about [decorations](../../plugin-fundamentals/adjusting-service).
6770

6871
As always, we're passing in the original `MailService` as a constructor parameter, so we can return it in the `getDecorated` method, as well as use the original `send` method after having adjusted the `$templateData`.
6972

7073
In this example, we're adding `myCustomData` to the `$templateData`, so that one should be available then.
7174

72-
If we add `{{ myCustomData }}` to any mail template, it should then print "Example data". You can use any kind of data here, e.g. an array of data.
75+
If we add <code v-pre>{{ myCustomData }}</code> to any mail template, it should then print "Example data". You can use any kind of data here, e.g. an array of data.
7376

7477
### Register your decorator
7578

7679
Of course you still have to register the decoration to the service container. Beware of the `decorates` attribute of our service.
7780

7881
Here's the respective example `services.xml`:
7982

80-
```xml
81-
// <plugin root>/src/Resources/config/services.xml
83+
::: code-group
84+
85+
```xml [PLUGIN_ROOT/src/Resources/config/services.xml]
8286
<?xml version="1.0" ?>
8387
<container xmlns="http://symfony.com/schema/dic/services"
8488
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -91,3 +95,45 @@ Here's the respective example `services.xml`:
9195
</services>
9296
</container>
9397
```
98+
99+
:::
100+
101+
## Adding data via subscriber
102+
103+
In many cases, adding mail data via an event subscriber is a suitable solution. This way, you avoid the overhead of decorating the mail service. Simply create an event subscriber and listen to the `MailBeforeValidateEvent` event. There, you can safely add template or mail data.
104+
Here is a small example:
105+
106+
::: code-group
107+
108+
```php [PLUGIN_ROOT/src/Subscriber/MyMailSubscriber.php]
109+
<?php
110+
111+
declare(strict_types=1);
112+
113+
namespace Swag\BasicExample\Subscriber;
114+
115+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
116+
use Shopware\Core\Content\MailTemplate\Service\Event\MailBeforeValidateEvent;
117+
118+
class MyMailSubscriber implements EventSubscriberInterface
119+
{
120+
public static function getSubscribedEvents(): array
121+
{
122+
return [
123+
MailBeforeValidateEvent::class => 'beforeMailValidate',
124+
];
125+
}
126+
127+
public function beforeMailValidate(
128+
MailBeforeValidateEvent $event
129+
): void {
130+
$context = $event->getContext();
131+
$data = $event->getData(); // Get mail data
132+
$templateData = $event->getTemplateData(); // Get mail template data
133+
134+
$event->addTemplateData('key', 'value'); // Example of adding data to the mail template
135+
}
136+
}
137+
```
138+
139+
:::

0 commit comments

Comments
 (0)