|
4 | 4 | * See COPYING.txt for license details. |
5 | 5 | */ |
6 | 6 |
|
| 7 | +use Magento\Framework\Escaper; |
| 8 | +use Magento\Framework\View\Helper\SecureHtmlRenderer; |
| 9 | +use Magento\Swatches\Block\LayeredNavigation\RenderLayered; |
| 10 | +use Magento\Swatches\ViewModel\Product\Renderer\Configurable as ConfigurableViewModel; |
| 11 | + |
7 | 12 | // phpcs:disable PSR2.ControlStructures.SwitchDeclaration |
8 | 13 | // phpcs:disable Generic.WhiteSpace.ScopeIndent |
9 | 14 |
|
10 | | -/** @var $block \Magento\Swatches\Block\LayeredNavigation\RenderLayered */ |
11 | | -/** @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer */ |
12 | | -/** @var \Magento\Framework\Escaper $escaper */ |
| 15 | +/** @var RenderLayered $block */ |
| 16 | +/** @var SecureHtmlRenderer $secureRenderer */ |
| 17 | +/** @var Escaper $escaper */ |
13 | 18 | ?> |
14 | 19 | <?php $swatchData = $block->getSwatchData(); ?> |
15 | | -<div class="swatch-attribute swatch-layered <?= $block->escapeHtmlAttr($swatchData['attribute_code']) ?>" |
16 | | - data-attribute-code="<?= $block->escapeHtmlAttr($swatchData['attribute_code']) ?>" |
17 | | - data-attribute-id="<?= $block->escapeHtmlAttr($swatchData['attribute_id']) ?>"> |
| 20 | +<?php /** @var ConfigurableViewModel $configurableViewModel */ ?> |
| 21 | +<?php $configurableViewModel = $block->getConfigurableViewModel() ?> |
| 22 | +<div class="swatch-attribute swatch-layered <?= $escaper->escapeHtmlAttr($swatchData['attribute_code']) ?>" |
| 23 | + data-attribute-code="<?= $escaper->escapeHtmlAttr($swatchData['attribute_code']) ?>" |
| 24 | + data-attribute-id="<?= $escaper->escapeHtmlAttr($swatchData['attribute_id']) ?>"> |
18 | 25 | <div class="swatch-attribute-options clearfix"> |
19 | 26 | <?php foreach ($swatchData['options'] as $option => $label): ?> |
20 | | - <a href="<?= $block->escapeUrl($label['link']) ?>" rel="nofollow" |
21 | | - aria-label="<?= $block->escapeHtmlAttr($label['label']) ?>" |
| 27 | + <a href="<?= $escaper->escapeUrl($label['link']) ?>" rel="nofollow" |
| 28 | + aria-label="<?= $escaper->escapeHtmlAttr($label['label']) ?>" |
22 | 29 | class="swatch-option-link-layered"> |
23 | 30 | <?php if (isset($swatchData['swatches'][$option]['type'])): ?> |
24 | 31 | <?php switch ($swatchData['swatches'][$option]['type']) { |
25 | 32 | case '3': |
26 | 33 | ?> |
27 | | - <div class="swatch-option <?= $block->escapeHtmlAttr($label['custom_style']) ?>" |
| 34 | + <div class="swatch-option <?= $escaper->escapeHtmlAttr($label['custom_style']) ?>" |
28 | 35 | tabindex="-1" |
29 | 36 | data-option-type="3" |
30 | | - data-option-id="<?= $block->escapeHtmlAttr($option) ?>" |
31 | | - data-option-label="<?= $block->escapeHtmlAttr($label['label']) ?>" |
| 37 | + data-option-id="<?= $escaper->escapeHtmlAttr($option) ?>" |
| 38 | + data-option-label="<?= $escaper->escapeHtmlAttr($label['label']) ?>" |
32 | 39 | data-option-tooltip-thumb="" |
33 | 40 | data-option-tooltip-value="" |
34 | 41 | ></div> |
|
43 | 50 | 'swatch_image', |
44 | 51 | $swatchData['swatches'][$option]['value'] |
45 | 52 | ); |
46 | | - $escapedUrl = $block->escapeUrl($swatchImagePath); |
| 53 | + $escapedUrl = $escaper->escapeUrl($swatchImagePath); |
47 | 54 | ?> |
48 | | - <div class="swatch-option image <?= $block->escapeHtmlAttr($label['custom_style']) ?>" |
| 55 | + <div class="swatch-option image <?= $escaper->escapeHtmlAttr($label['custom_style']) ?>" |
49 | 56 | tabindex="-1" |
50 | 57 | data-option-type="2" |
51 | | - data-option-id="<?= $block->escapeHtmlAttr($option) ?>" |
52 | | - data-option-label="<?= $block->escapeHtmlAttr($label['label']) ?>" |
53 | | - data-option-tooltip-thumb="<?= $block->escapeUrl($swatchThumbPath) ?>" |
| 58 | + data-option-id="<?= $escaper->escapeHtmlAttr($option) ?>" |
| 59 | + data-option-label="<?= $escaper->escapeHtmlAttr($label['label']) ?>" |
| 60 | + data-option-tooltip-thumb="<?= $escaper->escapeUrl($swatchThumbPath) ?>" |
54 | 61 | data-option-tooltip-value=""> |
55 | 62 | </div> |
56 | 63 | <?php |
57 | | - $element = 'swatchImageOption' .$escaper->escapeJs($option); |
58 | | - $script = 'var ' .$element |
59 | | - .' = document.querySelector(\'div[data-option-id="' .$escaper->escapeJs($option) |
60 | | - .'"]\');' .PHP_EOL; |
61 | | - $script .= $element .'.style.background = "url(\'' |
62 | | - .$escapedUrl .'\') no-repeat center";' .PHP_EOL; |
63 | | - $script .= $element .'.style.backgroundSize = "initial";'; |
| 64 | + $element = 'swatchImageOption' . $escaper->escapeJs($option); |
| 65 | + $script = 'var ' . $element |
| 66 | + . ' = document.querySelector(\'div[data-option-id="' . $escaper->escapeJs($option) |
| 67 | + . '"]\');' . PHP_EOL; |
| 68 | + $script .= $element . '.style.background = "url(\'' |
| 69 | + . $escapedUrl . '\') no-repeat center";' . PHP_EOL; |
| 70 | + $script .= $element . '.style.backgroundSize = "initial";'; |
64 | 71 | ?> |
65 | | - <?= /* @noEscape*/ $secureRenderer->renderTag('script', [], $script, false); ?> |
| 72 | + <?= /* @noEscape*/ $secureRenderer->renderTag('script', [], $script, false) ?> |
66 | 73 | <?php break; |
67 | 74 | case '1': |
68 | 75 | ?> |
69 | | - <div class="swatch-option color <?= $block->escapeHtmlAttr($label['custom_style']) ?>" |
| 76 | + <div class="swatch-option color <?= $escaper->escapeHtmlAttr($label['custom_style']) ?>" |
70 | 77 | tabindex="-1" |
71 | 78 | data-option-type="1" |
72 | | - data-option-id="<?= $block->escapeHtmlAttr($option) ?>" |
73 | | - data-option-label="<?= $block->escapeHtmlAttr($label['label']) ?>" |
| 79 | + data-option-id="<?= $escaper->escapeHtmlAttr($option) ?>" |
| 80 | + data-option-label="<?= $escaper->escapeHtmlAttr($label['label']) ?>" |
74 | 81 | data-option-tooltip-thumb="" |
75 | | - data-option-tooltip-value="<?= $block->escapeHtmlAttr( |
| 82 | + data-option-tooltip-value="<?= $escaper->escapeHtmlAttr( |
76 | 83 | $swatchData['swatches'][$option]['value'] |
77 | 84 | ) ?>"> |
78 | 85 | </div> |
79 | 86 | <?php |
80 | | - $element = 'swatchImageOption' .$escaper->escapeJs($option); |
| 87 | + $element = 'swatchImageOption' . $escaper->escapeJs($option); |
81 | 88 | $backgroundValue = $escaper->escapeJs( |
82 | 89 | str_replace('\'', '\\\'', $swatchData['swatches'][$option]['value']) |
83 | 90 | ); |
84 | | - $script = 'var ' .$element |
85 | | - .' = document.querySelector(\'div[data-option-id="' .$escaper->escapeJs($option) |
86 | | - .'"]\');' .PHP_EOL; |
87 | | - $script .= $element .'.style.background = "' .$backgroundValue |
88 | | - .' no-repeat center";' .PHP_EOL; |
89 | | - $script .= $element .'.style.backgroundSize = "initial";'; |
| 91 | + $script = 'var ' . $element |
| 92 | + . ' = document.querySelector(\'div[data-option-id="' . $escaper->escapeJs($option) |
| 93 | + . '"]\');' . PHP_EOL; |
| 94 | + $script .= $element . '.style.background = "' . $backgroundValue |
| 95 | + . ' no-repeat center";' . PHP_EOL; |
| 96 | + $script .= $element . '.style.backgroundSize = "initial";'; |
90 | 97 | ?> |
91 | | - <?= /* @noEscape*/ $secureRenderer->renderTag('script', [], $script, false); ?> |
| 98 | + <?= /* @noEscape*/ $secureRenderer->renderTag('script', [], $script, false) ?> |
92 | 99 | <?php break; |
93 | 100 | case '0': |
94 | 101 | default: |
95 | 102 | ?> |
96 | | - <div class="swatch-option text <?= $block->escapeHtmlAttr($label['custom_style']) ?>" |
| 103 | + <div class="swatch-option text <?= $escaper->escapeHtmlAttr($label['custom_style']) ?>" |
97 | 104 | tabindex="-1" |
98 | 105 | data-option-type="0" |
99 | | - data-option-id="<?= $block->escapeHtmlAttr($option) ?>" |
100 | | - data-option-label="<?= $block->escapeHtmlAttr($label['label']) ?>" |
| 106 | + data-option-id="<?= $escaper->escapeHtmlAttr($option) ?>" |
| 107 | + data-option-label="<?= $escaper->escapeHtmlAttr($label['label']) ?>" |
101 | 108 | data-option-tooltip-thumb="" |
102 | 109 | data-option-tooltip-value="" |
103 | | - ><?= $block->escapeHtml($swatchData['swatches'][$option]['value']) ?></div> |
| 110 | + ><?= $escaper->escapeHtml($swatchData['swatches'][$option]['value']) ?></div> |
104 | 111 | <?php break; |
105 | 112 | } ?> |
106 | 113 | <?php endif; ?> |
107 | 114 | </a> |
108 | 115 | <?php endforeach; ?> |
109 | 116 | </div> |
110 | 117 | </div> |
111 | | -<?php $scriptString = <<<script |
112 | | -
|
113 | | - require(["jquery", "Magento_Swatches/js/swatch-renderer"], function ($) { |
114 | | - $('.swatch-layered.{$block->escapeJs($swatchData['attribute_code'])}') |
115 | | - .find('[data-option-type="1"], [data-option-type="2"], [data-option-type="0"], [data-option-type="3"]') |
116 | | - .SwatchRendererTooltip(); |
117 | | - }); |
118 | | -
|
119 | | -script; |
120 | | -?> |
121 | | -<?= /* @noEscape */ $secureRenderer->renderTag('script', [], $scriptString, false) ?> |
| 118 | +<script type="text/x-magento-init"> |
| 119 | + { |
| 120 | + ".swatch-layered.<?= $escaper->escapeJs($swatchData['attribute_code']) ?>": { |
| 121 | + "Magento_Swatches/js/layered/swatch-renderer": { |
| 122 | + "showTooltip": <?= $escaper->escapeJs($configurableViewModel->getShowSwatchTooltip()) ?> |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | +</script> |
0 commit comments