Skip to content

Commit 0f6e435

Browse files
committed
PWA-806: Localize emails sent through GraphQL application
- Emulate correct store around email sends through GraphQL
1 parent 8b51cf8 commit 0f6e435

File tree

4 files changed

+81
-71
lines changed

4 files changed

+81
-71
lines changed

app/code/Magento/StoreGraphQl/Controller/HttpHeaderProcessor/StoreProcessor.php

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77

88
namespace Magento\StoreGraphQl\Controller\HttpHeaderProcessor;
99

10-
use Magento\Framework\App\AreaInterface;
11-
use Magento\Framework\App\AreaList;
12-
use Magento\Framework\App\ObjectManager;
13-
use Magento\Framework\App\State;
1410
use Magento\GraphQl\Controller\HttpHeaderProcessorInterface;
1511
use Magento\Store\Model\StoreManagerInterface;
1612
use Magento\Framework\App\Http\Context as HttpContext;
@@ -36,35 +32,19 @@ class StoreProcessor implements HttpHeaderProcessorInterface
3632
*/
3733
private $storeCookieManager;
3834

39-
/**
40-
* @var AreaList
41-
*/
42-
private $areaList;
43-
44-
/**
45-
* @var State
46-
*/
47-
private $appState;
48-
4935
/**
5036
* @param StoreManagerInterface $storeManager
5137
* @param HttpContext $httpContext
5238
* @param StoreCookieManagerInterface $storeCookieManager
53-
* @param AreaList $areaList
54-
* @param State $appState
5539
*/
5640
public function __construct(
5741
StoreManagerInterface $storeManager,
5842
HttpContext $httpContext,
59-
StoreCookieManagerInterface $storeCookieManager,
60-
AreaList $areaList = null,
61-
State $appState = null
43+
StoreCookieManagerInterface $storeCookieManager
6244
) {
6345
$this->storeManager = $storeManager;
6446
$this->httpContext = $httpContext;
6547
$this->storeCookieManager = $storeCookieManager;
66-
$this->areaList = $areaList ?? ObjectManager::getInstance()->get(AreaList::class);
67-
$this->appState = $appState ?? ObjectManager::getInstance()->get(State::class);
6848
}
6949

7050
/**
@@ -87,10 +67,6 @@ public function processHeaderValue(string $headerValue) : void
8767
$this->storeManager->setCurrentStore($storeCode);
8868
$this->updateContext($storeCode);
8969
}
90-
91-
// Load translations for the app
92-
$area = $this->areaList->getArea($this->appState->getAreaCode());
93-
$area->load(AreaInterface::PART_TRANSLATE);
9470
}
9571

9672
/**
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace Magento\StoreGraphQl\Plugin;
4+
5+
use Magento\Framework\App\AreaInterface;
6+
use Magento\Framework\App\AreaList;
7+
use Magento\Framework\App\State;
8+
use Magento\Framework\Exception\LocalizedException;
9+
use Magento\Framework\Exception\NoSuchEntityException;
10+
use Magento\Framework\Mail\Template\TransportBuilder;
11+
use Magento\Store\Model\App\Emulation;
12+
use Magento\Store\Model\StoreManagerInterface;
13+
14+
/**
15+
* Emulate the correct store when GraphQL is sending an email
16+
*/
17+
class LocalizeEmail
18+
{
19+
/**
20+
* @var StoreManagerInterface
21+
*/
22+
private $storeManager;
23+
24+
/**
25+
* @var Emulation
26+
*/
27+
private $emulation;
28+
29+
/**
30+
* @var AreaList
31+
*/
32+
private $areaList;
33+
34+
/**
35+
* @var State
36+
*/
37+
private $appState;
38+
39+
/**
40+
* @param StoreManagerInterface $storeManager
41+
* @param Emulation $emulation
42+
* @param AreaList $areaList
43+
* @param State $appState
44+
*/
45+
public function __construct(
46+
StoreManagerInterface $storeManager,
47+
Emulation $emulation,
48+
AreaList $areaList,
49+
State $appState
50+
) {
51+
$this->storeManager = $storeManager;
52+
$this->emulation = $emulation;
53+
$this->areaList = $areaList;
54+
$this->appState = $appState;
55+
}
56+
57+
/**
58+
* Emulate the correct store during email preparation
59+
*
60+
* @param TransportBuilder $subject
61+
* @param \Closure $proceed
62+
* @return mixed
63+
* @throws NoSuchEntityException|LocalizedException
64+
*/
65+
public function aroundGetTransport(TransportBuilder $subject, \Closure $proceed)
66+
{
67+
// Load translations for the app
68+
$area = $this->areaList->getArea($this->appState->getAreaCode());
69+
$area->load(AreaInterface::PART_TRANSLATE);
70+
71+
$currentStore = $this->storeManager->getStore();
72+
$this->emulation->startEnvironmentEmulation($currentStore->getId());
73+
$output = $proceed();
74+
$this->emulation->stopEnvironmentEmulation();
75+
76+
return $output;
77+
}
78+
}

app/code/Magento/StoreGraphQl/Plugin/StoreResolver.php

Lines changed: 0 additions & 44 deletions
This file was deleted.

app/code/Magento/StoreGraphQl/etc/graphql/di.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
</argument>
2424
</arguments>
2525
</type>
26-
<type name="Magento\Store\Model\Resolver\Store">
27-
<plugin name="graphQlLocalizationStoreResolver" type="Magento\StoreGraphQl\Plugin\StoreResolver" />
26+
<type name="Magento\Framework\Mail\Template\TransportBuilder">
27+
<plugin name="graphQlEmulateEmail" type="Magento\StoreGraphQl\Plugin\LocalizeEmail" />
2828
</type>
2929
</config>

0 commit comments

Comments
 (0)