Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
OptionManagement.php
Go to the documentation of this file.
1 <?php
8 
12 
14 {
19 
23  protected $resourceModel;
24 
30  public function __construct(
33  ) {
34  $this->attributeRepository = $attributeRepository;
35  $this->resourceModel = $resourceModel;
36  }
37 
42  {
43  if (empty($attributeCode)) {
44  throw new InputException(__('The attribute code is empty. Enter the code and try again.'));
45  }
46 
47  $attribute = $this->attributeRepository->get($entityType, $attributeCode);
48  if (!$attribute->usesSource()) {
49  throw new StateException(__('The "%1" attribute doesn\'t work with options.', $attributeCode));
50  }
51 
52  $optionLabel = $option->getLabel();
53  $optionId = $this->getOptionId($option);
54  $options = [];
55  $options['value'][$optionId][0] = $optionLabel;
56  $options['order'][$optionId] = $option->getSortOrder();
57 
58  if (is_array($option->getStoreLabels())) {
59  foreach ($option->getStoreLabels() as $label) {
60  $options['value'][$optionId][$label->getStoreId()] = $label->getLabel();
61  }
62  }
63 
64  if ($option->getIsDefault()) {
65  $attribute->setDefault([$optionId]);
66  }
67 
68  $attribute->setOption($options);
69  try {
70  $this->resourceModel->save($attribute);
71  if ($optionLabel && $attribute->getAttributeCode()) {
72  $this->setOptionValue($option, $attribute, $optionLabel);
73  }
74  } catch (\Exception $e) {
75  throw new StateException(__('The "%1" attribute can\'t be saved.', $attributeCode));
76  }
77 
78  return $this->getOptionId($option);
79  }
80 
84  public function delete($entityType, $attributeCode, $optionId)
85  {
86  if (empty($attributeCode)) {
87  throw new InputException(__('The attribute code is empty. Enter the code and try again.'));
88  }
89 
90  $attribute = $this->attributeRepository->get($entityType, $attributeCode);
91  if (!$attribute->usesSource()) {
92  throw new StateException(__('The "%1" attribute has no option.', $attributeCode));
93  }
95 
96  $removalMarker = [
97  'option' => [
98  'value' => [$optionId => []],
99  'delete' => [$optionId => '1'],
100  ],
101  ];
102  $attribute->addData($removalMarker);
103  try {
104  $this->resourceModel->save($attribute);
105  } catch (\Exception $e) {
106  throw new StateException(__('The "%1" attribute can\'t be saved.', $attributeCode));
107  }
108 
109  return true;
110  }
111 
116  {
117  if (empty($attributeCode)) {
118  throw new InputException(__('The attribute code is empty. Enter the code and try again.'));
119  }
120  $attribute = $this->attributeRepository->get($entityType, $attributeCode);
121 
122  try {
123  $options = $attribute->getOptions();
124  } catch (\Exception $e) {
125  throw new StateException(__('The options for "%1" attribute can\'t be loaded.', $attributeCode));
126  }
127 
128  return $options;
129  }
130 
137  protected function validateOption($attribute, $optionId)
138  {
139  if (!$attribute->getSource()->getOptionText($optionId)) {
140  throw new NoSuchEntityException(
141  __(
142  'The "%1" attribute doesn\'t include an option with "%2" ID.',
143  $attribute->getAttributeCode(),
144  $optionId
145  )
146  );
147  }
148  }
149 
154  private function getOptionId(\Magento\Eav\Api\Data\AttributeOptionInterface $option) : string
155  {
156  return $option->getValue() ?: 'new_option';
157  }
158 
165  private function setOptionValue(
166  \Magento\Eav\Api\Data\AttributeOptionInterface $option,
167  \Magento\Eav\Api\Data\AttributeInterface $attribute,
168  string $optionLabel
169  ) {
170  $optionId = $attribute->getSource()->getOptionId($optionLabel);
171  if ($optionId) {
172  $option->setValue($attribute->getSource()->getOptionId($optionId));
173  } elseif (is_array($option->getStoreLabels())) {
174  foreach ($option->getStoreLabels() as $label) {
175  if ($optionId = $attribute->getSource()->getOptionId($label->getLabel())) {
176  $option->setValue($attribute->getSource()->getOptionId($optionId));
177  break;
178  }
179  }
180  }
181  }
182 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
add($entityType, $attributeCode, $option)
__construct(\Magento\Eav\Model\AttributeRepository $attributeRepository, \Magento\Eav\Model\ResourceModel\Entity\Attribute $resourceModel)
__()
Definition: __.php:13
$label
Definition: details.phtml:21
$attributeCode
Definition: extend.phtml:12