8
8
9
9
namespace Elgentos \PrismicIO \ViewModel ;
10
10
11
+ use Elgentos \PrismicIO \Api \ConfigurationInterface ;
11
12
use Elgentos \PrismicIO \Api \RouteRepositoryInterface ;
12
13
use Elgentos \PrismicIO \Exception \RouteNotFoundException ;
13
14
use Magento \Framework \UrlInterface ;
@@ -41,17 +42,35 @@ class LinkResolver extends LinkResolverAbstract implements ArgumentInterface
41
42
* @var array
42
43
*/
43
44
private $ urlCache = [];
45
+ /**
46
+ * @var ConfigurationInterface
47
+ */
48
+ private $ configuration ;
49
+ /**
50
+ * @var array
51
+ */
52
+ private $ cachedLanguageStoreIds ;
44
53
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
+ */
45
62
public function __construct (
46
63
UrlInterface $ urlBuilder ,
47
64
StoreManagerInterface $ storeManager ,
48
65
RouteRepositoryInterface $ routeRepository ,
49
- UrlFinderInterface $ urlFinder
66
+ UrlFinderInterface $ urlFinder ,
67
+ ConfigurationInterface $ configuration
50
68
) {
51
69
$ this ->urlBuilder = $ urlBuilder ;
52
70
$ this ->storeManager = $ storeManager ;
53
71
$ this ->routeRepository = $ routeRepository ;
54
72
$ this ->urlFinder = $ urlFinder ;
73
+ $ this ->configuration = $ configuration ;
55
74
}
56
75
57
76
/**
@@ -78,7 +97,7 @@ public function getMediaUrl(\stdClass $link): ?string
78
97
79
98
public function getStore (\stdClass $ link ): StoreInterface
80
99
{
81
- $ storeId = $ link ->store ?? $ link ->store_id ?? null ;
100
+ $ storeId = $ link ->store ?? $ link ->store_id ?? $ this -> getStoreIdFromLink ( $ link ) ?? null ;
82
101
return $ this ->storeManager ->getStore ($ storeId );
83
102
}
84
103
@@ -230,4 +249,40 @@ public function getUrlRewrite(string $targetPath, StoreInterface $store)
230
249
]);
231
250
}
232
251
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
+
233
288
}
0 commit comments