Skip to content

Commit eb982d5

Browse files
committed
MAGETWO-66588: [Backport] Perfomance toolkit generator improvement for 2.1
1 parent 4a3eb46 commit eb982d5

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

app/code/Magento/Catalog/Model/Product/Attribute/Repository.php

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use Magento\Framework\Exception\InputException;
1010
use Magento\Framework\Exception\NoSuchEntityException;
11+
use Magento\Eav\Api\Data\AttributeInterface;
1112

1213
/**
1314
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -49,11 +50,6 @@ class Repository implements \Magento\Catalog\Api\ProductAttributeRepositoryInter
4950
*/
5051
protected $searchCriteriaBuilder;
5152

52-
/**
53-
* @var \Magento\Catalog\Api\ProductAttributeOptionManagementInterface
54-
*/
55-
private $optionManagement;
56-
5753
/**
5854
* @param \Magento\Catalog\Model\ResourceModel\Attribute $attributeResource
5955
* @param \Magento\Catalog\Helper\Product $productHelper
@@ -117,12 +113,17 @@ public function save(\Magento\Catalog\Api\Data\ProductAttributeInterface $attrib
117113
throw NoSuchEntityException::singleField('attribute_code', $existingModel->getAttributeCode());
118114
}
119115

116+
// Attribute code must not be changed after attribute creation
117+
$attribute->setAttributeCode($existingModel->getAttributeCode());
120118
$attribute->setAttributeId($existingModel->getAttributeId());
121119
$attribute->setIsUserDefined($existingModel->getIsUserDefined());
122120
$attribute->setFrontendInput($existingModel->getFrontendInput());
123121

124122
if (is_array($attribute->getFrontendLabels())) {
125-
$frontendLabel[0] = $existingModel->getDefaultFrontendLabel();
123+
$defaultFrontendLabel = $attribute->getDefaultFrontendLabel();
124+
$frontendLabel[0] = !empty($defaultFrontendLabel)
125+
? $defaultFrontendLabel
126+
: $existingModel->getDefaultFrontendLabel();
126127
foreach ($attribute->getFrontendLabels() as $item) {
127128
$frontendLabel[$item->getStoreId()] = $item->getLabel();
128129
}
@@ -171,10 +172,31 @@ public function save(\Magento\Catalog\Api\Data\ProductAttributeInterface $attrib
171172
);
172173
$attribute->setIsUserDefined(1);
173174
}
174-
$this->attributeResource->save($attribute);
175-
foreach ($attribute->getOptions() as $option) {
176-
$this->getOptionManagement()->add($attribute->getAttributeCode(), $option);
175+
if (!empty($attribute->getData(AttributeInterface::OPTIONS))) {
176+
$options = [];
177+
$sortOrder = 0;
178+
$default = [];
179+
$optionIndex = 0;
180+
foreach ($attribute->getOptions() as $option) {
181+
$optionIndex++;
182+
$optionId = $option->getValue() ?: 'option_' . $optionIndex;
183+
$options['value'][$optionId][0] = $option->getLabel();
184+
$options['order'][$optionId] = $option->getSortOrder() ?: $sortOrder++;
185+
if (is_array($option->getStoreLabels())) {
186+
foreach ($option->getStoreLabels() as $label) {
187+
$options['value'][$optionId][$label->getStoreId()] = $label->getLabel();
188+
}
189+
}
190+
if ($option->getIsDefault()) {
191+
$default[] = $optionId;
192+
}
193+
}
194+
$attribute->setDefault($default);
195+
if (count($options)) {
196+
$attribute->setOption($options);
197+
}
177198
}
199+
$this->attributeResource->save($attribute);
178200
return $this->get($attribute->getAttributeCode());
179201
}
180202

@@ -253,16 +275,4 @@ protected function validateFrontendInput($frontendInput)
253275
throw InputException::invalidFieldValue('frontend_input', $frontendInput);
254276
}
255277
}
256-
257-
/**
258-
* @return \Magento\Catalog\Api\ProductAttributeOptionManagementInterface
259-
*/
260-
private function getOptionManagement()
261-
{
262-
if (null === $this->optionManagement) {
263-
$this->optionManagement = \Magento\Framework\App\ObjectManager::getInstance()
264-
->get('Magento\Catalog\Api\ProductAttributeOptionManagementInterface');
265-
}
266-
return $this->optionManagement;
267-
}
268278
}

0 commit comments

Comments
 (0)