Skip to content

Commit

Permalink
Merge pull request #209 from delyriand/fix/translated-content-indexing
Browse files Browse the repository at this point in the history
fix: index translations correctly for taxons and attribute value
  • Loading branch information
delyriand authored Jun 3, 2024
2 parents 41ce10d + f02ad40 commit 0549d0c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/AutoMapper/ProductAttributeValueReader/SelectReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public function getValue(ProductAttributeValueInterface $productAttribute)
}

$currentLocale = $productAttribute->getLocaleCode();
$attribute = $productAttribute->getAttribute();
if (null !== $attribute->getTranslation()->getLocale()) {
$currentLocale = $attribute->getTranslation()->getLocale();
}
$choices = $productAttribute->getAttribute()->getConfiguration()['choices'] ?? [];
$productAttributeValue = $productAttribute->getValue();
if (!is_iterable($productAttributeValue)) {
Expand Down
30 changes: 26 additions & 4 deletions src/AutoMapper/ProductMapperConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public function __construct(
$this->channelSimulationContext = $channelSimulationContext;
}

/**
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function process(MapperGeneratorMetadataInterface $metadata): void
{
if (!$metadata instanceof MapperMetadata) {
Expand Down Expand Up @@ -100,13 +103,27 @@ public function process(MapperGeneratorMetadataInterface $metadata): void
});

$metadata->forMember('mainTaxon', function (ProductInterface $product) {
return null !== $product->getMainTaxon()
? $this->autoMapper->map($product->getMainTaxon(), $this->configuration->getTargetClass('taxon'))
: null;
$mainTaxon = $product->getMainTaxon();
if (null === $mainTaxon) {
return null;
}

$currentLocale = $product->getTranslation()->getLocale();
if (null !== $currentLocale) {
$mainTaxon->setCurrentLocale($currentLocale);
}

return $this->autoMapper->map($mainTaxon, $this->configuration->getTargetClass('taxon'));
});

$metadata->forMember('product_taxons', function (ProductInterface $product): array {
return array_map(function (ProductTaxonInterface $productTaxon) {
return array_map(function (ProductTaxonInterface $productTaxon) use ($product) {
$taxon = $productTaxon->getTaxon();
$currentLocale = $product->getTranslation()->getLocale();
if (null !== $currentLocale && null !== $taxon) {
$taxon->setCurrentLocale($currentLocale);
}

// todo add parent taxon in Taxon object with automapper
return $this->autoMapper->map($productTaxon, $this->configuration->getTargetClass('product_taxon'));
}, $product->getProductTaxons()->toArray());
Expand Down Expand Up @@ -149,6 +166,11 @@ public function getAttributes(ProductInterface $product): array
}
$productAttributeDTOClass = $this->configuration->getTargetClass('product_attribute');
foreach ($product->getAttributesByLocale($currentLocale, $currentLocale) as $attributeValue) {
$attribute = $attributeValue->getAttribute();
$currentLocale = $product->getTranslation()->getLocale();
if (null !== $currentLocale && null !== $attribute) {
$attribute->setCurrentLocale($currentLocale);
}
if (null === $attributeValue->getName() || null === $attributeValue->getValue()) {
continue;
}
Expand Down

0 comments on commit 0549d0c

Please sign in to comment.