Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 51 additions & 5 deletions guides/plugins/plugins/content/mail/add-data-to-mails.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@

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

```php
// <plugin root>/src/Service/AddDataToMails.php
::: code-group

```php [PLUGIN_ROOT/src/Service/AddDataToMails.php]

Check warning on line 32 in guides/plugins/plugins/content/mail/add-data-to-mails.md

View workflow job for this annotation

GitHub Actions / Runner

[LanguageTool] reported by reviewdog 🐶 File types are normally capitalized. (FILE_EXTENSIONS_CASE[1]) Suggestions: `PHP` URL: https://languagetool.org/insights/post/spelling-capital-letters/ Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1 Category: CASING Raw Output: guides/plugins/plugins/content/mail/add-data-to-mails.md:32:3: File types are normally capitalized. (FILE_EXTENSIONS_CASE[1]) Suggestions: `PHP` URL: https://languagetool.org/insights/post/spelling-capital-letters/ Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1 Category: CASING
<?php declare(strict_types=1);

namespace Swag\BasicExample\Service;
Expand Down Expand Up @@ -63,22 +64,25 @@
}
```

:::

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).

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`.

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

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.
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.

### Register your decorator

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

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

```xml
// <plugin root>/src/Resources/config/services.xml
::: code-group

```xml [PLUGIN_ROOT/src/Resources/config/services.xml]

Check warning on line 85 in guides/plugins/plugins/content/mail/add-data-to-mails.md

View workflow job for this annotation

GitHub Actions / Runner

[LanguageTool] reported by reviewdog 🐶 File types are normally capitalized. (FILE_EXTENSIONS_CASE[1]) Suggestions: `XML` URL: https://languagetool.org/insights/post/spelling-capital-letters/ Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1 Category: CASING Raw Output: guides/plugins/plugins/content/mail/add-data-to-mails.md:85:3: File types are normally capitalized. (FILE_EXTENSIONS_CASE[1]) Suggestions: `XML` URL: https://languagetool.org/insights/post/spelling-capital-letters/ Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1 Category: CASING
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Expand All @@ -91,3 +95,45 @@
</services>
</container>
```

:::

## Adding data via subscriber

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.
Here is a small example:

::: code-group

```php [PLUGIN_ROOT/src/Subscriber/MyMailSubscriber.php]

Check warning on line 108 in guides/plugins/plugins/content/mail/add-data-to-mails.md

View workflow job for this annotation

GitHub Actions / Runner

[LanguageTool] reported by reviewdog 🐶 File types are normally capitalized. (FILE_EXTENSIONS_CASE[1]) Suggestions: `PHP` URL: https://languagetool.org/insights/post/spelling-capital-letters/ Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1 Category: CASING Raw Output: guides/plugins/plugins/content/mail/add-data-to-mails.md:108:3: File types are normally capitalized. (FILE_EXTENSIONS_CASE[1]) Suggestions: `PHP` URL: https://languagetool.org/insights/post/spelling-capital-letters/ Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1 Category: CASING
<?php

declare(strict_types=1);

namespace Swag\BasicExample\Subscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Content\MailTemplate\Service\Event\MailBeforeValidateEvent;

class MyMailSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
MailBeforeValidateEvent::class => 'beforeMailValidate',
];
}

public function beforeMailValidate(
MailBeforeValidateEvent $event
): void {
$context = $event->getContext();
$data = $event->getData(); // Get mail data
$templateData = $event->getTemplateData(); // Get mail template data

$event->addTemplateData('key', 'value'); // Example of adding data to the mail template
}
}
```

:::