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

#8810 - REST API - Attribute option creation -> no ID returned #12920

Merged
merged 7 commits into from
Jul 3, 2018
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function getItems($attributeCode);
* @param \Magento\Eav\Api\Data\AttributeOptionInterface $option
* @throws \Magento\Framework\Exception\StateException
* @throws \Magento\Framework\Exception\InputException
* @return bool
* @return \Magento\Eav\Api\Data\AttributeOptionInterface
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please return the option ID instead of object

*/
public function add($attributeCode, $option);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ interface AttributeOptionManagementInterface
* @param \Magento\Eav\Api\Data\AttributeOptionInterface $option
* @throws \Magento\Framework\Exception\StateException
* @throws \Magento\Framework\Exception\InputException
* @return bool
* @return \Magento\Eav\Api\Data\AttributeOptionInterface
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please return the option ID instead of object

*/
public function add($entityType, $attributeCode, $option);

Expand Down
29 changes: 27 additions & 2 deletions app/code/Magento/Eav/Model/Entity/Attribute/OptionManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ public function add($entityType, $attributeCode, $option)
throw new StateException(__('Attribute %1 doesn\'t work with options', $attributeCode));
}

$optionLabel = $option->getLabel();
$optionId = $this->getOptionId($option);
$options = [];
$options['value'][$optionId][0] = $option->getLabel();
$options['value'][$optionId][0] = $optionLabel;
$options['order'][$optionId] = $option->getSortOrder();

if (is_array($option->getStoreLabels())) {
Expand All @@ -67,11 +68,14 @@ public function add($entityType, $attributeCode, $option)
$attribute->setOption($options);
try {
$this->resourceModel->save($attribute);
if ($optionLabel && $attribute->getAttributeCode()) {
$this->setOptionValue($option, $attribute, $optionLabel);
}
} catch (\Exception $e) {
throw new StateException(__('Cannot save attribute %1', $attributeCode));
}

return true;
return $option;
}

/**
Expand Down Expand Up @@ -147,4 +151,25 @@ private function getOptionId($option)
{
return $option->getValue() ?: 'new_option';
}

/**
* @param \Magento\Eav\Api\Data\AttributeOptionInterface $option
* @param \Magento\Eav\Api\Data\AttributeInterface $attribute
* @param string $optionLabel
* @return void
*/
protected function setOptionValue($option, $attribute, $optionLabel)
{
$optionId = $attribute->getSource()->getOptionId($optionLabel);
if ($optionId) {
$option->setValue($attribute->getSource()->getOptionId($optionId));
} elseif (is_array($option->getStoreLabels())) {
foreach ($option->getStoreLabels() as $label) {
if ($optionId = $attribute->getSource()->getOptionId($label->getLabel())) {
$option->setValue($attribute->getSource()->getOptionId($optionId));
break;
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function testAdd()
$attributeMock->expects($this->once())->method('setDefault')->with(['new_option']);
$attributeMock->expects($this->once())->method('setOption')->with($option);
$this->resourceModelMock->expects($this->once())->method('save')->with($attributeMock);
$this->assertTrue($this->model->add($entityType, $attributeCode, $optionMock));
$this->assertEquals($optionMock, $this->model->add($entityType, $attributeCode, $optionMock));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function testAdd($optionData)
]
);

$this->assertTrue($response);
$this->assertNotNull($response[AttributeOptionInterface::VALUE]);
$updatedData = $this->getAttributeOptions($testAttributeCode);
$lastOption = array_pop($updatedData);
$this->assertEquals(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function testAdd($optionData)
]
);

$this->assertTrue($response);
$this->assertNotNull($response[AttributeOptionInterface::VALUE]);
$updatedData = $this->getAttributeOptions($testAttributeCode);
$lastOption = array_pop($updatedData);
$this->assertEquals(
Expand Down