Skip to content

Commit 567045b

Browse files
committed
Merge remote-tracking branch 'origin/MC-36768' into 2.4-develop-pr100
2 parents 8973198 + 320fae7 commit 567045b

File tree

21 files changed

+758
-594
lines changed

21 files changed

+758
-594
lines changed

app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Action/DataProvider.php

Lines changed: 65 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,24 @@
66
namespace Magento\CatalogSearch\Model\Indexer\Fulltext\Action;
77

88
use Magento\Catalog\Api\Data\ProductInterface;
9+
use Magento\Catalog\Model\Product;
910
use Magento\Catalog\Model\Product\Attribute\Source\Status;
11+
use Magento\Catalog\Model\Product\Type;
12+
use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory;
13+
use Magento\CatalogSearch\Model\ResourceModel\EngineInterface;
14+
use Magento\CatalogSearch\Model\ResourceModel\EngineProvider;
15+
use Magento\Eav\Model\Config;
16+
use Magento\Eav\Model\Entity\Attribute;
1017
use Magento\Framework\App\ResourceConnection;
18+
use Magento\Framework\DataObject;
19+
use Magento\Framework\DB\Adapter\AdapterInterface;
1120
use Magento\Framework\DB\Select;
21+
use Magento\Framework\EntityManager\EntityMetadata;
22+
use Magento\Framework\EntityManager\MetadataPool;
23+
use Magento\Framework\Event\ManagerInterface;
1224
use Magento\Store\Model\Store;
25+
use Magento\Store\Model\StoreManagerInterface;
26+
use Zend_Db;
1327

1428
/**
1529
* Catalog search full test search data provider.
@@ -24,7 +38,7 @@ class DataProvider
2438
/**
2539
* Searchable attributes cache
2640
*
27-
* @var \Magento\Eav\Model\Entity\Attribute[]
41+
* @var Attribute[]
2842
*/
2943
private $searchableAttributes;
3044

@@ -50,40 +64,40 @@ class DataProvider
5064
private $productEmulators = [];
5165

5266
/**
53-
* @var \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory
67+
* @var CollectionFactory
5468
*/
5569
private $productAttributeCollectionFactory;
5670

5771
/**
5872
* Eav config
5973
*
60-
* @var \Magento\Eav\Model\Config
74+
* @var Config
6175
*/
6276
private $eavConfig;
6377

6478
/**
6579
* Catalog product type
6680
*
67-
* @var \Magento\Catalog\Model\Product\Type
81+
* @var Type
6882
*/
6983
private $catalogProductType;
7084

7185
/**
7286
* Core event manager proxy
7387
*
74-
* @var \Magento\Framework\Event\ManagerInterface
88+
* @var ManagerInterface
7589
*/
7690
private $eventManager;
7791

7892
/**
7993
* Store manager
8094
*
81-
* @var \Magento\Store\Model\StoreManagerInterface
95+
* @var StoreManagerInterface
8296
*/
8397
private $storeManager;
8498

8599
/**
86-
* @var \Magento\CatalogSearch\Model\ResourceModel\EngineInterface
100+
* @var EngineInterface
87101
*/
88102
private $engine;
89103

@@ -93,12 +107,12 @@ class DataProvider
93107
private $resource;
94108

95109
/**
96-
* @var \Magento\Framework\DB\Adapter\AdapterInterface
110+
* @var AdapterInterface
97111
*/
98112
private $connection;
99113

100114
/**
101-
* @var \Magento\Framework\EntityManager\EntityMetadata
115+
* @var EntityMetadata
102116
*/
103117
private $metadata;
104118

@@ -126,24 +140,24 @@ class DataProvider
126140

