Skip to content

Commit e713317

Browse files
authored
Merge branch '2.4-develop' into add-mftf-for-admin-analytics
2 parents db43cb8 + da50b9b commit e713317

File tree

34 files changed

+1665
-331
lines changed

34 files changed

+1665
-331
lines changed

app/code/Magento/Catalog/view/adminhtml/templates/catalog/product/composite/configure.phtml

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
*/
66

77
/** @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer */
8+
$blockId = $block->getId();
89
?>
9-
<div id="product_composite_configure" class="product-configure-popup">
10+
<div id="product_composite_configure"
11+
class="product-configure-popup product-configure-popup-<?= $block->escapeHtmlAttr($blockId) ?>">
1012
<iframe name="product_composite_configure_iframe" id="product_composite_configure_iframe"></iframe>
1113
<?= /* @noEscape */ $secureRenderer->renderEventListenerAsTag(
1214
'onload',
@@ -31,40 +33,45 @@
3133
</form>
3234
<?= /* @noEscape */ $secureRenderer->renderEventListenerAsTag(
3335
'onsubmit',
34-
"productConfigure.onConfirmBtn();event.preventDefault()",
36+
'productConfigure.onConfirmBtn();event.preventDefault()',
3537
'.product_composite_configure_form:last-of-type'
3638
) ?>
3739

3840
<div id="product_composite_configure_confirmed" class="product_composite_configure_confirmed"></div>
3941

4042
<?php $scriptString = <<<script
41-
prodCompConfIframe = document.querySelector("iframe[name='product_composite_configure_iframe']:last-of-type");
43+
prodCompConfIframe = document.querySelector(
44+
".product-configure-popup-$blockId iframe[name='product_composite_configure_iframe']"
45+
);
4246
prodCompConfIframe.style.width = 0;
4347
prodCompConfIframe.style.height = 0;
4448
prodCompConfIframe.style.border = "0px solid #fff";
4549
prodCompConfIframe.style.position = "absolute";
4650
prodCompConfIframe.style.top = "-1000px";
4751
prodCompConfIframe.style.left = "-1000px";
48-
prodCompConfMessages = document.querySelectorAll(".product_composite_configure_messages");
49-
for (var i = 0; i < prodCompConfMessages.length; i++) {
50-
prodCompConfMessages[i].style.display = "none";
51-
}
52-
prodCompConfFormAdd = document.querySelectorAll(".product_composite_configure_form_additional");
53-
for (var i = 0; i < prodCompConfFormAdd.length; i++) {
54-
prodCompConfFormAdd[i].style.display = "none";
55-
}
56-
prodCompConfFormConf = document.querySelectorAll(".product_composite_configure_form_confirmed");
57-
for (var i = 0; i < prodCompConfFormConf.length; i++) {
58-
prodCompConfFormConf[i].style.display = "none";
59-
}
60-
prodCompConfConf = document.querySelectorAll(".product_composite_configure_confirmed");
61-
for (var i = 0; i < prodCompConfConf.length; i++) {
62-
prodCompConfConf[i].style.display = "none";
63-
}
64-
prodConfPopup = document.querySelectorAll(".product-configure-popup");
65-
for (var i = 0; i < prodConfPopup.length; i++) {
66-
prodConfPopup[i].style.display = "none";
67-
}
52+
53+
prodCompConfMessages = document.querySelector(
54+
".product-configure-popup-$blockId .product_composite_configure_messages"
55+
);
56+
prodCompConfMessages.style.display = "none";
57+
58+
prodCompConfFormAdd = document.querySelector(
59+
".product-configure-popup-$blockId .product_composite_configure_form_additional"
60+
);
61+
prodCompConfFormAdd.style.display = "none";
62+
63+
prodCompConfFormConf = document.querySelector(
64+
".product-configure-popup-$blockId .product_composite_configure_form_confirmed"
65+
);
66+
prodCompConfFormConf.style.display = "none";
67+
68+
prodCompConfConf = document.querySelector(
69+
".product-configure-popup-$blockId .product_composite_configure_confirmed"
70+
);
71+
prodCompConfConf.style.display = "none";
72+
73+
prodConfPopup = document.querySelector(".product-configure-popup-$blockId");
74+
prodConfPopup.style.display = "none";
6875
6976
require([
7077
"jquery",

app/code/Magento/Eav/Model/Config.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,7 @@ protected function _createAttribute($entityType, $attributeData)
839839
}
840840
/** @var AbstractAttribute $attribute */
841841
$attribute = $this->createAttribute($model)->setData($attributeData);
842+
$attribute->setOrigData('entity_type_id', $attribute->getEntityTypeId());
842843
$this->_addAttributeReference(
843844
$attributeData['attribute_id'],
844845
$code,

app/code/Magento/Eav/Test/Unit/Model/ConfigTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,13 @@ public function testGetAttributes($cacheEnabled)
223223
->method('getData')
224224
->willReturn([$attributeData]);
225225
$entityAttributeMock = $this->getMockBuilder(Attribute::class)
226-
->setMethods(['setData', 'load', 'toArray'])
226+
->setMethods(['setData', 'setOrigData', 'load', 'toArray'])
227227
->disableOriginalConstructor()
228228
->getMock();
229229
$entityAttributeMock->method('setData')
230230
->willReturnSelf();
231+
$entityAttributeMock->method('setOrigData')
232+
->willReturn($attributeData);
231233
$entityAttributeMock->method('load')
232234
->willReturnSelf();
233235
$entityAttributeMock->method('toArray')

app/code/Magento/Elasticsearch/Elasticsearch5/Model/Client/Elasticsearch.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,23 @@ public function createIndex($index, $settings)
166166
);
167167
}
168168

169+
/**
170+
* Add/update an Elasticsearch index settings.
171+
*
172+
* @param string $index
173+
* @param array $settings
174+
* @return void
175+
*/
176+
public function putIndexSettings(string $index, array $settings): void
177+
{
178+
$this->getClient()->indices()->putSettings(
179+
[
180+
'index' => $index,
181+
'body' => $settings,
182+
]
183+
);
184+
}
185+
169186
/**
170187
* Delete an Elasticsearch index.
171188
*
@@ -348,6 +365,17 @@ private function prepareFieldInfo($fieldInfo)
348365
return $fieldInfo;
349366
}
350367

368+
/**
369+
* Get mapping from Elasticsearch index.
370+
*
371+
* @param array $params
372+
* @return array
373+
*/
374+
public function getMapping(array $params): array
375+
{
376+
return $this->getClient()->indices()->getMapping($params);
377+
}
378+
351379
/**
352380
* Delete mapping in Elasticsearch index
353381
*

app/code/Magento/Elasticsearch/Model/Adapter/Elasticsearch.php

Lines changed: 132 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66

77
namespace Magento\Elasticsearch\Model\Adapter;
88

9+
use Magento\Catalog\Api\Data\ProductAttributeInterface;
10+
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
11+
use Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\StaticField;
12+
use Magento\Framework\App\ObjectManager;
13+
use Magento\Framework\Stdlib\ArrayManager;
14+
915
/**
1016
* Elasticsearch adapter
1117
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -69,6 +75,31 @@ class Elasticsearch
6975
*/
7076
private $batchDocumentDataMapper;
7177

78+
/**
79+
* @var array
80+
*/
81+
private $mappedAttributes = [];
82+
83+
/**
84+
* @var string[]
85+
*/
86+
private $indexByCode = [];
87+
88+
/**
89+
* @var ProductAttributeRepositoryInterface
90+
*/
91+
private $productAttributeRepository;
92+
93+
/**
94+
* @var StaticField
95+
*/
96+
private $staticFieldProvider;
97+
98+
/**
99+
* @var ArrayManager
100+
*/
101+
private $arrayManager;
102+
72103
/**
73104
* @param \Magento\Elasticsearch\SearchAdapter\ConnectionManager $connectionManager
74105
* @param FieldMapperInterface $fieldMapper
@@ -78,7 +109,11 @@ class Elasticsearch
78109
* @param Index\IndexNameResolver $indexNameResolver
79110
* @param BatchDataMapperInterface $batchDocumentDataMapper
80111
* @param array $options
112+
* @param ProductAttributeRepositoryInterface|null $productAttributeRepository
113+
* @param StaticField|null $staticFieldProvider
114+
* @param ArrayManager|null $arrayManager
81115
* @throws \Magento\Framework\Exception\LocalizedException
116+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
82117
*/
83118
public function __construct(
84119
\Magento\Elasticsearch\SearchAdapter\ConnectionManager $connectionManager,
@@ -88,7 +123,10 @@ public function __construct(
88123
\Psr\Log\LoggerInterface $logger,
89124
\Magento\Elasticsearch\Model\Adapter\Index\IndexNameResolver $indexNameResolver,
90125
BatchDataMapperInterface $batchDocumentDataMapper,
91-
$options = []
126+
$options = [],
127+
ProductAttributeRepositoryInterface $productAttributeRepository = null,
128+
StaticField $staticFieldProvider = null,
129+
ArrayManager $arrayManager = null
92130
) {
93131
$this->connectionManager = $connectionManager;
94132
$this->fieldMapper = $fieldMapper;
@@ -97,6 +135,12 @@ public function __construct(
97135
$this->logger = $logger;
98136
$this->indexNameResolver = $indexNameResolver;
99137
$this->batchDocumentDataMapper = $batchDocumentDataMapper;
138+
$this->productAttributeRepository = $productAttributeRepository ?:
139+
ObjectManager::getInstance()->get(ProductAttributeRepositoryInterface::class);
140+
$this->staticFieldProvider = $staticFieldProvider ?:
141+
ObjectManager::getInstance()->get(StaticField::class);
142+
$this->arrayManager = $arrayManager ?:
143+
ObjectManager::getInstance()->get(ArrayManager::class);
100144

101145
try {
102146
$this->client = $this->connectionManager->getConnection($options);
@@ -322,6 +366,93 @@ public function updateAlias($storeId, $mappedIndexerId)
322366
// remove obsolete index
323367
if ($oldIndex) {
324368
$this->client->deleteIndex($oldIndex);
369+
unset($this->indexByCode[$mappedIndexerId . '_' . $storeId]);
370+
}
371+
372+
return $this;
373+
}
374+
375+
/**
376+
* Update Elasticsearch mapping for index.
377+
*
378+
* @param int $storeId
379+
* @param string $mappedIndexerId
380+
* @param string $attributeCode
381+
* @return $this
382+
*/
383+
public function updateIndexMapping(int $storeId, string $mappedIndexerId, string $attributeCode): self
384+
{
385+
$indexName = $this->getIndexFromAlias($storeId, $mappedIndexerId);
386+
if (empty($indexName)) {
387+
return $this;
388+
}
389+
390+
$attribute = $this->productAttributeRepository->get($attributeCode);
391+
$newAttributeMapping = $this->staticFieldProvider->getField($attribute);
392+
$mappedAttributes = $this->getMappedAttributes($indexName);
393+
394+
$attrToUpdate = array_diff_key($newAttributeMapping, $mappedAttributes);
395+
if (!empty($attrToUpdate)) {
396+
$settings['index']['mapping']['total_fields']['limit'] = $this
397+
->getMappingTotalFieldsLimit(array_merge($mappedAttributes, $attrToUpdate));
398+
$this->client->putIndexSettings($indexName, ['settings' => $settings]);
399+
400+
$this->client->addFieldsMapping(
401+
$attrToUpdate,
402+
$indexName,
403+
$this->clientConfig->getEntityType()
404+
);
405+
$this->setMappedAttributes($indexName, $attrToUpdate);
406+
}
407+
408+
return $this;
409+
}
410+
411+
/**
412+
* Retrieve index definition from class.
413+
*
414+
* @param int $storeId
415+
* @param string $mappedIndexerId
416+
* @return string
417+
*/
418+
private function getIndexFromAlias(int $storeId, string $mappedIndexerId): string
419+
{
420+
$indexCode = $mappedIndexerId . '_' . $storeId;
421+
if (!isset($this->indexByCode[$indexCode])) {
422+
$this->indexByCode[$indexCode] = $this->indexNameResolver->getIndexFromAlias($storeId, $mappedIndexerId);
423+
}
424+
425+
return $this->indexByCode[$indexCode];
426+
}
427+
428+
/**
429+
* Retrieve mapped attributes from class.
430+
*
431+
* @param string $indexName
432+
* @return array
433+
*/
434+
private function getMappedAttributes(string $indexName): array
435+
{
436+
if (empty($this->mappedAttributes[$indexName])) {
437+
$mappedAttributes = $this->client->getMapping(['index' => $indexName]);
438+
$pathField = $this->arrayManager->findPath('properties', $mappedAttributes);
439+
$this->mappedAttributes[$indexName] = $this->arrayManager->get($pathField, $mappedAttributes, []);
440+
}
441+
442+
return $this->mappedAttributes[$indexName];
443+
}
444+
445+
/**
446+
* Set mapped attributes to class.
447+
*
448+
* @param string $indexName
449+
* @param array $mappedAttributes
450+
* @return $this
451+
*/
452+
private function setMappedAttributes(string $indexName, array $mappedAttributes): self
453+
{
454+
foreach ($mappedAttributes as $attributeCode => $attributeParams) {
455+
$this->mappedAttributes[$indexName][$attributeCode] = $attributeParams;
325456
}
326457

327458
return $this;

0 commit comments

Comments
 (0)