Skip to content

Commit

Permalink
Merge pull request #46 from aligent/fix/image_urls
Browse files Browse the repository at this point in the history
Include default image attributes in custom attribute data
  • Loading branch information
aligent-lturner authored Nov 14, 2024
2 parents fa3e14f + 1ef5f42 commit c8c14af
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 16 deletions.
51 changes: 35 additions & 16 deletions src/index/Model/Data/ImageFieldsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
use Magento\Catalog\Model\View\Asset\Image as ImageAsset;
use Magento\Catalog\Model\View\Asset\ImageFactory;
use Magento\Framework\App\Area;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\View\ConfigInterface;
use Magento\Store\Model\App\Emulation;
use Psr\Log\LoggerInterface;

class ImageFieldsProvider implements AdditionalFieldsProviderInterface
{
Expand All @@ -27,6 +29,7 @@ class ImageFieldsProvider implements AdditionalFieldsProviderInterface
* @param ImageFactory $imageAssetFactory
* @param ConfigInterface $presentationConfig
* @param Emulation $emulation
* @param LoggerInterface $logger
* @param array $imageAttributeConfig
*/
public function __construct(
Expand All @@ -35,6 +38,7 @@ public function __construct(
private readonly ImageFactory $imageAssetFactory,
private readonly ConfigInterface $presentationConfig,
private readonly Emulation $emulation,
private readonly LoggerInterface $logger,
private readonly array $imageAttributeConfig = []
) {
}
Expand All @@ -58,34 +62,39 @@ public function getFields(array $productIds, $storeId): array
$result = [];
foreach ($products as $productId => $product) {
foreach ($this->imageAttributeConfig as $fredhopperAttribute => $imageConfig) {
$imageParams = $this->getImageParamsForStore($imageConfig['display_area'], (int)$storeId);
/** @var ImageAsset $asset */
$asset = $this->imageAssetFactory->create([
'miscParams' => $imageParams,
'filePath' => $product->getData($imageConfig['attribute_code']),
]);
$result[$productId][$fredhopperAttribute] = $asset->getUrl();
$path = $product->getData($imageConfig['attribute_code']);
try {
$imageUrl = $this->getImageUrlForStore($imageConfig['display_area'], (int)$storeId, $path);
} catch (LocalizedException $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
continue;
}

$result[$productId][$fredhopperAttribute] = $imageUrl;
}
}
return $result;
}

/**
* Get image parameters for a given store
* Get image url for a given store and path
*
* @param string $imageDisplayArea
* @param int $storeId
* @return array
* @param string $path
* @return string
* @throws LocalizedException
*/
private function getImageParamsForStore(string $imageDisplayArea, int $storeId): array
private function getImageUrlForStore(string $imageDisplayArea, int $storeId, string $path): string
{
$this->emulation->startEnvironmentEmulation(
$storeId,
Area::AREA_FRONTEND,
true
);

if (!isset($this->imageParams[$imageDisplayArea][$storeId])) {
try {
$this->emulation->startEnvironmentEmulation(
$storeId,
Area::AREA_FRONTEND,
true
);
$imageArguments = $this->getImageParams($imageDisplayArea);
$this->imageParams[$imageDisplayArea][$storeId] = $this->paramsBuilder->build($imageArguments);
} catch (\Exception) {
Expand All @@ -96,7 +105,17 @@ private function getImageParamsForStore(string $imageDisplayArea, int $storeId):
$this->emulation->stopEnvironmentEmulation();
}
}
return $this->imageParams[$imageDisplayArea][$storeId];
/** @var ImageAsset $asset */
$asset = $this->imageAssetFactory->create(
[
'miscParams' => $this->imageParams[$imageDisplayArea][$storeId],
'filePath' => $path
]
);
$url = $asset->getUrl();
// always stop emulation
$this->emulation->stopEnvironmentEmulation();
return $url;
}

/**
Expand Down
17 changes: 17 additions & 0 deletions src/index/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,21 @@
<argument name="additionalFieldsProvider" xsi:type="object">additionalFieldsProviderForFredhopper</argument>
</arguments>
</type>

<type name="Aligent\FredhopperCommon\Model\Config\CustomAttributeConfig">
<arguments>
<argument name="customAttributeData" xsi:type="array">
<item name="_imageurl" xsi:type="array">
<item name="attribute_code" xsi:type="string">_imageurl</item>
<item name="fredhopper_type" xsi:type="string">asset</item>
<item name="label" xsi:type="string">Image URL</item>
</item>
<item name="_thumburl" xsi:type="array">
<item name="attribute_code" xsi:type="string">_thumburl</item>
<item name="fredhopper_type" xsi:type="string">asset</item>
<item name="label" xsi:type="string">Thumbnail URL</item>
</item>
</argument>
</arguments>
</type>
</config>

0 comments on commit c8c14af

Please sign in to comment.