Skip to content

Commit 2a4a222

Browse files
committed
add view model
1 parent f463caf commit 2a4a222

File tree

10 files changed

+122
-34
lines changed

10 files changed

+122
-34
lines changed

app/code/Magento/Swatches/Block/Product/Renderer/Configurable.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,6 @@ class Configurable extends \Magento\ConfigurableProduct\Block\Product\View\Type\
6363
*/
6464
private const XML_PATH_SWATCHES_PER_PRODUCT = 'catalog/frontend/swatches_per_product';
6565

66-
/**
67-
* Config path if swatch tooltips are enabled
68-
*/
69-
private const XML_PATH_SHOW_SWATCH_TOOLTIP = 'catalog/frontend/show_swatch_tooltip';
70-
7166
/**
7267
* @var Product
7368
*/
@@ -216,19 +211,6 @@ public function getNumberSwatchesPerProduct()
216211
);
217212
}
218213

219-
/**
220-
* Get config if swatch tooltips should be rendered.
221-
*
222-
* @return string
223-
*/
224-
public function getShowSwatchTooltip()
225-
{
226-
return $this->_scopeConfig->getValue(
227-
self::XML_PATH_SHOW_SWATCH_TOOLTIP,
228-
ScopeInterface::SCOPE_STORE
229-
);
230-
}
231-
232214
/**
233215
* Set product to block
234216
*
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types = 1);
7+
namespace Magento\Swatches\ViewModel\Product\Renderer;
8+
9+
use Magento\Framework\App\Config\ScopeConfigInterface;
10+
use Magento\Framework\View\Element\Block\ArgumentInterface;
11+
use Magento\Store\Model\ScopeInterface;
12+
13+
/**
14+
* Class Configurable
15+
*/
16+
class Configurable implements ArgumentInterface
17+
{
18+
/**
19+
* Config path if swatch tooltips are enabled
20+
*/
21+
private const XML_PATH_SHOW_SWATCH_TOOLTIP = 'catalog/frontend/show_swatch_tooltip';
22+
23+
/**
24+
* @var ScopeConfigInterface
25+
*/
26+
private $scopeConfig;
27+
28+
/**
29+
* Configurable constructor.
30+
*
31+
* @param ScopeConfigInterface $scopeConfig
32+
*/
33+
public function __construct(ScopeConfigInterface $scopeConfig)
34+
{
35+
$this->scopeConfig = $scopeConfig;
36+
}
37+
38+
/**
39+
* Get config if swatch tooltips should be rendered.
40+
*
41+
* @return string
42+
*/
43+
public function getShowSwatchTooltip()
44+
{
45+
return $this->scopeConfig->getValue(
46+
self::XML_PATH_SHOW_SWATCH_TOOLTIP,
47+
ScopeInterface::SCOPE_STORE
48+
);
49+
}
50+
}

app/code/Magento/Swatches/view/frontend/layout/catalog_category_view.xml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,19 @@
55
* See COPYING.txt for license details.
66
*/
77
-->
8-
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
8+
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
910
<body>
1011
<referenceBlock name="category.product.type.details.renderers">
11-
<block class="Magento\Swatches\Block\Product\Renderer\Listing\Configurable" name="category.product.type.details.renderers.configurable" as="configurable" template="Magento_Swatches::product/listing/renderer.phtml" ifconfig="catalog/frontend/show_swatches_in_product_list" />
12+
<block class="Magento\Swatches\Block\Product\Renderer\Listing\Configurable"
13+
name="category.product.type.details.renderers.configurable" as="configurable"
14+
template="Magento_Swatches::product/listing/renderer.phtml"
15+
ifconfig="catalog/frontend/show_swatches_in_product_list">
16+
<arguments>
17+
<argument name="configurable_view_model"
18+
xsi:type="object">Magento\Swatches\ViewModel\Product\Renderer\Configurable</argument>
19+
</arguments>
20+
</block>
1221
</referenceBlock>
1322
</body>
1423
</page>

