Skip to content

Commit 10cc12a

Browse files
committed
Migrate helper to ViewModel
1 parent 98e1159 commit 10cc12a

File tree

3 files changed

+87
-9
lines changed

3 files changed

+87
-9
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Customer\ViewModel;
8+
9+
use Magento\Directory\Helper\Data as DataHelper;
10+
use Magento\Customer\Helper\Address as AddressHelper;
11+
use Magento\Framework\View\Element\Block\ArgumentInterface;
12+
13+
/**
14+
* Customer address view model.
15+
*/
16+
17+
class Address implements ArgumentInterface
18+
{
19+
/**
20+
* Data helper
21+
*
22+
* @var DataHelper
23+
*/
24+
private $helperData;
25+
26+
/**
27+
* Address helper
28+
*
29+
* @var AddressHelper
30+
*/
31+
private $helperAddress;
32+
33+
/**
34+
* @param Data $helperData
35+
* @param Address $helperAddress
36+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
37+
*/
38+
public function __construct(
39+
Data $helperData,
40+
Address $helperAddress
41+
) {
42+
$this->helperData= $helperData;
43+
$this->helperAddress= $helperAddress;
44+
}
45+
46+
public function dataGetAttributeValidationClass($param)
47+
{
48+
return $this->dataAddress->getAttributeValidationClass($param);
49+
}
50+
51+
public function addressGetAttributeValidationClass($param)
52+
{
53+
return $this->helperAddress->getAttributeValidationClass($param);
54+
}
55+
56+
public function addressGetStreetLines()
57+
{
58+
return $this->helperAddress->getStreetLines();
59+
}
60+
61+
public function addressIsVatAttributeVisible()
62+
{
63+
return $this->helperAddress->isVatAttributeVisible();
64+
}
65+
66+
public function dataGetRegionJson()
67+
{
68+
return $this->helperData->getRegionJson();
69+
}
70+
71+
public function dataGetCountriesWithOptionalZip()
72+
{
73+
return $this->helperData->getCountriesWithOptionalZip();
74+
}
75+
}

app/code/Magento/Customer/view/frontend/layout/customer_address_form.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<arguments>
2222
<argument name="attribute_data" xsi:type="object">Magento\Customer\Block\DataProviders\AddressAttributeData</argument>
2323
<argument name="post_code_config" xsi:type="object">Magento\Customer\Block\DataProviders\PostCodesPatternsAttributeData</argument>
24+
<argument name="view_model" xsi:type="object">Magento\Customer\ViewModel\Data</argument>
2425
</arguments>
2526
</block>
2627
</referenceContainer>

app/code/Magento/Customer/view/frontend/templates/address/edit.phtml

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66

77
/** @var \Magento\Customer\Block\Address\Edit $block */
8+
/** @var \Magento\Customer\ViewModel\Address $viewModel */
9+
$viewModel = $block->getData('viewModel');
810
?>
911
<?php $_company = $block->getLayout()->createBlock(\Magento\Customer\Block\Widget\Company::class) ?>
1012
<?php $_telephone = $block->getLayout()->createBlock(\Magento\Customer\Block\Widget\Telephone::class) ?>
@@ -19,13 +21,13 @@
1921
<?php $_dataHelper = $this->helper(\Magento\Directory\Helper\Data::class); ?>
2022
<?php $_addressHelper = $this->helper(\Magento\Customer\Helper\Address::class); ?>
2123

22-
<?php $_vatidValidationClass = $_addressHelper->getAttributeValidationClass('vat_id'); ?>
23-
<?php $_cityValidationClass = $_addressHelper->getAttributeValidationClass('city'); ?>
24-
<?php $_postcodeValidationClass_value = $_addressHelper->getAttributeValidationClass('postcode'); ?>
24+
<?php $_vatidValidationClass = $viewModel->addressGetAttributeValidationClass('vat_id'); ?>
25+
<?php $_cityValidationClass = $viewModel->addressGetAttributeValidationClass('city'); ?>
26+
<?php $_postcodeValidationClass_value = $viewModel->addressGetAttributeValidationClass('postcode'); ?>
2527
<?php $_postcodeValidationClass = $_postcodeValidationClass_value; ?>
26-
<?php $_streetValidationClass = $_addressHelper->getAttributeValidationClass('street'); ?>
28+
<?php $_streetValidationClass = $viewModel->addressGetAttributeValidationClass('street'); ?>
2729
<?php $_streetValidationClass = trim(str_replace('required-entry', '', $_streetValidationClass)); ?>
28-
<?php $_regionValidationClass = $_addressHelper->getAttributeValidationClass('region'); ?>
30+
<?php $_regionValidationClass = $viewModel->addressGetAttributeValidationClass('region'); ?>
2931

3032
<form class="form-address-edit"
3133
action="<?= $block->escapeUrl($block->getSaveUrl()) ?>"
@@ -65,7 +67,7 @@
6567
id="street_1"
6668
class="input-text <?= $block->escapeHtmlAttr($_streetValidationClass) ?>"/>
6769
<div class="nested">
68-
<?php for ($_i = 1, $_n = $_addressHelper->getStreetLines(); $_i < $_n; $_i++): ?>
70+
<?php for ($_i = 1, $_n = $viewModel->addressGetStreetLines(); $_i < $_n; $_i++): ?>
6971
<div class="field additional">
7072
<label class="label" for="street_<?= /* @noEscape */ $_i + 1 ?>">
7173
<span><?= $block->escapeHtml(__('Street Address %1', $_i + 1)) ?></span>
@@ -83,7 +85,7 @@
8385
</div>
8486
</div>
8587

86-
<?php if ($_addressHelper->isVatAttributeVisible()): ?>
88+
<?php if ($viewModel->addressGetisVatAttributeVisible()): ?>
8789
<div class="field taxvat">
8890
<label class="label" for="vat_id">
8991
<span><?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('vat_id') ?></span>
@@ -214,9 +216,9 @@
214216
"regionInputId": "#region",
215217
"postcodeId": "#zip",
216218
"form": "#form-validate",
217-
"regionJson": <?= /* @noEscape */ $_dataHelper->getRegionJson() ?>,
219+
"regionJson": <?= /* @noEscape */ $viewModel->dataGetRegionJson() ?>,
218220
"defaultRegion": "<?= (int) $block->getRegionId() ?>",
219-
"countriesWithOptionalZip": <?= /* @noEscape */ $_dataHelper->getCountriesWithOptionalZip(true) ?>
221+
"countriesWithOptionalZip": <?= /* @noEscape */ $viewModel->dataGetCountriesWithOptionalZip(true) ?>
220222
}
221223
}
222224
}

0 commit comments

Comments
 (0)