1111use Magento \Catalog \Model \View \Asset \Image as ImageAsset ;
1212use Magento \Catalog \Model \View \Asset \ImageFactory ;
1313use Magento \Framework \App \Area ;
14+ use Magento \Framework \Exception \LocalizedException ;
1415use Magento \Framework \View \ConfigInterface ;
1516use Magento \Store \Model \App \Emulation ;
17+ use Psr \Log \LoggerInterface ;
1618
1719class ImageFieldsProvider implements AdditionalFieldsProviderInterface
1820{
@@ -27,6 +29,7 @@ class ImageFieldsProvider implements AdditionalFieldsProviderInterface
2729 * @param ImageFactory $imageAssetFactory
2830 * @param ConfigInterface $presentationConfig
2931 * @param Emulation $emulation
32+ * @param LoggerInterface $logger
3033 * @param array $imageAttributeConfig
3134 */
3235 public function __construct (
@@ -35,6 +38,7 @@ public function __construct(
3538 private readonly ImageFactory $ imageAssetFactory ,
3639 private readonly ConfigInterface $ presentationConfig ,
3740 private readonly Emulation $ emulation ,
41+ private readonly LoggerInterface $ logger ,
3842 private readonly array $ imageAttributeConfig = []
3943 ) {
4044 }
@@ -58,34 +62,39 @@ public function getFields(array $productIds, $storeId): array
5862 $ result = [];
5963 foreach ($ products as $ productId => $ product ) {
6064 foreach ($ this ->imageAttributeConfig as $ fredhopperAttribute => $ imageConfig ) {
61- $ imageParams = $ this ->getImageParamsForStore ($ imageConfig ['display_area ' ], (int )$ storeId );
62- /** @var ImageAsset $asset */
63- $ asset = $ this ->imageAssetFactory ->create ([
64- 'miscParams ' => $ imageParams ,
65- 'filePath ' => $ product ->getData ($ imageConfig ['attribute_code ' ]),
66- ]);
67- $ result [$ productId ][$ fredhopperAttribute ] = $ asset ->getUrl ();
65+ $ path = $ product ->getData ($ imageConfig ['attribute_code ' ]);
66+ try {
67+ $ imageUrl = $ this ->getImageUrlForStore ($ imageConfig ['display_area ' ], (int )$ storeId , $ path );
68+ } catch (LocalizedException $ e ) {
69+ $ this ->logger ->error ($ e ->getMessage (), ['exception ' => $ e ]);
70+ continue ;
71+ }
72+
73+ $ result [$ productId ][$ fredhopperAttribute ] = $ imageUrl ;
6874 }
6975 }
7076 return $ result ;
7177 }
7278
7379 /**
74- * Get image parameters for a given store
80+ * Get image url for a given store and path
7581 *
7682 * @param string $imageDisplayArea
7783 * @param int $storeId
78- * @return array
84+ * @param string $path
85+ * @return string
86+ * @throws LocalizedException
7987 */
80- private function getImageParamsForStore (string $ imageDisplayArea , int $ storeId ): array
88+ private function getImageUrlForStore (string $ imageDisplayArea , int $ storeId, string $ path ): string
8189 {
90+ $ this ->emulation ->startEnvironmentEmulation (
91+ $ storeId ,
92+ Area::AREA_FRONTEND ,
93+ true
94+ );
95+
8296 if (!isset ($ this ->imageParams [$ imageDisplayArea ][$ storeId ])) {
8397 try {
84- $ this ->emulation ->startEnvironmentEmulation (
85- $ storeId ,
86- Area::AREA_FRONTEND ,
87- true
88- );
8998 $ imageArguments = $ this ->getImageParams ($ imageDisplayArea );
9099 $ this ->imageParams [$ imageDisplayArea ][$ storeId ] = $ this ->paramsBuilder ->build ($ imageArguments );
91100 } catch (\Exception ) {
@@ -96,7 +105,17 @@ private function getImageParamsForStore(string $imageDisplayArea, int $storeId):
96105 $ this ->emulation ->stopEnvironmentEmulation ();
97106 }
98107 }
99- return $ this ->imageParams [$ imageDisplayArea ][$ storeId ];
108+ /** @var ImageAsset $asset */
109+ $ asset = $ this ->imageAssetFactory ->create (
110+ [
111+ 'miscParams ' => $ this ->imageParams [$ imageDisplayArea ][$ storeId ],
112+ 'filePath ' => $ path
113+ ]
114+ );
115+ $ url = $ asset ->getUrl ();
116+ // always stop emulation
117+ $ this ->emulation ->stopEnvironmentEmulation ();
118+ return $ url ;
100119 }
101120
102121 /**
0 commit comments