app/code/Magento/Swatches/view/frontend/layout/catalog_product_view_type_configurable.xml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,18 @@
55
* See COPYING.txt for license details.
66
*/
77
-->
8-
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
8+
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
910
<body>
1011
<referenceContainer name="product.info.options.configurable" remove="true"/>
1112
<referenceBlock name="product.info.options.wrapper">
12-
<block class="Magento\Swatches\Block\Product\Renderer\Configurable" name="product.info.options.swatches" as="swatch_options" before="-" />
13+
<block class="Magento\Swatches\Block\Product\Renderer\Configurable" name="product.info.options.swatches"
14+
as="swatch_options" before="-">
15+
<arguments>
16+
<argument name="configurable_view_model"
17+
xsi:type="object">Magento\Swatches\ViewModel\Product\Renderer\Configurable</argument>
18+
</arguments>
19+
</block>
1320
</referenceBlock>
1421
</body>
1522
</page>

app/code/Magento/Swatches/view/frontend/layout/catalog_widget_product_list.xml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,19 @@
33
~ See COPYING.txt for license details.
44
-->
55

6-
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
6+
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
7+
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
78
<body>
89
<referenceBlock name="category.product.type.widget.details.renderers">
9-
<block class="Magento\Swatches\Block\Product\Renderer\Listing\Configurable" name="category.product.type.details.renderers.configurable" as="configurable" template="Magento_Swatches::product/listing/renderer.phtml" ifconfig="catalog/frontend/show_swatches_in_product_list"/>
10+
<block class="Magento\Swatches\Block\Product\Renderer\Listing\Configurable"
11+
name="category.product.type.details.renderers.configurable" as="configurable"
12+
template="Magento_Swatches::product/listing/renderer.phtml"
13+
ifconfig="catalog/frontend/show_swatches_in_product_list">
14+
<arguments>
15+
<argument name="configurable_view_model"
16+
xsi:type="object">Magento\Swatches\ViewModel\Product\Renderer\Configurable</argument>
17+
</arguments>
18+
</block>
1019
</referenceBlock>
1120
</body>
12-
</page>
21+
</page>

app/code/Magento/Swatches/view/frontend/layout/catalogsearch_advanced_result.xml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,19 @@
55
* See COPYING.txt for license details.
66
*/
77
-->
8-
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
8+
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
910
<body>
1011
<referenceBlock name="category.product.type.details.renderers">
11-
<block class="Magento\Swatches\Block\Product\Renderer\Listing\Configurable" name="category.product.type.details.renderers.configurable" as="configurable" template="Magento_Swatches::product/listing/renderer.phtml" ifconfig="catalog/frontend/show_swatches_in_product_list" />
12+
<block class="Magento\Swatches\Block\Product\Renderer\Listing\Configurable"
13+
name="category.product.type.details.renderers.configurable" as="configurable"
14+
template="Magento_Swatches::product/listing/renderer.phtml"
15+
ifconfig="catalog/frontend/show_swatches_in_product_list">
16+
<arguments>
17+
<argument name="configurable_view_model"
18+
xsi:type="object">Magento\Swatches\ViewModel\Product\Renderer\Configurable</argument>
19+
</arguments>
20+
</block>
1221
</referenceBlock>
1322
</body>
1423
</page>

app/code/Magento/Swatches/view/frontend/layout/catalogsearch_result_index.xml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,19 @@
55
* See COPYING.txt for license details.
66
*/
77
-->
8-
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
8+
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
910
<body>
1011
<referenceBlock name="category.product.type.details.renderers">
11-
<block class="Magento\Swatches\Block\Product\Renderer\Listing\Configurable" name="category.product.type.details.renderers.configurable" as="configurable" template="Magento_Swatches::product/listing/renderer.phtml" ifconfig="catalog/frontend/show_swatches_in_product_list"/>
12+
<block class="Magento\Swatches\Block\Product\Renderer\Listing\Configurable"
13+
name="category.product.type.details.renderers.configurable" as="configurable"
14+
template="Magento_Swatches::product/listing/renderer.phtml"
15+
ifconfig="catalog/frontend/show_swatches_in_product_list">
16+
<arguments>
17+
<argument name="configurable_view_model"
18+
xsi:type="object">Magento\Swatches\ViewModel\Product\Renderer\Configurable</argument>
19+
</arguments>
20+
</block>
1221
</referenceBlock>
1322
</body>
1423
</page>

