From 812b535011ced0c5e223b44839081a1e06962c18 Mon Sep 17 00:00:00 2001 From: Vincent ENJALBERT Date: Fri, 15 Dec 2023 11:47:48 +0100 Subject: [PATCH 1/6] #3117 - Fix attribute option translation while reindexing --- .../Model/Category/Indexer/Fulltext.php | 29 ++++++++++++++++- .../Product/Indexer/Fulltext/Action/Full.php | 31 +++++++++++++++++-- 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/module-elasticsuite-catalog/Model/Category/Indexer/Fulltext.php b/src/module-elasticsuite-catalog/Model/Category/Indexer/Fulltext.php index 13b182bc2..1449874ff 100644 --- a/src/module-elasticsuite-catalog/Model/Category/Indexer/Fulltext.php +++ b/src/module-elasticsuite-catalog/Model/Category/Indexer/Fulltext.php @@ -17,6 +17,9 @@ use Magento\Store\Model\StoreManagerInterface; use Smile\ElasticsuiteCatalog\Model\Category\Indexer\Fulltext\Action\Full; use Magento\Framework\Indexer\SaveHandler\IndexerInterface; +use Magento\Framework\Locale\ResolverInterface; +use Magento\Framework\TranslateInterface; +use Magento\Framework\App\ObjectManager; /** * Categories fulltext indexer @@ -52,22 +55,37 @@ class Fulltext implements \Magento\Framework\Indexer\ActionInterface, \Magento\F */ private $fullAction; + /** + * @var ResolverInterface + */ + private $localeResolver; + + /** + * @var TranslateInterface + */ + private $translator; + /** * @param Full $fullAction The full index action * @param IndexerInterface $indexerHandler The index handler * @param StoreManagerInterface $storeManager The Store Manager * @param DimensionFactory $dimensionFactory The dimension factory + * @param ResolverInterface $localeResolver The locale resolver */ public function __construct( Full $fullAction, IndexerInterface $indexerHandler, StoreManagerInterface $storeManager, - DimensionFactory $dimensionFactory + DimensionFactory $dimensionFactory, + ResolverInterface $localeResolver = null, + TranslateInterface $translator = null ) { $this->fullAction = $fullAction; $this->indexerHandler = $indexerHandler; $this->storeManager = $storeManager; $this->dimensionFactory = $dimensionFactory; + $this->localeResolver = $localeResolver ?? ObjectManager::getInstance()->get(ResolverInterface::class); + $this->translator = $translator ?? ObjectManager::getInstance()->get(TranslateInterface::class); } /** @@ -82,10 +100,19 @@ public function execute($ids) $storeIds = array_keys($this->storeManager->getStores()); foreach ($storeIds as $storeId) { + // load store translation for static attribute options + $this->localeResolver->emulate($storeId); + $this->translator->setLocale($this->localeResolver->getLocale())->loadData(null, true); + $dimension = $this->dimensionFactory->create(['name' => 'scope', 'value' => $storeId]); $this->indexerHandler->deleteIndex([$dimension], new ExtendedArray($ids)); $this->indexerHandler->saveIndex([$dimension], $this->fullAction->rebuildStoreIndex($storeId, $ids)); + + $this->localeResolver->revert(); } + + // reinitialize translation + $this->translator->setLocale($this->localeResolver->getLocale())->loadData(null, true); } /** diff --git a/src/module-elasticsuite-catalog/Model/Product/Indexer/Fulltext/Action/Full.php b/src/module-elasticsuite-catalog/Model/Product/Indexer/Fulltext/Action/Full.php index 17b394e50..12c5aa8ab 100644 --- a/src/module-elasticsuite-catalog/Model/Product/Indexer/Fulltext/Action/Full.php +++ b/src/module-elasticsuite-catalog/Model/Product/Indexer/Fulltext/Action/Full.php @@ -15,6 +15,9 @@ namespace Smile\ElasticsuiteCatalog\Model\Product\Indexer\Fulltext\Action; use Smile\ElasticsuiteCatalog\Model\ResourceModel\Product\Indexer\Fulltext\Action\Full as ResourceModel; +use Magento\Framework\Locale\ResolverInterface; +use Magento\Framework\TranslateInterface; +use Magento\Framework\App\ObjectManager; /** * Elasticsearch product full indexer. @@ -25,19 +28,35 @@ */ class Full { + /** * @var \Smile\ElasticsuiteCatalog\Model\ResourceModel\Product\Indexer\Fulltext\Action\Full */ private $resourceModel; + /** + * @var ResolverInterface + */ + private $localeResolver; + + /** + * @var TranslateInterface + */ + private $translator; + /** * Constructor. * * @param ResourceModel $resourceModel Indexer resource model. */ - public function __construct(ResourceModel $resourceModel) - { + public function __construct( + ResourceModel $resourceModel, + ResolverInterface $localeResolver = null, + TranslateInterface $translator = null + ) { $this->resourceModel = $resourceModel; + $this->localeResolver = $localeResolver ?? ObjectManager::getInstance()->get(ResolverInterface::class); + $this->translator = $translator ?? ObjectManager::getInstance()->get(TranslateInterface::class); } /** @@ -51,6 +70,10 @@ public function __construct(ResourceModel $resourceModel) */ public function rebuildStoreIndex($storeId, $productIds = null) { + // load store translation for static attribute options + $this->localeResolver->emulate($storeId); + $this->translator->setLocale($this->localeResolver->getLocale())->loadData(null, true); + $productId = 0; // Magento is only sending children ids here. Ensure to reindex also the parents product ids, if any. @@ -68,6 +91,10 @@ public function rebuildStoreIndex($storeId, $productIds = null) yield $productId => $productData; } } while (!empty($products)); + + // reinitialize translation + $this->localeResolver->revert(); + $this->translator->setLocale($this->localeResolver->getLocale())->loadData(null, true); } /** From bcf8b538b75bdd176d55b8e7310634cc7e88c310 Mon Sep 17 00:00:00 2001 From: Vincent ENJALBERT Date: Fri, 15 Dec 2023 11:53:48 +0100 Subject: [PATCH 2/6] #3117 - Fix attribute option translation while reindexing (Fix php doc) --- .../Model/Category/Indexer/Fulltext.php | 1 + .../Model/Product/Indexer/Fulltext/Action/Full.php | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/module-elasticsuite-catalog/Model/Category/Indexer/Fulltext.php b/src/module-elasticsuite-catalog/Model/Category/Indexer/Fulltext.php index 1449874ff..fb1a525ec 100644 --- a/src/module-elasticsuite-catalog/Model/Category/Indexer/Fulltext.php +++ b/src/module-elasticsuite-catalog/Model/Category/Indexer/Fulltext.php @@ -71,6 +71,7 @@ class Fulltext implements \Magento\Framework\Indexer\ActionInterface, \Magento\F * @param StoreManagerInterface $storeManager The Store Manager * @param DimensionFactory $dimensionFactory The dimension factory * @param ResolverInterface $localeResolver The locale resolver + * @param TranslateInterface $translator The translator handler */ public function __construct( Full $fullAction, diff --git a/src/module-elasticsuite-catalog/Model/Product/Indexer/Fulltext/Action/Full.php b/src/module-elasticsuite-catalog/Model/Product/Indexer/Fulltext/Action/Full.php index 12c5aa8ab..cd573585e 100644 --- a/src/module-elasticsuite-catalog/Model/Product/Indexer/Fulltext/Action/Full.php +++ b/src/module-elasticsuite-catalog/Model/Product/Indexer/Fulltext/Action/Full.php @@ -47,7 +47,9 @@ class Full /** * Constructor. * - * @param ResourceModel $resourceModel Indexer resource model. + * @param ResourceModel $resourceModel Indexer resource model. + * @param ResolverInterface $localeResolver The locale resolver + * @param TranslateInterface $translator The translator handler */ public function __construct( ResourceModel $resourceModel, From 2c1970857ac5af0510c2684abbdc10770c854e3a Mon Sep 17 00:00:00 2001 From: Vincent ENJALBERT Date: Wed, 10 Jan 2024 16:21:46 +0100 Subject: [PATCH 3/6] #3117 - Fix attribute option translation while reindexing (in category/executeFull) --- .../Model/Category/Indexer/Fulltext.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/module-elasticsuite-catalog/Model/Category/Indexer/Fulltext.php b/src/module-elasticsuite-catalog/Model/Category/Indexer/Fulltext.php index fb1a525ec..ea35733e4 100644 --- a/src/module-elasticsuite-catalog/Model/Category/Indexer/Fulltext.php +++ b/src/module-elasticsuite-catalog/Model/Category/Indexer/Fulltext.php @@ -126,10 +126,19 @@ public function executeFull() $storeIds = array_keys($this->storeManager->getStores()); foreach ($storeIds as $storeId) { + // load store translation for static attribute options + $this->localeResolver->emulate($storeId); + $this->translator->setLocale($this->localeResolver->getLocale())->loadData(null, true); + $dimension = $this->dimensionFactory->create(['name' => 'scope', 'value' => $storeId]); $this->indexerHandler->cleanIndex([$dimension]); $this->indexerHandler->saveIndex([$dimension], $this->fullAction->rebuildStoreIndex($storeId)); + + $this->localeResolver->revert(); } + + // reinitialize translation + $this->translator->setLocale($this->localeResolver->getLocale())->loadData(null, true); } /** From 5bbf80dedc3c9d2297d8ac334edf53956fb5d679 Mon Sep 17 00:00:00 2001 From: Vincent ENJALBERT Date: Thu, 11 Jan 2024 13:55:49 +0100 Subject: [PATCH 4/6] fix --- .../Model/Category/Indexer/Fulltext.php | 39 +-------------- .../Product/Indexer/Fulltext/Action/Full.php | 29 +---------- .../Fulltext/Datasource/AttributeData.php | 49 ++++++++++++++++++- 3 files changed, 50 insertions(+), 67 deletions(-) diff --git a/src/module-elasticsuite-catalog/Model/Category/Indexer/Fulltext.php b/src/module-elasticsuite-catalog/Model/Category/Indexer/Fulltext.php index ea35733e4..13b182bc2 100644 --- a/src/module-elasticsuite-catalog/Model/Category/Indexer/Fulltext.php +++ b/src/module-elasticsuite-catalog/Model/Category/Indexer/Fulltext.php @@ -17,9 +17,6 @@ use Magento\Store\Model\StoreManagerInterface; use Smile\ElasticsuiteCatalog\Model\Category\Indexer\Fulltext\Action\Full; use Magento\Framework\Indexer\SaveHandler\IndexerInterface; -use Magento\Framework\Locale\ResolverInterface; -use Magento\Framework\TranslateInterface; -use Magento\Framework\App\ObjectManager; /** * Categories fulltext indexer @@ -55,38 +52,22 @@ class Fulltext implements \Magento\Framework\Indexer\ActionInterface, \Magento\F */ private $fullAction; - /** - * @var ResolverInterface - */ - private $localeResolver; - - /** - * @var TranslateInterface - */ - private $translator; - /** * @param Full $fullAction The full index action * @param IndexerInterface $indexerHandler The index handler * @param StoreManagerInterface $storeManager The Store Manager * @param DimensionFactory $dimensionFactory The dimension factory - * @param ResolverInterface $localeResolver The locale resolver - * @param TranslateInterface $translator The translator handler */ public function __construct( Full $fullAction, IndexerInterface $indexerHandler, StoreManagerInterface $storeManager, - DimensionFactory $dimensionFactory, - ResolverInterface $localeResolver = null, - TranslateInterface $translator = null + DimensionFactory $dimensionFactory ) { $this->fullAction = $fullAction; $this->indexerHandler = $indexerHandler; $this->storeManager = $storeManager; $this->dimensionFactory = $dimensionFactory; - $this->localeResolver = $localeResolver ?? ObjectManager::getInstance()->get(ResolverInterface::class); - $this->translator = $translator ?? ObjectManager::getInstance()->get(TranslateInterface::class); } /** @@ -101,19 +82,10 @@ public function execute($ids) $storeIds = array_keys($this->storeManager->getStores()); foreach ($storeIds as $storeId) { - // load store translation for static attribute options - $this->localeResolver->emulate($storeId); - $this->translator->setLocale($this->localeResolver->getLocale())->loadData(null, true); - $dimension = $this->dimensionFactory->create(['name' => 'scope', 'value' => $storeId]); $this->indexerHandler->deleteIndex([$dimension], new ExtendedArray($ids)); $this->indexerHandler->saveIndex([$dimension], $this->fullAction->rebuildStoreIndex($storeId, $ids)); - - $this->localeResolver->revert(); } - - // reinitialize translation - $this->translator->setLocale($this->localeResolver->getLocale())->loadData(null, true); } /** @@ -126,19 +98,10 @@ public function executeFull() $storeIds = array_keys($this->storeManager->getStores()); foreach ($storeIds as $storeId) { - // load store translation for static attribute options - $this->localeResolver->emulate($storeId); - $this->translator->setLocale($this->localeResolver->getLocale())->loadData(null, true); - $dimension = $this->dimensionFactory->create(['name' => 'scope', 'value' => $storeId]); $this->indexerHandler->cleanIndex([$dimension]); $this->indexerHandler->saveIndex([$dimension], $this->fullAction->rebuildStoreIndex($storeId)); - - $this->localeResolver->revert(); } - - // reinitialize translation - $this->translator->setLocale($this->localeResolver->getLocale())->loadData(null, true); } /** diff --git a/src/module-elasticsuite-catalog/Model/Product/Indexer/Fulltext/Action/Full.php b/src/module-elasticsuite-catalog/Model/Product/Indexer/Fulltext/Action/Full.php index cd573585e..c05e2d229 100644 --- a/src/module-elasticsuite-catalog/Model/Product/Indexer/Fulltext/Action/Full.php +++ b/src/module-elasticsuite-catalog/Model/Product/Indexer/Fulltext/Action/Full.php @@ -15,9 +15,6 @@ namespace Smile\ElasticsuiteCatalog\Model\Product\Indexer\Fulltext\Action; use Smile\ElasticsuiteCatalog\Model\ResourceModel\Product\Indexer\Fulltext\Action\Full as ResourceModel; -use Magento\Framework\Locale\ResolverInterface; -use Magento\Framework\TranslateInterface; -use Magento\Framework\App\ObjectManager; /** * Elasticsearch product full indexer. @@ -34,31 +31,15 @@ class Full */ private $resourceModel; - /** - * @var ResolverInterface - */ - private $localeResolver; - - /** - * @var TranslateInterface - */ - private $translator; - /** * Constructor. * * @param ResourceModel $resourceModel Indexer resource model. - * @param ResolverInterface $localeResolver The locale resolver - * @param TranslateInterface $translator The translator handler */ public function __construct( - ResourceModel $resourceModel, - ResolverInterface $localeResolver = null, - TranslateInterface $translator = null + ResourceModel $resourceModel ) { $this->resourceModel = $resourceModel; - $this->localeResolver = $localeResolver ?? ObjectManager::getInstance()->get(ResolverInterface::class); - $this->translator = $translator ?? ObjectManager::getInstance()->get(TranslateInterface::class); } /** @@ -72,10 +53,6 @@ public function __construct( */ public function rebuildStoreIndex($storeId, $productIds = null) { - // load store translation for static attribute options - $this->localeResolver->emulate($storeId); - $this->translator->setLocale($this->localeResolver->getLocale())->loadData(null, true); - $productId = 0; // Magento is only sending children ids here. Ensure to reindex also the parents product ids, if any. @@ -93,10 +70,6 @@ public function rebuildStoreIndex($storeId, $productIds = null) yield $productId => $productData; } } while (!empty($products)); - - // reinitialize translation - $this->localeResolver->revert(); - $this->translator->setLocale($this->localeResolver->getLocale())->loadData(null, true); } /** diff --git a/src/module-elasticsuite-catalog/Model/Product/Indexer/Fulltext/Datasource/AttributeData.php b/src/module-elasticsuite-catalog/Model/Product/Indexer/Fulltext/Datasource/AttributeData.php index c9f8e025f..1fda87b5a 100644 --- a/src/module-elasticsuite-catalog/Model/Product/Indexer/Fulltext/Datasource/AttributeData.php +++ b/src/module-elasticsuite-catalog/Model/Product/Indexer/Fulltext/Datasource/AttributeData.php @@ -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. @@ -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 * @@ -65,6 +89,10 @@ 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, @@ -72,12 +100,20 @@ public function __construct( 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); } /** @@ -85,6 +121,13 @@ public function __construct( */ 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); @@ -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); } From aeac805857bbfc5a7360c1ca656a5b9544370796 Mon Sep 17 00:00:00 2001 From: Vincent ENJALBERT Date: Thu, 11 Jan 2024 13:56:27 +0100 Subject: [PATCH 5/6] fix --- .../Model/Product/Indexer/Fulltext/Action/Full.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/module-elasticsuite-catalog/Model/Product/Indexer/Fulltext/Action/Full.php b/src/module-elasticsuite-catalog/Model/Product/Indexer/Fulltext/Action/Full.php index c05e2d229..62edfaa64 100644 --- a/src/module-elasticsuite-catalog/Model/Product/Indexer/Fulltext/Action/Full.php +++ b/src/module-elasticsuite-catalog/Model/Product/Indexer/Fulltext/Action/Full.php @@ -34,11 +34,10 @@ class Full /** * Constructor. * - * @param ResourceModel $resourceModel Indexer resource model. + * @param ResourceModel $resourceModel Indexer resource model. */ - public function __construct( - ResourceModel $resourceModel - ) { + public function __construct(ResourceModel $resourceModel) + { $this->resourceModel = $resourceModel; } From 10df720b7f0892c6769d36703eaed126011d9b69 Mon Sep 17 00:00:00 2001 From: Vincent ENJALBERT Date: Thu, 11 Jan 2024 13:56:37 +0100 Subject: [PATCH 6/6] fix --- .../Model/Product/Indexer/Fulltext/Action/Full.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/module-elasticsuite-catalog/Model/Product/Indexer/Fulltext/Action/Full.php b/src/module-elasticsuite-catalog/Model/Product/Indexer/Fulltext/Action/Full.php index 62edfaa64..17b394e50 100644 --- a/src/module-elasticsuite-catalog/Model/Product/Indexer/Fulltext/Action/Full.php +++ b/src/module-elasticsuite-catalog/Model/Product/Indexer/Fulltext/Action/Full.php @@ -25,7 +25,6 @@ */ class Full { - /** * @var \Smile\ElasticsuiteCatalog\Model\ResourceModel\Product\Indexer\Fulltext\Action\Full */