3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
7
+
6
8
namespace Magento \Swatches \Model \Plugin ;
7
9
8
10
use Magento \Catalog \Model \ResourceModel \Eav \Attribute ;
9
11
use Magento \Framework \App \ObjectManager ;
10
12
use Magento \Framework \Exception \InputException ;
13
+ use Magento \Framework \Exception \LocalizedException ;
11
14
use Magento \Framework \Serialize \Serializer \Json ;
15
+ use Magento \Swatches \Helper \Data ;
12
16
use Magento \Swatches \Model \ResourceModel \Swatch as SwatchResource ;
17
+ use Magento \Swatches \Model \ResourceModel \Swatch \CollectionFactory ;
13
18
use Magento \Swatches \Model \Swatch ;
19
+ use Magento \Swatches \Model \SwatchFactory ;
14
20
15
21
/**
16
22
* Plugin model for Catalog Resource Attribute
@@ -21,11 +27,6 @@ class EavAttribute
21
27
{
22
28
const DEFAULT_STORE_ID = 0 ;
23
29
24
- /**
25
- * @var SwatchResource
26
- */
27
- private $ swatchResource ;
28
-
29
30
/**
30
31
* Base option title used for string operations to detect is option already exists or new
31
32
*/
@@ -37,20 +38,30 @@ class EavAttribute
37
38
private const API_OPTION_PREFIX = 'id_ ' ;
38
39
39
40
/**
40
- * @var \Magento\Swatches\Model\ResourceModel\Swatch\ CollectionFactory
41
+ * @var CollectionFactory
41
42
*/
42
43
protected $ swatchCollectionFactory ;
43
44
44
45
/**
45
- * @var \Magento\Swatches\Model\ SwatchFactory
46
+ * @var SwatchFactory
46
47
*/
47
48
protected $ swatchFactory ;
48
49
49
50
/**
50
- * @var \Magento\Swatches\Helper\ Data
51
+ * @var Data
51
52
*/
52
53
protected $ swatchHelper ;
53
54
55
+ /**
56
+ * @var Json
57
+ */
58
+ private $ serializer ;
59
+
60
+ /**
61
+ * @var SwatchResource
62
+ */
63
+ private $ swatchResource ;
64
+
54
65
/**
55
66
* Array which contains links for new created attributes for swatches
56
67
*
@@ -66,23 +77,16 @@ class EavAttribute
66
77
protected $ isSwatchExists ;
67
78
68
79
/**
69
- * Serializer from arrays to string.
70
- *
71
- * @var Json
72
- */
73
- private $ serializer ;
74
-
75
- /**
76
- * @param \Magento\Swatches\Model\ResourceModel\Swatch\CollectionFactory $collectionFactory
77
- * @param \Magento\Swatches\Model\SwatchFactory $swatchFactory
78
- * @param \Magento\Swatches\Helper\Data $swatchHelper
80
+ * @param CollectionFactory $collectionFactory
81
+ * @param SwatchFactory $swatchFactory
82
+ * @param Data $swatchHelper
79
83
* @param Json|null $serializer
80
84
* @param SwatchResource|null $swatchResource
81
85
*/
82
86
public function __construct (
83
- \ Magento \ Swatches \ Model \ ResourceModel \ Swatch \ CollectionFactory $ collectionFactory ,
84
- \ Magento \ Swatches \ Model \ SwatchFactory $ swatchFactory ,
85
- \ Magento \ Swatches \ Helper \ Data $ swatchHelper ,
87
+ CollectionFactory $ collectionFactory ,
88
+ SwatchFactory $ swatchFactory ,
89
+ Data $ swatchHelper ,
86
90
Json $ serializer = null ,
87
91
SwatchResource $ swatchResource = null
88
92
) {
@@ -97,6 +101,7 @@ public function __construct(
97
101
* Set base data to Attribute
98
102
*
99
103
* @param Attribute $attribute
104
+ *
100
105
* @return void
101
106
*/
102
107
public function beforeBeforeSave (Attribute $ attribute )
@@ -113,8 +118,9 @@ public function beforeBeforeSave(Attribute $attribute)
113
118
* Swatch save operations
114
119
*
115
120
* @param Attribute $attribute
116
- * @throws \Magento\Framework\Exception\LocalizedException
121
+ *
117
122
* @return void
123
+ * @throws LocalizedException
118
124
*/
119
125
public function afterAfterSave (Attribute $ attribute )
120
126
{
@@ -129,6 +135,7 @@ public function afterAfterSave(Attribute $attribute)
129
135
* Substitute suitable options and swatches arrays
130
136
*
131
137
* @param Attribute $attribute
138
+ *
132
139
* @return void
133
140
*/
134
141
protected function setProperOptionsArray (Attribute $ attribute )
@@ -164,8 +171,9 @@ protected function setProperOptionsArray(Attribute $attribute)
164
171
* Prepare attribute for conversion from any swatch type to dropdown
165
172
*
166
173
* @param Attribute $attribute
167
- * @throws \Magento\Framework\Exception\LocalizedException
174
+ *
168
175
* @return void
176
+ * @throws LocalizedException
169
177
*/
170
178
protected function convertSwatchToDropdown (Attribute $ attribute )
171
179
{
@@ -187,8 +195,9 @@ protected function convertSwatchToDropdown(Attribute $attribute)
187
195
* Creates array which link new option ids
188
196
*
189
197
* @param Attribute $attribute
198
+ *
190
199
* @return Attribute
191
- * @throws \Magento\Framework\Exception\ LocalizedException
200
+ * @throws LocalizedException
192
201
*/
193
202
protected function processSwatchOptions (Attribute $ attribute )
194
203
{
@@ -209,15 +218,14 @@ protected function processSwatchOptions(Attribute $attribute)
209
218
* Get options array without deleted items
210
219
*
211
220
* @param array $optionsArray
221
+ *
212
222
* @return array
213
223
*/
214
224
protected function prepareOptionIds (array $ optionsArray )
215
225
{
216
226
if (isset ($ optionsArray ['value ' ]) && is_array ($ optionsArray ['value ' ])) {
217
227
foreach (array_keys ($ optionsArray ['value ' ]) as $ optionId ) {
218
- if (isset ($ optionsArray ['delete ' ]) && isset ($ optionsArray ['delete ' ][$ optionId ])
219
- && $ optionsArray ['delete ' ][$ optionId ] == 1
220
- ) {
228
+ if (isset ($ optionsArray ['delete ' ][$ optionId ]) && $ optionsArray ['delete ' ][$ optionId ] == 1 ) {
221
229
unset($ optionsArray ['value ' ][$ optionId ]);
222
230
}
223
231
}
@@ -230,6 +238,7 @@ protected function prepareOptionIds(array $optionsArray)
230
238
*
231
239
* @param array $optionsArray
232
240
* @param array $attributeSavedOptions
241
+ *
233
242
* @return void
234
243
*/
235
244
protected function prepareOptionLinks (array $ optionsArray , array $ attributeSavedOptions )
@@ -249,6 +258,7 @@ protected function prepareOptionLinks(array $optionsArray, array $attributeSaved
249
258
* Save all Swatches data
250
259
*
251
260
* @param Attribute $attribute
261
+ *
252
262
* @return void
253
263
*/
254
264
protected function saveSwatchParams (Attribute $ attribute )
@@ -266,6 +276,7 @@ protected function saveSwatchParams(Attribute $attribute)
266
276
* Save Visual Swatch data
267
277
*
268
278
* @param Attribute $attribute
279
+ *
269
280
* @return void
270
281
*/
271
282
protected function processVisualSwatch (Attribute $ attribute )
@@ -294,7 +305,7 @@ protected function processVisualSwatch(Attribute $attribute)
294
305
*
295
306
* @param array $attributeOptions
296
307
* @param int|null $swatchType
297
- * @throws \Magento\Framework\Exception\ LocalizedException
308
+ * @throws LocalizedException
298
309
*/
299
310
private function cleanEavAttributeOptionSwatchValues (array $ attributeOptions , int $ swatchType = null )
300
311
{
@@ -309,7 +320,7 @@ private function cleanEavAttributeOptionSwatchValues(array $attributeOptions, in
309
320
* Cleaning the text type of swatch option values after switching.
310
321
*
311
322
* @param array $attributeOptions
312
- * @throws \Magento\Framework\Exception\ LocalizedException
323
+ * @throws LocalizedException
313
324
*/
314
325
private function cleanTextSwatchValuesAfterSwitch (array $ attributeOptions )
315
326
{
@@ -320,6 +331,7 @@ private function cleanTextSwatchValuesAfterSwitch(array $attributeOptions)
320
331
* Get the visual swatch type based on its value
321
332
*
322
333
* @param string $value
334
+ *
323
335
* @return int
324
336
*/
325
337
private function determineSwatchType ($ value )
@@ -330,13 +342,15 @@ private function determineSwatchType($value)
330
342
} elseif (!empty ($ value ) && $ value [0 ] == '/ ' ) {
331
343
$ swatchType = Swatch::SWATCH_TYPE_VISUAL_IMAGE ;
332
344
}
345
+
333
346
return $ swatchType ;
334
347
}
335
348
336
349
/**
337
350
* Save Textual Swatch data
338
351
*
339
352
* @param Attribute $attribute
353
+ *
340
354
* @return void
341
355
*/
342
356
protected function processTextualSwatch (Attribute $ attribute )
@@ -352,7 +366,7 @@ protected function processTextualSwatch(Attribute $attribute)
352
366
}
353
367
$ defaultSwatchValue = reset ($ storeValues );
354
368
foreach ($ storeValues as $ storeId => $ value ) {
355
- if (! $ value ) {
369
+ if ($ value === null || $ value === '' ) {
356
370
$ value = $ defaultSwatchValue ;
357
371
}
358
372
$ swatch = $ this ->loadSwatchIfExists ($ optionId , $ storeId );
@@ -361,7 +375,7 @@ protected function processTextualSwatch(Attribute $attribute)
361
375
$ swatch ,
362
376
$ optionId ,
363
377
$ storeId ,
364
- \ Magento \ Swatches \ Model \ Swatch::SWATCH_TYPE_TEXTUAL ,
378
+ Swatch::SWATCH_TYPE_TEXTUAL ,
365
379
$ value
366
380
);
367
381
$ this ->isSwatchExists = null ;
@@ -374,13 +388,16 @@ protected function processTextualSwatch(Attribute $attribute)
374
388
* Get option id. If it not exist get it from dependency link array
375
389
*
376
390
* @param integer $optionId
391
+ *
377
392
* @return int
378
393
*/
379
394
protected function getAttributeOptionId ($ optionId )
380
395
{
381
- if (substr ($ optionId , 0 , 6 ) == self ::BASE_OPTION_TITLE || substr ($ optionId , 0 , 3 ) == self ::API_OPTION_PREFIX ) {
382
- $ optionId = isset ($ this ->dependencyArray [$ optionId ]) ? $ this ->dependencyArray [$ optionId ] : null ;
396
+ if (strpos ((string )$ optionId , self ::BASE_OPTION_TITLE ) === 0 ||
397
+ strpos ((string )$ optionId , self ::API_OPTION_PREFIX ) === 0 ) {
398
+ $ optionId = $ this ->dependencyArray [$ optionId ] ?? null ;
383
399
}
400
+
384
401
return $ optionId ;
385
402
}
386
403
@@ -389,11 +406,13 @@ protected function getAttributeOptionId($optionId)
389
406
*
390
407
* @param Attribute $attribute
391
408
* @param integer $optionId
409
+ *
392
410
* @return bool
393
411
*/
394
412
protected function isOptionForDelete (Attribute $ attribute , $ optionId )
395
413
{
396
414
$ isOptionForDelete = $ attribute ->getData ('option/delete/ ' . $ optionId );
415
+
397
416
return isset ($ isOptionForDelete ) && $ isOptionForDelete ;
398
417
}
399
418
@@ -402,6 +421,7 @@ protected function isOptionForDelete(Attribute $attribute, $optionId)
402
421
*
403
422
* @param int $optionId
404
423
* @param int $storeId
424
+ *
405
425
* @return Swatch
406
426
*/
407
427
protected function loadSwatchIfExists ($ optionId , $ storeId )
@@ -415,6 +435,7 @@ protected function loadSwatchIfExists($optionId, $storeId)
415
435
if ($ loadedSwatch ->getId ()) {
416
436
$ this ->isSwatchExists = true ;
417
437
}
438
+
418
439
return $ loadedSwatch ;
419
440
}
420
441
@@ -426,6 +447,7 @@ protected function loadSwatchIfExists($optionId, $storeId)
426
447
* @param integer $storeId
427
448
* @param integer $type
428
449
* @param string $value
450
+ *
429
451
* @return void
430
452
*/
431
453
protected function saveSwatchData ($ swatch , $ optionId , $ storeId , $ type , $ value )
@@ -446,6 +468,7 @@ protected function saveSwatchData($swatch, $optionId, $storeId, $type, $value)
446
468
* Save default swatch value using Swatch model instead of Eav model
447
469
*
448
470
* @param Attribute $attribute
471
+ *
449
472
* @return void
450
473
*/
451
474
protected function saveDefaultSwatchOptionValue (Attribute $ attribute )
@@ -455,7 +478,7 @@ protected function saveDefaultSwatchOptionValue(Attribute $attribute)
455
478
}
456
479
$ defaultValue = $ attribute ->getData ('default/0 ' );
457
480
if (!empty ($ defaultValue )) {
458
- /** @var \Magento\Swatches\Model\ Swatch $swatch */
481
+ /** @var Swatch $swatch */
459
482
$ swatch = $ this ->swatchFactory ->create ();
460
483
$ swatch ->getResource ()->saveDefaultSwatchOption (
461
484
$ attribute ->getId (),
@@ -468,6 +491,7 @@ protected function saveDefaultSwatchOptionValue(Attribute $attribute)
468
491
* Validate that attribute options exist
469
492
*
470
493
* @param Attribute $attribute
494
+ *
471
495
* @return bool
472
496
* @throws InputException
473
497
*/
@@ -482,6 +506,7 @@ protected function validateOptions(Attribute $attribute)
482
506
if ($ options && !$ this ->isOptionsValid ($ options , $ attribute )) {
483
507
throw new InputException (__ ('Admin is a required field in each row ' ));
484
508
}
509
+
485
510
return true ;
486
511
}
487
512
@@ -490,22 +515,26 @@ protected function validateOptions(Attribute $attribute)
490
515
*
491
516
* @param array $options
492
517
* @param Attribute $attribute
518
+ *
493
519
* @return bool
494
520
*/
495
521
protected function isOptionsValid (array $ options , Attribute $ attribute )
496
522
{
497
523
if (!isset ($ options ['value ' ])) {
498
524
return false ;
499
525
}
526
+
500
527
foreach ($ options ['value ' ] as $ optionId => $ option ) {
501
528
// do not validate options marked as deleted
502
529
if ($ this ->isOptionForDelete ($ attribute , $ optionId )) {
503
530
continue ;
504
531
}
532
+
505
533
if (!isset ($ option [0 ]) || $ option [0 ] === '' ) {
506
534
return false ;
507
535
}
508
536
}
537
+
509
538
return true ;
510
539
}
511
540
@@ -516,13 +545,15 @@ protected function isOptionsValid(array $options, Attribute $attribute)
516
545
*
517
546
* @param Attribute $attribute
518
547
* @param bool $result
548
+ *
519
549
* @return bool
520
550
*/
521
551
public function afterUsesSource (Attribute $ attribute , $ result )
522
552
{
523
553
if ($ this ->swatchHelper ->isSwatchAttribute ($ attribute )) {
524
554
return true ;
525
555
}
556
+
526
557
return $ result ;
527
558
}
528
559
}
0 commit comments