Skip to content

Commit bc104f0

Browse files
author
Klymenko, Volodymyr(vklymenko)
committed
Merge pull request magento#494 from magento-dragons/MAGETWO-40750
MAGETWO-40750: Make Product Attribute Swatches available for both CE and EE
2 parents 26459ac + 71037fa commit bc104f0

File tree

71 files changed

+9491
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+9491
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Swatches\Block\Adminhtml\Attribute\Edit\Options;
7+
8+
use \Magento\Swatches\Model\Swatch as SwatchModel;
9+
10+
/**
11+
* Backend swatch abstract block
12+
*/
13+
abstract class AbstractSwatch extends \Magento\Eav\Block\Adminhtml\Attribute\Edit\Options\Options
14+
{
15+
/**
16+
* @var \Magento\Catalog\Model\Product\Media\Config
17+
*/
18+
protected $mediaConfig;
19+
20+
/**
21+
* Helper to move image from tmp to catalog
22+
*
23+
* @var \Magento\Swatches\Helper\Media
24+
*/
25+
protected $swatchHelper;
26+
27+
/**
28+
* Prepare option values of user defined attribute
29+
*
30+
* @codeCoverageIgnore
31+
* @param array|\Magento\Eav\Model\Resource\Entity\Attribute\Option $option
32+
* @param string $inputType
33+
* @param array $defaultValues
34+
* @return array
35+
*/
36+
protected function _prepareUserDefinedAttributeOptionValues($option, $inputType, $defaultValues)
37+
{
38+
$optionId = $option->getId();
39+
40+
$value['checked'] = in_array($optionId, $defaultValues) ? 'checked="checked"' : '';
41+
$value['intype'] = $inputType;
42+
$value['id'] = $optionId;
43+
$value['sort_order'] = $option->getSortOrder();
44+
45+
foreach ($this->getStores() as $store) {
46+
$value = array_merge(
47+
$value,
48+
$this->createStoreValues($store->getId(), $optionId)
49+
);
50+
}
51+
52+
return [$value];
53+
}
54+
55+
/**
56+
* @param \Magento\Backend\Block\Template\Context $context
57+
* @param \Magento\Framework\Registry $registry
58+
* @param \Magento\Eav\Model\Resource\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory
59+
* @param \Magento\Framework\Validator\UniversalFactory $universalFactory
60+
* @param \Magento\Catalog\Model\Product\Media\Config $mediaConfig
61+
* @param \Magento\Swatches\Helper\Media $swatchHelper
62+
* @param array $data
63+
*/
64+
public function __construct(
65+
\Magento\Backend\Block\Template\Context $context,
66+
\Magento\Framework\Registry $registry,
67+
\Magento\Eav\Model\Resource\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory,
68+
\Magento\Framework\Validator\UniversalFactory $universalFactory,
69+
\Magento\Catalog\Model\Product\Media\Config $mediaConfig,
70+
\Magento\Swatches\Helper\Media $swatchHelper,
71+
array $data = []
72+
) {
73+
parent::__construct($context, $registry, $attrOptionCollectionFactory, $universalFactory, $data);
74+
$this->swatchHelper = $swatchHelper;
75+
$this->mediaConfig = $mediaConfig;
76+
}
77+
78+
/**
79+
* Create store values
80+
*
81+
* @codeCoverageIgnore
82+
* @param integer $storeId
83+
* @param integer $optionId
84+
* @return array
85+
*/
86+
protected function createStoreValues($storeId, $optionId)
87+
{
88+
$value = [];
89+
$storeValues = $this->getStoreOptionValues($storeId);
90+
$swatchStoreValue = isset($storeValues['swatch']) ? $storeValues['swatch'] : null;
91+
$value['store' . $storeId] = isset($storeValues[$optionId]) ?
92+
$this->escapeHtml($storeValues[$optionId]) : '';
93+
$value['swatch' . $storeId] = isset($swatchStoreValue[$optionId]) ?
94+
$this->escapeHtml($swatchStoreValue[$optionId]) : '';
95+
96+
return $value;
97+
}
98+
99+
/**
100+
* Retrieve attribute option values for given store id
101+
*
102+
* @param int $storeId
103+
* @return array
104+
*/
105+
public function getStoreOptionValues($storeId)
106+
{
107+
$values = $this->getData('store_option_values_' . $storeId);
108+
if ($values === null) {
109+
$values = [];
110+
$valuesCollection = $this->_attrOptionCollectionFactory->create();
111+
$valuesCollection->setAttributeFilter(
112+
$this->getAttributeObject()->getId()
113+
)->setStoreFilter(
114+
$storeId,
115+
false
116+
);
117+
$valuesCollection->getSelect()->joinLeft(
118+
['swatch_table' => $valuesCollection->getTable('eav_attribute_option_swatch')],
119+
'swatch_table.option_id = main_table.option_id AND swatch_table.store_id = '.$storeId,
120+
'swatch_table.value AS label'
121+
);
122+
$valuesCollection->load();
123+
foreach ($valuesCollection as $item) {
124+
$values[$item->getId()] = $item->getValue();
125+
$values['swatch'][$item->getId()] = $item->getLabel();
126+
}
127+
$this->setData('store_option_values_' . $storeId, $values);
128+
}
129+
return $values;
130+
}
131+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Swatches\Block\Adminhtml\Attribute\Edit\Options;
7+
8+
/**
9+
* Block Class for Text Swatch
10+
*/
11+
class Text extends AbstractSwatch
12+
{
13+
/**
14+
* @var string
15+
*/
16+
protected $_template = 'Magento_Swatches::catalog/product/attribute/text.phtml';
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Swatches\Block\Adminhtml\Attribute\Edit\Options;
7+
8+
/**
9+
* Block Class for Visual Swatch
10+
*/
11+
class Visual extends AbstractSwatch
12+
{
13+
/**
14+
* @var string
15+
*/
16+
protected $_template = 'Magento_Swatches::catalog/product/attribute/visual.phtml';
17+
18+
/**
19+
* Create store values
20+
*
21+
* @codeCoverageIgnore
22+
* @param integer $storeId
23+
* @param integer $optionId
24+
* @return array
25+
*/
26+
protected function createStoreValues($storeId, $optionId)
27+
{
28+
$value = [];
29+
$value['store' . $storeId] = '';
30+
$value['defaultswatch' . $storeId] = '';
31+
$value['swatch' . $storeId] = '';
32+
$storeValues = $this->getStoreOptionValues($storeId);
33+
$swatchStoreValue = null;
34+
35+
if (isset($storeValues['swatch'])) {
36+
$swatchStoreValue = $storeValues['swatch'];
37+
}
38+
39+
if (isset($storeValues[$optionId])) {
40+
$value['store' . $storeId] = $this->escapeHtml($storeValues[$optionId]);
41+
}
42+
43+
if (isset($swatchStoreValue[$optionId])) {
44+
$value['defaultswatch' . $storeId] = $this->escapeHtml($swatchStoreValue[$optionId]);
45+
}
46+
47+
$swatchStoreValue = $this->reformatSwatchLabels($swatchStoreValue);
48+
if (isset($swatchStoreValue[$optionId])) {
49+
$value['swatch' . $storeId] = $this->escapeHtml($swatchStoreValue[$optionId]);
50+
}
51+
52+
return $value;
53+
}
54+
55+
/**
56+
* Parse swatch labels for template
57+
*
58+
* @codeCoverageIgnore
59+
* @param null $swatchStoreValue
60+
* @return string
61+
*/
62+
protected function reformatSwatchLabels($swatchStoreValue = null)
63+
{
64+
if ($swatchStoreValue === null) {
65+
return;
66+
}
67+
$newSwatch = '';
68+
foreach ($swatchStoreValue as $key => $value) {
69+
if ($value[0] == '#') {
70+
$newSwatch[$key] = 'background: '.$value;
71+
} elseif ($value[0] == '/') {
72+
$mediaUrl = $this->swatchHelper->getSwatchMediaUrl();
73+
$newSwatch[$key] = 'background: url('.$mediaUrl.$value.'); background-size: cover;';
74+
}
75+
}
76+
return $newSwatch;
77+
}
78+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Swatches\Block\Adminhtml\Product\Attribute\Edit;
8+
9+
use Magento\Swatches\Model\Swatch;
10+
11+
/**
12+
* Class Form
13+
*/
14+
class Form extends \Magento\Framework\Data\Form
15+
{
16+
/**
17+
* @param array $values
18+
* @return $this
19+
*/
20+
public function addValues($values)
21+
{
22+
if (!is_array($values)) {
23+
return $this;
24+
}
25+
$values = array_merge(
26+
$values,
27+
$this->getAdditionalData($values)
28+
);
29+
if (isset($values['frontend_input']) && 'select' == $values['frontend_input']
30+
&& isset($values[Swatch::SWATCH_INPUT_TYPE_KEY])
31+
) {
32+
$values['frontend_input'] = 'swatch_' . $values[Swatch::SWATCH_INPUT_TYPE_KEY];
33+
}
34+
35+
return parent::addValues($values);
36+
}
37+
38+
/**
39+
* @param array $values
40+
* @return array
41+
*/
42+
protected function getAdditionalData(array $values)
43+
{
44+
$additionalData = [];
45+
if (isset($values['additional_data'])) {
46+
$additionalData = unserialize($values['additional_data']);
47+
if (!is_array($additionalData)) {
48+
$additionalData = [];
49+
}
50+
}
51+
52+
return $additionalData;
53+
}
54+
}

0 commit comments

Comments
 (0)