Visual.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Swatches\Block\Adminhtml\Attribute\Edit\Options;
  7. /**
  8. * Block Class for Visual Swatch
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Visual extends AbstractSwatch
  14. {
  15. /**
  16. * @var string
  17. */
  18. protected $_template = 'Magento_Swatches::catalog/product/attribute/visual.phtml';
  19. /**
  20. * Create store values
  21. *
  22. * Method not intended to escape HTML entities
  23. * Escaping will be applied in template files
  24. *
  25. * @param integer $storeId
  26. * @param integer $optionId
  27. * @return array
  28. */
  29. protected function createStoreValues($storeId, $optionId)
  30. {
  31. $value = [];
  32. $value['store' . $storeId] = '';
  33. $value['defaultswatch' . $storeId] = '';
  34. $value['swatch' . $storeId] = '';
  35. $storeValues = $this->getStoreOptionValues($storeId);
  36. $swatchStoreValue = null;
  37. if (isset($storeValues['swatch'])) {
  38. $swatchStoreValue = $storeValues['swatch'];
  39. }
  40. if (isset($storeValues[$optionId])) {
  41. $value['store' . $storeId] = $storeValues[$optionId];
  42. }
  43. if (isset($swatchStoreValue[$optionId])) {
  44. $value['defaultswatch' . $storeId] = $swatchStoreValue[$optionId];
  45. }
  46. $swatchStoreValue = $this->reformatSwatchLabels($swatchStoreValue);
  47. if (isset($swatchStoreValue[$optionId])) {
  48. $value['swatch' . $storeId] = $swatchStoreValue[$optionId];
  49. }
  50. return $value;
  51. }
  52. /**
  53. * Return json config for visual option JS initialization
  54. *
  55. * @return array
  56. * @since 100.1.0
  57. */
  58. public function getJsonConfig()
  59. {
  60. $values = [];
  61. foreach ($this->getOptionValues() as $value) {
  62. $values[] = $value->getData();
  63. }
  64. $data = [
  65. 'attributesData' => $values,
  66. 'uploadActionUrl' => $this->getUrl('swatches/iframe/show'),
  67. 'isSortable' => (int)(!$this->getReadOnly() && !$this->canManageOptionDefaultOnly()),
  68. 'isReadOnly' => (int)$this->getReadOnly()
  69. ];
  70. return json_encode($data);
  71. }
  72. /**
  73. * Parse swatch labels for template
  74. *
  75. * @codeCoverageIgnore
  76. * @param null|array $swatchStoreValue
  77. * @return null|array
  78. */
  79. protected function reformatSwatchLabels($swatchStoreValue = null)
  80. {
  81. if ($swatchStoreValue === null) {
  82. return;
  83. }
  84. $newSwatch = [];
  85. foreach ($swatchStoreValue as $key => $value) {
  86. if ($value[0] == '#') {
  87. $newSwatch[$key] = 'background: '.$value;
  88. } elseif ($value[0] == '/') {
  89. $mediaUrl = $this->swatchHelper->getSwatchMediaUrl();
  90. $newSwatch[$key] = 'background: url('.$mediaUrl.$value.'); background-size: cover;';
  91. }
  92. }
  93. return $newSwatch;
  94. }
  95. }