Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#3117 - Fix attribute option translation while reindexing #3135

Open
wants to merge 6 commits into
base: 2.11.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
use Smile\ElasticsuiteCore\Api\Index\DatasourceInterface;
use Smile\ElasticsuiteCore\Api\Index\Mapping\DynamicFieldProviderInterface;
use Smile\ElasticsuiteCore\Index\Mapping\FieldFactory;
use Magento\Framework\Locale\ResolverInterface;
use Magento\Framework\TranslateInterface;
use Magento\Framework\App\State;
use Magento\Framework\App\AreaList;

/**
* Datasource used to index product attributes.
Expand Down Expand Up @@ -56,6 +60,26 @@ class AttributeData extends AbstractAttributeData implements DatasourceInterface
*/
private $isIndexingChildProductSkuEnabled;

/**
* @var ResolverInterface
*/
private $localeResolver;

/**
* @var TranslateInterface
*/
private $translator;

/**
* @var State
*/
private $appState;

/**
* @var AreaList
*/
private $areaList;

/**
* Constructor
*
Expand All @@ -65,26 +89,45 @@ class AttributeData extends AbstractAttributeData implements DatasourceInterface
* @param array $indexedBackendModels List of indexed backend models added to the default list.
* @param array $forbiddenChildrenAttributes List of the forbidden children attributes.
* @param ScopeConfigInterface $scopeConfig Scope Config.
* @param ResolverInterface $localeResolver The locale resolver
* @param TranslateInterface $translator The translator handler
* @param State $appState The app state
* @param AreaList $areaList Area list helper
*/
public function __construct(
ResourceModel $resourceModel,
FieldFactory $fieldFactory,
AttributeHelper $attributeHelper,
array $indexedBackendModels = [],
array $forbiddenChildrenAttributes = [],
ScopeConfigInterface $scopeConfig = null
ScopeConfigInterface $scopeConfig = null,
ResolverInterface $localeResolver = null,
TranslateInterface $translator = null,
State $appState = null,
AreaList $areaList = null
) {
parent::__construct($resourceModel, $fieldFactory, $attributeHelper, $indexedBackendModels);

$this->scopeConfig = $scopeConfig;
$this->forbiddenChildrenAttributes = array_values($forbiddenChildrenAttributes);
$this->localeResolver = $localeResolver ?? ObjectManager::getInstance()->get(ResolverInterface::class);
$this->translator = $translator ?? ObjectManager::getInstance()->get(TranslateInterface::class);
$this->appState = $appState ?? ObjectManager::getInstance()->get(State::class);
$this->areaList = $areaList ?? ObjectManager::getInstance()->get(AreaList::class);
}

/**
* {@inheritdoc}
*/
public function addData($storeId, array $indexData)
{
// load store translation for static attribute options
$this->localeResolver->emulate($storeId);
$this->translator->setLocale($this->localeResolver->getLocale())->loadData(null, true);
// Translate area part may not be loaded :
$area = $this->areaList->getArea($this->appState->getAreaCode());
$area->load(\Magento\Framework\App\Area::PART_TRANSLATE);

$productIds = array_keys($indexData);
$indexData = $this->addAttributeData($storeId, $productIds, $indexData);

Expand Down Expand Up @@ -114,6 +157,10 @@ public function addData($storeId, array $indexData)
}
}

// reinitialize translation
$this->localeResolver->revert();
$this->translator->setLocale($this->localeResolver->getLocale())->loadData(null, true);

return $this->filterCompositeProducts($indexData);
}

Expand Down
Loading