Skip to content

Commit c8c14af

Browse files
Merge pull request #46 from aligent/fix/image_urls
Include default image attributes in custom attribute data
2 parents fa3e14f + 1ef5f42 commit c8c14af

File tree

2 files changed

+52
-16
lines changed

2 files changed

+52
-16
lines changed

src/index/Model/Data/ImageFieldsProvider.php

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
use Magento\Catalog\Model\View\Asset\Image as ImageAsset;
1212
use Magento\Catalog\Model\View\Asset\ImageFactory;
1313
use Magento\Framework\App\Area;
14+
use Magento\Framework\Exception\LocalizedException;
1415
use Magento\Framework\View\ConfigInterface;
1516
use Magento\Store\Model\App\Emulation;
17+
use Psr\Log\LoggerInterface;
1618

1719
class 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
/**

src/index/etc/di.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,21 @@
4343
<argument name="additionalFieldsProvider" xsi:type="object">additionalFieldsProviderForFredhopper</argument>
4444
</arguments>
4545
</type>
46+
47+
<type name="Aligent\FredhopperCommon\Model\Config\CustomAttributeConfig">
48+
<arguments>
49+
<argument name="customAttributeData" xsi:type="array">
50+
<item name="_imageurl" xsi:type="array">
51+
<item name="attribute_code" xsi:type="string">_imageurl</item>
52+
<item name="fredhopper_type" xsi:type="string">asset</item>
53+
<item name="label" xsi:type="string">Image URL</item>
54+
</item>
55+
<item name="_thumburl" xsi:type="array">
56+
<item name="attribute_code" xsi:type="string">_thumburl</item>
57+
<item name="fredhopper_type" xsi:type="string">asset</item>
58+
<item name="label" xsi:type="string">Thumbnail URL</item>
59+
</item>
60+
</argument>
61+
</arguments>
62+
</type>
4663
</config>

0 commit comments

Comments
 (0)