Skip to content

Commit

Permalink
[FEATURE] Adds Event before bodytext parsing
Browse files Browse the repository at this point in the history
This event is executed before the body text for the individual newsletter is processed. The event can be used to change or reset the body text that is used for parsing.

Relates to: https://projekte.in2code.de/issues/61356
  • Loading branch information
dhoffmann1979 committed Jan 25, 2024
1 parent 8ea5384 commit 3731b4a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
33 changes: 33 additions & 0 deletions Classes/Events/BeforeBodytextIsParsedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);
namespace In2code\Luxletter\Events;

use In2code\Luxletter\Domain\Model\Queue;

final class BeforeBodytextIsParsedEvent
{
protected Queue $queue;

protected string $bodytext = '';

public function getQueue(): Queue
{
return $this->queue;
}

public function setQueue(Queue $queue): void
{
$this->queue = $queue;
}

public function getBodytext(): string
{
return $this->bodytext;
}

public function setBodytext(string $bodytext): void
{
$this->bodytext = $bodytext;
}
}
7 changes: 6 additions & 1 deletion Classes/Mail/ProgressQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use In2code\Luxletter\Domain\Service\BodytextManipulation\LinkHashing;
use In2code\Luxletter\Domain\Service\LogService;
use In2code\Luxletter\Domain\Service\Parsing\Newsletter;
use In2code\Luxletter\Events\BeforeBodytextIsParsedEvent;
use In2code\Luxletter\Events\ProgressQueueEvent;
use In2code\Luxletter\Exception\ArgumentMissingException;
use In2code\Luxletter\Exception\MisconfigurationException;
Expand Down Expand Up @@ -181,8 +182,12 @@ protected function getSubject(Queue $queue): string
*/
protected function getBodyText(Queue $queue): string
{
$event = GeneralUtility::makeInstance(BeforeBodytextIsParsedEvent::class);
$event->setQueue($queue);
$this->eventDispatcher->dispatch($event);

$bodytext = $this->parseService->parseBodytext(
$queue->getNewsletter()->getBodytext(),
$event->getBodytext() !== '' ? $event->getBodytext() : $queue->getNewsletter()->getBodytext(),
[
'user' => $queue->getUser(),
'newsletter' => $queue->getNewsletter(),
Expand Down
28 changes: 28 additions & 0 deletions Documentation/Tech/Events.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,31 @@ final class DemoEventlistener
}
}
```

### BeforeBodytextIsParsedEvent

This event is executed before the body text for the individual newsletter is processed. The event can be used to change or reset the body text that is used for parsing.

The `$queue` property is available in the event, which can be used to access all relevant data for the newsletter.
If the `$bodytext` property is set via an event listener, the content of this property is used for further processing. If there is no content in the property, the standard text from the newsletter is used.

Sample Eventlistener:

```
<?php
declare(strict_types=1);
namespace Vendor\Extension\EventListener;
use In2code\Luxletter\Events\AfterTestMailButtonClickedEvent;
use In2code\Luxletter\Events\BeforeBodytextIsParsedEvent;
final class DemoEventlistener
{
public function __invoke(BeforeBodytextIsParsedEvent $event): void
{
$event->setBodytext('<h1>I am the new body</h1>');
}
}
```

0 comments on commit 3731b4a

Please sign in to comment.