1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Swatches\Block\Adminhtml\Attribute\Edit\Options;
use \Magento\Swatches\Model\Swatch as SwatchModel;
/**
* Backend swatch abstract block
*/
abstract class AbstractSwatch extends \Magento\Eav\Block\Adminhtml\Attribute\Edit\Options\Options
{
/**
* @var \Magento\Catalog\Model\Product\Media\Config
*/
protected $mediaConfig;
/**
* Helper to move image from tmp to catalog
*
* @var \Magento\Swatches\Helper\Media
*/
protected $swatchHelper;
/**
* Prepare option values of user defined attribute
*
* @codeCoverageIgnore
* @param array|\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option $option
* @param string $inputType
* @param array $defaultValues
* @return array
*/
protected function _prepareUserDefinedAttributeOptionValues($option, $inputType, $defaultValues)
{
$optionId = $option->getId();
$value['checked'] = in_array($optionId, $defaultValues) ? 'checked="checked"' : '';
$value['intype'] = $inputType;
$value['id'] = $optionId;
$value['sort_order'] = $option->getSortOrder();
foreach ($this->getStores() as $store) {
$value = array_merge(
$value,
$this->createStoreValues($store->getId(), $optionId)
);
}
return [$value];
}
/**
* @param \Magento\Backend\Block\Template\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory
* @param \Magento\Framework\Validator\UniversalFactory $universalFactory
* @param \Magento\Catalog\Model\Product\Media\Config $mediaConfig
* @param \Magento\Swatches\Helper\Media $swatchHelper
* @param array $data
*/
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory,
\Magento\Framework\Validator\UniversalFactory $universalFactory,
\Magento\Catalog\Model\Product\Media\Config $mediaConfig,
\Magento\Swatches\Helper\Media $swatchHelper,
array $data = []
) {
parent::__construct($context, $registry, $attrOptionCollectionFactory, $universalFactory, $data);
$this->swatchHelper = $swatchHelper;
$this->mediaConfig = $mediaConfig;
}
/**
* Create store values
*
* Method not intended to escape HTML entities
* Escaping will be applied in template files
*
* @param integer $storeId
* @param integer $optionId
* @return array
*/
protected function createStoreValues($storeId, $optionId)
{
$value = [];
$storeValues = $this->getStoreOptionValues($storeId);
$swatchStoreValue = isset($storeValues['swatch']) ? $storeValues['swatch'] : null;
$value['store' . $storeId] = isset($storeValues[$optionId]) ? $storeValues[$optionId] : '';
$value['swatch' . $storeId] = isset($swatchStoreValue[$optionId]) ? $swatchStoreValue[$optionId] : '';
return $value;
}
/**
* Retrieve attribute option values for given store id
*
* @param int $storeId
* @return array
*/
public function getStoreOptionValues($storeId)
{
$values = $this->getData('store_option_values_' . $storeId);
if ($values === null) {
$values = [];
$valuesCollection = $this->_attrOptionCollectionFactory->create();
$valuesCollection->setAttributeFilter(
$this->getAttributeObject()->getId()
);
$this->addCollectionStoreFilter($valuesCollection, $storeId);
$valuesCollection->getSelect()->joinLeft(
['swatch_table' => $valuesCollection->getTable('eav_attribute_option_swatch')],
'swatch_table.option_id = main_table.option_id AND swatch_table.store_id = '.$storeId,
'swatch_table.value AS label'
);
$valuesCollection->load();
foreach ($valuesCollection as $item) {
$values[$item->getId()] = $item->getValue();
$values['swatch'][$item->getId()] = $item->getLabel();
}
$this->setData('store_option_values_' . $storeId, $values);
}
return $values;
}
/**
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection $valuesCollection
* @param int $storeId
* @return void
*/
private function addCollectionStoreFilter($valuesCollection, $storeId)
{
$joinCondition = $valuesCollection->getConnection()->quoteInto(
'tsv.option_id = main_table.option_id AND tsv.store_id = ?',
$storeId
);
$select = $valuesCollection->getSelect();
$select->joinLeft(
['tsv' => $valuesCollection->getTable('eav_attribute_option_value')],
$joinCondition,
'value'
);
if (\Magento\Store\Model\Store::DEFAULT_STORE_ID == $storeId) {
$select->where(
'tsv.store_id = ?',
$storeId
);
}
$valuesCollection->setOrder('value', \Magento\Framework\Data\Collection::SORT_ORDER_ASC);
}
}