99use Magento \Framework \App \ObjectManager ;
1010use Magento \Framework \Exception \InputException ;
1111use Magento \Framework \Serialize \Serializer \Json ;
12+ use Magento \Swatches \Model \ResourceModel \Swatch as SwatchResource ;
1213use Magento \Swatches \Model \Swatch ;
1314
1415/**
@@ -18,6 +19,11 @@ class EavAttribute
1819{
1920 const DEFAULT_STORE_ID = 0 ;
2021
22+ /**
23+ * @var SwatchResource
24+ */
25+ private $ swatchResource ;
26+
2127 /**
2228 * Base option title used for string operations to detect is option already exists or new
2329 */
@@ -64,17 +70,20 @@ class EavAttribute
6470 * @param \Magento\Swatches\Model\SwatchFactory $swatchFactory
6571 * @param \Magento\Swatches\Helper\Data $swatchHelper
6672 * @param Json|null $serializer
73+ * @param SwatchResource|null $swatchResource
6774 */
6875 public function __construct (
6976 \Magento \Swatches \Model \ResourceModel \Swatch \CollectionFactory $ collectionFactory ,
7077 \Magento \Swatches \Model \SwatchFactory $ swatchFactory ,
7178 \Magento \Swatches \Helper \Data $ swatchHelper ,
72- Json $ serializer = null
79+ Json $ serializer = null ,
80+ SwatchResource $ swatchResource = null
7381 ) {
7482 $ this ->swatchCollectionFactory = $ collectionFactory ;
7583 $ this ->swatchFactory = $ swatchFactory ;
7684 $ this ->swatchHelper = $ swatchHelper ;
7785 $ this ->serializer = $ serializer ?: ObjectManager::getInstance ()->create (Json::class);
86+ $ this ->swatchResource = $ swatchResource ?: ObjectManager::getInstance ()->create (SwatchResource::class);
7887 }
7988
8089 /**
@@ -148,6 +157,7 @@ protected function setProperOptionsArray(Attribute $attribute)
148157 * Prepare attribute for conversion from any swatch type to dropdown
149158 *
150159 * @param Attribute $attribute
160+ * @throws \Magento\Framework\Exception\LocalizedException
151161 * @return void
152162 */
153163 protected function convertSwatchToDropdown (Attribute $ attribute )
@@ -157,6 +167,7 @@ protected function convertSwatchToDropdown(Attribute $attribute)
157167 if (!empty ($ additionalData )) {
158168 $ additionalData = $ this ->serializer ->unserialize ($ additionalData );
159169 if (is_array ($ additionalData ) && isset ($ additionalData [Swatch::SWATCH_INPUT_TYPE_KEY ])) {
170+ $ this ->cleanEavAttributeOptionSwatchValues ($ attribute ->getOption ());
160171 unset($ additionalData [Swatch::SWATCH_INPUT_TYPE_KEY ]);
161172 $ attribute ->setData ('additional_data ' , $ this ->serializer ->serialize ($ additionalData ));
162173 }
@@ -235,6 +246,7 @@ protected function saveSwatchParams(Attribute $attribute)
235246 {
236247 if ($ this ->swatchHelper ->isVisualSwatch ($ attribute )) {
237248 $ this ->processVisualSwatch ($ attribute );
249+ $ this ->cleanTextSwatchValuesAfterSwitch ($ attribute ->getOptiontext ());
238250 } elseif ($ this ->swatchHelper ->isTextSwatch ($ attribute )) {
239251 $ this ->processTextualSwatch ($ attribute );
240252 }
@@ -267,6 +279,33 @@ protected function processVisualSwatch(Attribute $attribute)
267279 }
268280 }
269281
282+ /**
283+ * Clean swatch option values after switching to the dropdown type.
284+ *
285+ * @param array $attributeOptions
286+ * @param null $swatchType
287+ * @throws \Magento\Framework\Exception\LocalizedException
288+ */
289+ private function cleanEavAttributeOptionSwatchValues ($ attributeOptions , $ swatchType = null )
290+ {
291+ if (count ($ attributeOptions ) && isset ($ attributeOptions ['value ' ])) {
292+ $ optionsIDs = array_keys ($ attributeOptions ['value ' ]);
293+
294+ $ this ->swatchResource ->clearSwatchOptionByOptionIdAndType ($ optionsIDs , $ swatchType );
295+ }
296+ }
297+
298+ /**
299+ * Cleaning the text type of swatch option values after switching.
300+ *
301+ * @param array $attributeOptions
302+ * @throws \Magento\Framework\Exception\LocalizedException
303+ */
304+ private function cleanTextSwatchValuesAfterSwitch ($ attributeOptions )
305+ {
306+ $ this ->cleanEavAttributeOptionSwatchValues ($ attributeOptions , Swatch::SWATCH_TYPE_TEXTUAL );
307+ }
308+
270309 /**
271310 * @param string $value
272311 * @return int
@@ -432,7 +471,7 @@ protected function validateOptions(Attribute $attribute)
432471 $ options = $ attribute ->getData ('optiontext ' );
433472 }
434473 if ($ options && !$ this ->isOptionsValid ($ options , $ attribute )) {
435- throw new InputException (__ ('Admin is a required field in the each row ' ));
474+ throw new InputException (__ ('Admin is a required field in each row ' ));
436475 }
437476 return true ;
438477 }
0 commit comments