app/code/Magento/Swatches/view/frontend/layout/wishlist_index_configure_type_configurable.xml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,18 @@
55
* See COPYING.txt for license details.
66
*/
77
-->
8-
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
8+
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
910
<body>
1011
<referenceBlock name="product.info.options.configurable" remove="true"/>
1112
<referenceBlock name="product.info.options.wrapper">
12-
<block class="Magento\Swatches\Block\Product\Renderer\Configurable" name="product.info.options.swatches" as="swatch_options" before="-" />
13+
<block class="Magento\Swatches\Block\Product\Renderer\Configurable" name="product.info.options.swatches"
14+
as="swatch_options" before="-">
15+
<arguments>
16+
<argument name="configurable_view_model"
17+
xsi:type="object">Magento\Swatches\ViewModel\Product\Renderer\Configurable</argument>
18+
</arguments>
19+
</block>
1320
</referenceBlock>
1421
</body>
1522
</page>

app/code/Magento/Swatches/view/frontend/templates/product/listing/renderer.phtml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
<?php
88
/** @var $block \Magento\Swatches\Block\Product\Renderer\Listing\Configurable */
99
$productId = $block->getProduct()->getId();
10+
/** @var \Magento\Swatches\ViewModel\Product\Renderer\Configurable $configurableViewModel */
11+
$configurableViewModel = $block->getConfigurableViewModel()
1012
?>
1113
<div class="swatch-opt-<?= $block->escapeHtmlAttr($productId) ?>"
1214
data-role="swatch-option-<?= $block->escapeHtmlAttr($productId) ?>"></div>
@@ -23,7 +25,7 @@ $productId = $block->getProduct()->getId();
2325
"jsonSwatchConfig": <?= /* @noEscape */ $block->getJsonSwatchConfig() ?>,
2426
"mediaCallback": "<?= $block->escapeJs($block->escapeUrl($block->getMediaCallback())) ?>",
2527
"jsonSwatchImageSizeConfig": <?= /* @noEscape */ $block->getJsonSwatchSizeConfig() ?>,
26-
"showTooltip": <?= $block->escapeJs($block->getShowSwatchTooltip()) ?>
28+
"showTooltip": <?= $block->escapeJs($configurableViewModel->getShowSwatchTooltip()) ?>
2729
}
2830
}
2931
}

app/code/Magento/Swatches/view/frontend/templates/product/view/renderer.phtml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
* See COPYING.txt for license details.
55
*/
66
?>
7-
<?php /** @var $block \Magento\Swatches\Block\Product\Renderer\Configurable */ ?>
7+
<?php
8+
/** @var $block \Magento\Swatches\Block\Product\Renderer\Configurable */
9+
/** @var \Magento\Swatches\ViewModel\Product\Renderer\Configurable $configurableViewModel */
10+
$configurableViewModel = $block->getConfigurableViewModel()
11+
?>
812
<div class="swatch-opt" data-role="swatch-options"></div>
913

1014
<script type="text/x-magento-init">
@@ -16,7 +20,7 @@
1620
"mediaCallback": "<?= $block->escapeJs($block->escapeUrl($block->getMediaCallback())) ?>",
1721
"gallerySwitchStrategy": "<?= $block->escapeJs($block->getVar('gallery_switch_strategy', 'Magento_ConfigurableProduct')) ?: 'replace'; ?>",
1822
"jsonSwatchImageSizeConfig": <?= /* @noEscape */ $block->getJsonSwatchSizeConfig() ?>,
19-
"showTooltip": <?= $block->escapeJs($block->getShowSwatchTooltip()) ?>
23+
"showTooltip": <?= $block->escapeJs($configurableViewModel->getShowSwatchTooltip()) ?>
2024
}
2125
},
2226
"*" : {

0 commit comments

Comments
 (0)