Skip to content

Commit

Permalink
fix: migrate translator fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tinect authored Mar 28, 2024
1 parent 775844d commit c9181d4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
<service id="Frosh\TemplateMail\Subscriber\FlowSubscriber">
<argument type="service" id="mail_template_type.repository"/>
<argument type="service" id="Frosh\TemplateMail\Services\MailFinderService"/>
<argument type="service" id="language.repository"/>
+ <argument type="service" id="Shopware\Core\Framework\Adapter\Translation\Translator"/>
<tag name="kernel.event_subscriber"/>
</service>

Expand Down
49 changes: 36 additions & 13 deletions src/Subscriber/FlowSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@
use Shopware\Core\Content\MailTemplate\Aggregate\MailTemplateType\MailTemplateTypeEntity;
use Shopware\Core\Content\MailTemplate\Event\MailSendSubscriberBridgeEvent;
use Shopware\Core\Defaults;
use Shopware\Core\Framework\Adapter\Translation\AbstractTranslator;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\Event\BusinessEvent;
use Shopware\Core\Framework\Validation\DataBag\DataBag;
use Shopware\Core\System\Language\LanguageEntity;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\ParameterBag;

class FlowSubscriber implements EventSubscriberInterface
{
/**
* @var EntityRepository
*/
private $mailTemplateTypeRepository;

/**
* @var MailFinderServiceInterface
*/
private $mailFinderService;
private EntityRepository $mailTemplateTypeRepository;
private MailFinderServiceInterface $mailFinderService;
private EntityRepository $languageRepository;
private AbstractTranslator $translator;

public function __construct(
EntityRepository $mailTemplateTypeRepository,
MailFinderServiceInterface $mailFinderService
MailFinderServiceInterface $mailFinderService,
EntityRepository $languageRepository,
AbstractTranslator $translator
)
{
$this->translator = $translator;
$this->languageRepository = $languageRepository;
$this->mailTemplateTypeRepository = $mailTemplateTypeRepository;
$this->mailFinderService = $mailFinderService;
}
Expand Down Expand Up @@ -65,14 +65,16 @@ public function onMailSendSubscriberBridgeEvent(MailSendSubscriberBridgeEvent $e
$this->sendMail($event->getDataBag(), $event->getMailTemplate()->getMailTemplateTypeId(), $event->getContext());
}

public function sendMail(DataBag $dataBag, string $mailTemplateTypeId, Context $context)
public function sendMail(DataBag $dataBag, string $mailTemplateTypeId, Context $context): void
{
/** @var MailTemplateTypeEntity $mailTemplateType */
$mailTemplateType = $this->mailTemplateTypeRepository->search(new Criteria([$mailTemplateTypeId]), $context) ->first();

$technicalName = $mailTemplateType->getTechnicalName();
$event = $this->createBusinessEventFromBag($dataBag, $context);

$this->fixTranslator($event);

$html = $this->mailFinderService->findTemplateByTechnicalName(MailFinderService::TYPE_HTML, $technicalName, $event);
$plain = $this->mailFinderService->findTemplateByTechnicalName(MailFinderService::TYPE_PLAIN, $technicalName, $event);
$subject = $this->mailFinderService->findTemplateByTechnicalName(MailFinderService::TYPE_SUBJECT, $technicalName, $event);
Expand All @@ -90,8 +92,29 @@ public function sendMail(DataBag $dataBag, string $mailTemplateTypeId, Context $
}
}

private function createBusinessEventFromBag(ParameterBag $dataBag, Context $context): BusinessEvent
private function createBusinessEventFromBag(ParameterBag $dataBag, Context $context): TemplateMailBusinessEvent
{
return new TemplateMailBusinessEvent($dataBag->get('salesChannelId') ?? Defaults::SALES_CHANNEL, $context);
}

private function fixTranslator(TemplateMailBusinessEvent $businessEvent): void
{
$criteria = new Criteria([$businessEvent->getContext()->getLanguageId()]);
$criteria->addAssociation('locale');

/** @var LanguageEntity $language */
$language = $this->languageRepository->search($criteria, $businessEvent->getContext())->first();

$locale = $language->getLocale();
if ($locale === null) {
return;
}

$this->translator->injectSettings(
$businessEvent->getSalesChannelId(),
$businessEvent->getContext()->getLanguageId(),
$locale->getCode(),
$businessEvent->getContext()
);
}
}

0 comments on commit c9181d4

Please sign in to comment.