25 private $swatchResource;
79 Json $serializer =
null,
82 $this->swatchCollectionFactory = $collectionFactory;
97 if ($this->swatchHelper->isSwatchAttribute(
$attribute)) {
100 $this->swatchHelper->assembleAdditionalDataEavAttribute(
$attribute);
114 if ($this->swatchHelper->isSwatchAttribute(
$attribute)) {
116 $this->saveDefaultSwatchOptionValue(
$attribute);
130 if ($this->swatchHelper->isVisualSwatch(
$attribute)) {
132 $defaultValue =
$attribute->getData(
'defaultvisual');
133 $optionsArray =
$attribute->getData(
'optionvisual');
134 $swatchesArray =
$attribute->getData(
'swatchvisual');
137 $defaultValue =
$attribute->getData(
'defaulttext');
138 $optionsArray =
$attribute->getData(
'optiontext');
139 $swatchesArray =
$attribute->getData(
'swatchtext');
141 if ($canReplace ==
true) {
142 if (!empty($optionsArray)) {
145 if (!empty($defaultValue)) {
146 $attribute->setData(
'default', $defaultValue);
150 if (!empty($swatchesArray)) {
151 $attribute->setData(
'swatch', $swatchesArray);
166 $additionalData =
$attribute->getData(
'additional_data');
167 if (!empty($additionalData)) {
168 $additionalData = $this->serializer->unserialize($additionalData);
171 $this->cleanEavAttributeOptionSwatchValues(
$option);
173 $attribute->setData(
'additional_data', $this->serializer->serialize($additionalData));
188 $optionsArray =
$attribute->getData(
'option');
190 if (!empty($optionsArray) && is_array($optionsArray)) {
192 $attributeSavedOptions =
$attribute->getSource()->getAllOptions();
207 if (isset($optionsArray[
'value']) && is_array($optionsArray[
'value'])) {
208 foreach (array_keys($optionsArray[
'value']) as
$optionId) {
209 if (isset($optionsArray[
'delete']) && isset($optionsArray[
'delete'][
$optionId])
210 && $optionsArray[
'delete'][
$optionId] == 1
212 unset($optionsArray[
'value'][
$optionId]);
216 return $optionsArray;
229 if (is_array($optionsArray[
'value'])) {
231 foreach (array_keys($optionsArray[
'value']) as $baseOptionId) {
232 $dependencyArray[$baseOptionId] = $attributeSavedOptions[$optionCounter][
'value'];
248 if ($this->swatchHelper->isVisualSwatch(
$attribute)) {
250 $attributeOptions =
$attribute->getOptiontext() ?: [];
251 $this->cleanTextSwatchValuesAfterSwitch($attributeOptions);
265 $swatchArray =
$attribute->getData(
'swatch/value');
266 if (isset($swatchArray) && is_array($swatchArray)) {
270 if (
$optionId ===
null || $isOptionForDelete) {
276 $swatchType = $this->determineSwatchType(
$value);
279 $this->isSwatchExists =
null;
291 private function cleanEavAttributeOptionSwatchValues(array $attributeOptions,
int $swatchType =
null)
293 if (count($attributeOptions) && isset($attributeOptions[
'value'])) {
294 $optionsIDs = array_keys($attributeOptions[
'value']);
296 $this->swatchResource->clearSwatchOptionByOptionIdAndType($optionsIDs, $swatchType);
306 private function cleanTextSwatchValuesAfterSwitch(array $attributeOptions)
315 private function determineSwatchType(
$value)
334 $swatchArray =
$attribute->getData(
'swatch/value');
335 if (isset($swatchArray) && is_array($swatchArray)) {
336 foreach ($swatchArray as
$optionId => $storeValues) {
339 if (
$optionId ===
null || $isOptionForDelete) {
343 $defaultSwatchValue = reset($storeValues);
346 $value = $defaultSwatchValue;
349 $swatch->isDeleted($isOptionForDelete);
357 $this->isSwatchExists =
null;
371 if (substr(
$optionId, 0, 6) == self::BASE_OPTION_TITLE) {
387 return isset($isOptionForDelete) && $isOptionForDelete;
399 $collection = $this->swatchCollectionFactory->create();
405 if ($loadedSwatch->getId()) {
406 $this->isSwatchExists =
true;
408 return $loadedSwatch;
423 if ($this->isSwatchExists) {
424 $swatch->setData(
'type',
$type);
425 $swatch->setData(
'value',
$value);
427 $swatch->setData(
'option_id',
$optionId);
428 $swatch->setData(
'store_id',
$storeId);
429 $swatch->setData(
'type',
$type);
430 $swatch->setData(
'value',
$value);
443 if (!$this->swatchHelper->isSwatchAttribute(
$attribute)) {
446 $defaultValue =
$attribute->getData(
'default/0');
447 if (!empty($defaultValue)) {
449 $swatch = $this->swatchFactory->create();
451 if (substr($defaultValue, 0, 6) == self::BASE_OPTION_TITLE &&
452 isset($this->dependencyArray[$defaultValue])
454 $defaultValue = $this->dependencyArray[$defaultValue];
456 $swatch->getResource()->saveDefaultSwatchOption(
$attribute->getId(), $defaultValue);
470 if ($this->swatchHelper->isVisualSwatch(
$attribute)) {
512 if ($this->swatchHelper->isSwatchAttribute(
$attribute)) {
afterAfterSave(Attribute $attribute)
saveSwatchData($swatch, $optionId, $storeId, $type, $value)
setProperOptionsArray(Attribute $attribute)
elseif(isset( $params[ 'redirect_parent']))
beforeBeforeSave(Attribute $attribute)
const SWATCH_INPUT_TYPE_DROPDOWN
prepareOptionLinks(array $optionsArray, array $attributeSavedOptions)
__construct(\Magento\Swatches\Model\ResourceModel\Swatch\CollectionFactory $collectionFactory, \Magento\Swatches\Model\SwatchFactory $swatchFactory, \Magento\Swatches\Helper\Data $swatchHelper, Json $serializer=null, SwatchResource $swatchResource=null)
const SWATCH_TYPE_VISUAL_IMAGE
validateOptions(Attribute $attribute)
const SWATCH_TYPE_TEXTUAL
afterUsesSource(Attribute $attribute, $result)
processSwatchOptions(Attribute $attribute)
getAttributeOptionId($optionId)
processVisualSwatch(Attribute $attribute)
prepareOptionIds(array $optionsArray)
saveSwatchParams(Attribute $attribute)
const SWATCH_INPUT_TYPE_KEY
const SWATCH_TYPE_VISUAL_COLOR
loadSwatchIfExists($optionId, $storeId)
convertSwatchToDropdown(Attribute $attribute)
isOptionForDelete(Attribute $attribute, $optionId)
processTextualSwatch(Attribute $attribute)
isOptionsValid(array $options, Attribute $attribute)