127141
/**
128142
* @param ResourceConnection $resource
129-
* @param \Magento\Catalog\Model\Product\Type $catalogProductType
130-
* @param \Magento\Eav\Model\Config $eavConfig
131-
* @param \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $prodAttributeCollectionFactory
132-
* @param \Magento\CatalogSearch\Model\ResourceModel\EngineProvider $engineProvider
133-
* @param \Magento\Framework\Event\ManagerInterface $eventManager
134-
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
135-
* @param \Magento\Framework\EntityManager\MetadataPool $metadataPool
143+
* @param Type $catalogProductType
144+
* @param Config $eavConfig
145+
* @param CollectionFactory $prodAttributeCollectionFactory
146+
* @param EngineProvider $engineProvider
147+
* @param ManagerInterface $eventManager
148+
* @param StoreManagerInterface $storeManager
149+
* @param MetadataPool $metadataPool
136150
* @param int $antiGapMultiplier
137151
*/
138152
public function __construct(
139153
ResourceConnection $resource,
140-
\Magento\Catalog\Model\Product\Type $catalogProductType,
141-
\Magento\Eav\Model\Config $eavConfig,
142-
\Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $prodAttributeCollectionFactory,
143-
\Magento\CatalogSearch\Model\ResourceModel\EngineProvider $engineProvider,
144-
\Magento\Framework\Event\ManagerInterface $eventManager,
145-
\Magento\Store\Model\StoreManagerInterface $storeManager,
146-
\Magento\Framework\EntityManager\MetadataPool $metadataPool,
154+
Type $catalogProductType,
155+
Config $eavConfig,
156+
CollectionFactory $prodAttributeCollectionFactory,
157+
EngineProvider $engineProvider,
158+
ManagerInterface $eventManager,
159+
StoreManagerInterface $storeManager,
160+
MetadataPool $metadataPool,
147161
int $antiGapMultiplier = 5
148162
) {
149163
$this->resource = $resource;
@@ -224,7 +238,7 @@ private function getSelectForSearchableProducts(
224238
$batch
225239
) {
226240
$websiteId = (int)$this->storeManager->getStore($storeId)->getWebsiteId();
227-
$lastProductId = (int) $lastProductId;
241+
$lastProductId = (int)$lastProductId;
228242

229243
$select = $this->connection->select()
230244
->useStraightJoin(true)
@@ -242,7 +256,7 @@ private function getSelectForSearchableProducts(
242256
$this->joinAttribute($select, 'status', $storeId, [Status::STATUS_ENABLED]);
243257

244258
if ($productIds !== null) {
245-
$select->where('e.entity_id IN (?)', $productIds, \Zend_Db::INT_TYPE);
259+
$select->where('e.entity_id IN (?)', $productIds, Zend_Db::INT_TYPE);
246260
}
247261
$select->where('e.entity_id > ?', $lastProductId);
248262
$select->order('e.entity_id');
@@ -308,14 +322,17 @@ private function joinAttribute(Select $select, $attributeCode, $storeId, array $
308322
*/
309323
public function getSearchableAttributes($backendType = null)
310324
{
325+
/** TODO: Remove this block in the next minor release and add a new public method instead */
326+
if ($this->eavConfig->getEntityType(Product::ENTITY)->getNeedRefreshSearchAttributesList()) {
327+
$this->clearSearchableAttributesList();
328+
}
311329
if (null === $this->searchableAttributes) {
312330
$this->searchableAttributes = [];
313331

314-
/** @var \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection $productAttributes */
315332
$productAttributes = $this->productAttributeCollectionFactory->create();
316333
$productAttributes->addToIndexFilter(true);
317334

318-
/** @var \Magento\Eav\Model\Entity\Attribute[] $attributes */
335+
/** @var Attribute[] $attributes */
319336
$attributes = $productAttributes->getItems();
320337

321338
/** @deprecated */
@@ -329,7 +346,7 @@ public function getSearchableAttributes($backendType = null)
329346
['engine' => $this->engine, 'attributes' => $attributes]
330347
);
331348

332-
$entity = $this->eavConfig->getEntityType(\Magento\Catalog\Model\Product::ENTITY)->getEntity();
349+
$entity = $this->eavConfig->getEntityType(Product::ENTITY)->getEntity();
333350

334351
foreach ($attributes as $attribute) {
335352
$attribute->setEntity($entity);
@@ -355,6 +372,18 @@ public function getSearchableAttributes($backendType = null)
355372
return $this->searchableAttributes;
356373
}
357374

375+
/**
376+
* Remove searchable attributes list.
377+
*
378+
* @return void
379+
*/
380+
private function clearSearchableAttributesList(): void
381+
{
382+
$this->searchableAttributes = null;
383+
$this->searchableAttributesByBackendType = [];
384+
$this->eavConfig->getEntityType(Product::ENTITY)->unsNeedRefreshSearchAttributesList();
385+
}
386+
358387
/**
359388
* Retrieve searchable attribute by Id or code
360389
*
@@ -369,7 +398,7 @@ public function getSearchableAttribute($attribute)
369398
return $attributes[$attribute];
370399
}
371400

372-
return $this->eavConfig->getAttribute(\Magento\Catalog\Model\Product::ENTITY, $attribute);
401+
return $this->eavConfig->getAttribute(Product::ENTITY, $attribute);
373402
}
374403

375404
/**
@@ -386,6 +415,7 @@ private function unifyField($field, $backendType = 'varchar')
386415
} else {
387416
$expr = $field;
388417
}
418+
389419
return $expr;
390420
}
391421

@@ -411,7 +441,7 @@ public function getProductAttributes($storeId, array $productIds, array $attribu
411441
)->where(
412442
'cpe.entity_id IN (?)',
413443
$productIds,
414-
\Zend_Db::INT_TYPE
444+
Zend_Db::INT_TYPE
415445
)
416446
);
417447
foreach ($attributeTypes as $backendType => $attributeIds) {
@@ -479,6 +509,7 @@ private function getProductTypeInstance($typeId)
479509

480510
$this->productTypes[$typeId] = $this->catalogProductType->factory($productEmulator);
481511
}
512+
482513
return $this->productTypes[$typeId];
483514
}
484515

@@ -513,6 +544,7 @@ public function getProductChildIds($productId, $typeId)
513544
if ($relation->getWhere() !== null) {
514545
$select->where($relation->getWhere());
515546
}
547+
516548
return $this->connection->fetchCol($select);
517549
}
518550

@@ -528,10 +560,11 @@ public function getProductChildIds($productId, $typeId)
528560
private function getProductEmulator($typeId)
529561
{
530562
if (!isset($this->productEmulators[$typeId])) {
531-
$productEmulator = new \Magento\Framework\DataObject();
563+
$productEmulator = new DataObject();
532564
$productEmulator->setTypeId($typeId);
533565
$this->productEmulators[$typeId] = $productEmulator;
534566
}
567+
535568
return $this->productEmulators[$typeId];
536569
}
537570

@@ -660,6 +693,7 @@ function ($value) {
660693
$attributeOptionValue .= $this->attributeOptions[$optionKey][$attrValueId] . ' ';
661694
}
662695
}
696+
663697
return empty($attributeOptionValue) ? null : trim($attributeOptionValue);
664698
}
665699

app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Attribute.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
*/
66
namespace Magento\CatalogSearch\Model\Indexer\Fulltext\Plugin;
77

8+
use Magento\Catalog\Model\Product;
89
use Magento\CatalogSearch\Model\Indexer\Fulltext;
10+
use Magento\CatalogSearch\Model\Indexer\Fulltext\Action\DataProvider;
911
use Magento\Framework\Model\AbstractModel;
1012
use Magento\Catalog\Model\ResourceModel\Attribute as AttributeResourceModel;
1113
use Magento\Framework\Search\Request\Config;
1214
use Magento\Framework\Indexer\IndexerRegistry;
1315
use Magento\Catalog\Api\Data\EavAttributeInterface;
16+
use Magento\Eav\Model\Config as EavConfig;
1417

1518
/**
1619
* Catalog search indexer plugin for catalog attribute.
@@ -37,16 +40,24 @@ class Attribute extends AbstractPlugin
3740
*/
3841
private $saveIsNew;
3942

43+
/**
44+
* @var EavConfig
45+
*/
46+
private $eavConfig;
47+
4048
/**
4149
* @param IndexerRegistry $indexerRegistry
4250
* @param Config $config
51+
* @param EavConfig $eavConfig
4352
*/
4453
public function __construct(
4554
IndexerRegistry $indexerRegistry,
46-
Config $config
55+
Config $config,
56+
EavConfig $eavConfig
4757
) {
4858
parent::__construct($indexerRegistry);
4959
$this->config = $config;
60+
$this->eavConfig = $eavConfig;
5061
}
5162

5263
/**
@@ -84,6 +95,11 @@ public function afterSave(
8495
}
8596
if ($this->saveIsNew || $this->saveNeedInvalidation) {
8697
$this->config->reset();
98+
/**
99+
* TODO: Remove this in next minor release and use public method instead.
100+
* @see DataProvider::getSearchableAttributes
101+
*/
102+
$this->eavConfig->getEntityType(Product::ENTITY)->setNeedRefreshSearchAttributesList(true);
87103
}
88104

89105
return $result;

0 commit comments

Comments
 (0)