Skip to content

Commit f2a12e4

Browse files
committed
Resolve linked store for a given link based on lang property
+ reverse search the right store based main language for a document linked in Magento
1 parent 09faae1 commit f2a12e4

File tree

1 file changed

+57
-2
lines changed

1 file changed

+57
-2
lines changed

ViewModel/LinkResolver.php

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Elgentos\PrismicIO\ViewModel;
1010

11+
use Elgentos\PrismicIO\Api\ConfigurationInterface;
1112
use Elgentos\PrismicIO\Api\RouteRepositoryInterface;
1213
use Elgentos\PrismicIO\Exception\RouteNotFoundException;
1314
use Magento\Framework\UrlInterface;
@@ -41,17 +42,35 @@ class LinkResolver extends LinkResolverAbstract implements ArgumentInterface
4142
* @var array
4243
*/
4344
private $urlCache = [];
45+
/**
46+
* @var ConfigurationInterface
47+
*/
48+
private $configuration;
49+
/**
50+
* @var array
51+
*/
52+
private $cachedLanguageStoreIds;
4453

54+
/**
55+
* LinkResolver constructor.
56+
* @param UrlInterface $urlBuilder
57+
* @param StoreManagerInterface $storeManager
58+
* @param RouteRepositoryInterface $routeRepository
59+
* @param UrlFinderInterface $urlFinder
60+
* @param ConfigurationInterface $configuration
61+
*/
4562
public function __construct(
4663
UrlInterface $urlBuilder,
4764
StoreManagerInterface $storeManager,
4865
RouteRepositoryInterface $routeRepository,
49-
UrlFinderInterface $urlFinder
66+
UrlFinderInterface $urlFinder,
67+
ConfigurationInterface $configuration
5068
) {
5169
$this->urlBuilder = $urlBuilder;
5270
$this->storeManager = $storeManager;
5371
$this->routeRepository = $routeRepository;
5472
$this->urlFinder = $urlFinder;
73+
$this->configuration = $configuration;
5574
}
5675

5776
/**
@@ -78,7 +97,7 @@ public function getMediaUrl(\stdClass $link): ?string
7897

7998
public function getStore(\stdClass $link): StoreInterface
8099
{
81-
$storeId = $link->store ?? $link->store_id ?? null;
100+
$storeId = $link->store ?? $link->store_id ?? $this->getStoreIdFromLink($link) ?? null;
82101
return $this->storeManager->getStore($storeId);
83102
}
84103

@@ -230,4 +249,40 @@ public function getUrlRewrite(string $targetPath, StoreInterface $store)
230249
]);
231250
}
232251

252+
/**
253+
* Resolve store id from $link->lang to a valid storeId
254+
*
255+
* @param \stdClass $link
256+
* @return int|null
257+
*/
258+
public function getStoreIdFromLink(\stdClass $link): ?int
259+
{
260+
if (! isset($link->lang)) {
261+
return null;
262+
}
263+
264+
return $this->getLanguageStoreIds()[$link->lang] ?? null;
265+
}
266+
267+
/**
268+
* Get reversed language code to store id mapping
269+
*
270+
* @return array
271+
*/
272+
private function getLanguageStoreIds(): array
273+
{
274+
if (null !== $this->cachedLanguageStoreIds) {
275+
return $this->cachedLanguageStoreIds;
276+
}
277+
278+
$languageStoreIds = [];
279+
foreach ($this->storeManager->getStores() as $store) {
280+
$languageCode = $this->configuration->getContentLanguage($store);
281+
$languageStoreIds[$languageCode] = $languageStoreIds[$languageCode] ?? +$store->getId();
282+
}
283+
284+
$this->cachedLanguageStoreIds = $languageStoreIds;
285+
return $languageStoreIds;
286+
}
287+
233288
}

0 commit comments

Comments
 (0)