Skip to content

Commit d9028a0

Browse files
committed
Merge remote-tracking branch 'af/MC-13877' into MC-13852
2 parents 2ec1888 + 639e097 commit d9028a0

File tree

3 files changed

+54
-60
lines changed

3 files changed

+54
-60
lines changed

app/code/Magento/Customer/Block/Address/Edit.php

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Customer\Block\Address;
77

8+
use Magento\Framework\Exception\LocalizedException;
89
use Magento\Framework\Exception\NoSuchEntityException;
910

1011
/**
@@ -46,6 +47,11 @@ class Edit extends \Magento\Directory\Block\Data
4647
*/
4748
protected $dataObjectHelper;
4849

50+
/**
51+
* @var \Magento\Customer\Api\AddressMetadataInterface
52+
*/
53+
private $addressMetadata;
54+
4955
/**
5056
* Constructor
5157
*
@@ -61,6 +67,7 @@ class Edit extends \Magento\Directory\Block\Data
6167
* @param \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer
6268
* @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
6369
* @param array $data
70+
* @param \Magento\Customer\Api\AddressMetadataInterface|null $addressMetadata
6471
*
6572
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
6673
*/
@@ -76,13 +83,15 @@ public function __construct(
7683
\Magento\Customer\Api\Data\AddressInterfaceFactory $addressDataFactory,
7784
\Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer,
7885
\Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
79-
array $data = []
86+
array $data = [],
87+
\Magento\Customer\Api\AddressMetadataInterface $addressMetadata = null
8088
) {
8189
$this->_customerSession = $customerSession;
8290
$this->_addressRepository = $addressRepository;
8391
$this->addressDataFactory = $addressDataFactory;
8492
$this->currentCustomer = $currentCustomer;
8593
$this->dataObjectHelper = $dataObjectHelper;
94+
$this->addressMetadata = $addressMetadata;
8695
parent::__construct(
8796
$context,
8897
$directoryHelper,
@@ -103,6 +112,32 @@ protected function _prepareLayout()
103112
{
104113
parent::_prepareLayout();
105114

115+
$this->initAddressObject();
116+
117+
$this->pageConfig->getTitle()->set($this->getTitle());
118+
119+
if ($postedData = $this->_customerSession->getAddressFormData(true)) {
120+
$postedData['region'] = [
121+
'region_id' => isset($postedData['region_id']) ? $postedData['region_id'] : null,
122+
'region' => $postedData['region'],
123+
];
124+
$this->dataObjectHelper->populateWithArray(
125+
$this->_address,
126+
$postedData,
127+
\Magento\Customer\Api\Data\AddressInterface::class
128+
);
129+
}
130+
$this->precheckRequiredAttributes();
131+
return $this;
132+
}
133+
134+
/**
135+
* Initialize address object.
136+
*
137+
* @return void
138+
*/
139+
private function initAddressObject()
140+
{
106141
// Init address object
107142
if ($addressId = $this->getRequest()->getParam('id')) {
108143
try {
@@ -124,22 +159,26 @@ protected function _prepareLayout()
124159
$this->_address->setLastname($customer->getLastname());
125160
$this->_address->setSuffix($customer->getSuffix());
126161
}
162+
}
127163

128-
$this->pageConfig->getTitle()->set($this->getTitle());
129-
130-
if ($postedData = $this->_customerSession->getAddressFormData(true)) {
131-
$postedData['region'] = [
132-
'region_id' => isset($postedData['region_id']) ? $postedData['region_id'] : null,
133-
'region' => $postedData['region'],
134-
];
135-
$this->dataObjectHelper->populateWithArray(
136-
$this->_address,
137-
$postedData,
138-
\Magento\Customer\Api\Data\AddressInterface::class
139-
);
164+
/**
165+
* Precheck attributes that may be required in attribute configuration.
166+
*
167+
* @return void
168+
*/
169+
private function precheckRequiredAttributes()
170+
{
171+
$precheckAttributes = $this->getData('check_attributes_on_render');
172+
$requiredAttributesPrechecked = [];
173+
if (!empty($precheckAttributes) && is_array($precheckAttributes)) {
174+
foreach ($precheckAttributes as $attributeCode) {
175+
$attributeMetadata = $this->addressMetadata->getAttributeMetadata($attributeCode);
176+
if ($attributeMetadata && $attributeMetadata->isRequired()) {
177+
$requiredAttributesPrechecked[$attributeCode] = $attributeCode;
178+
}
179+
}
140180
}
141-
142-
return $this;
181+
$this->setData('required_attributes_prechecked', $requiredAttributesPrechecked);
143182
}
144183

145184
/**

app/code/Magento/Customer/Helper/Address.php

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -417,23 +417,4 @@ public function isAttributeVisible($code)
417417
}
418418
return false;
419419
}
420-
421-
/**
422-
* Retrieve attribute required
423-
*
424-
* @param string $code
425-
* @return bool
426-
* @throws NoSuchEntityException
427-
* @throws \Magento\Framework\Exception\LocalizedException
428-
*/
429-
public function isAttributeRequired($code)
430-
{
431-
$attributeMetadata = $this->_addressMetadataService->getAttributeMetadata($code);
432-
433-
if ($attributeMetadata) {
434-
return $attributeMetadata->isRequired();
435-
}
436-
437-
return false;
438-
}
439420
}

app/code/Magento/Customer/Test/Unit/Helper/AddressTest.php

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -415,32 +415,6 @@ public function isAttributeVisibleDataProvider()
415415
];
416416
}
417417

418-
/**
419-
* Test is required filed by attribute code
420-
*
421-
* @param string $attributeCode
422-
* @param bool $isMetadataExists
423-
* @dataProvider isAttributeRequiredDataProvider
424-
* @covers \Magento\Customer\Helper\Address::isAttributeRequired()
425-
* @return void
426-
*/
427-
public function testIsAttributeRequired($attributeCode, $isMetadataExists)
428-
{
429-
$attributeMetadata = null;
430-
if ($isMetadataExists) {
431-
$attributeMetadata = $this->getMockBuilder(\Magento\Customer\Api\Data\AttributeMetadataInterface::class)
432-
->getMockForAbstractClass();
433-
$attributeMetadata->expects($this->once())
434-
->method('isRequired')
435-
->willReturn(true);
436-
}
437-
$this->addressMetadataService->expects($this->once())
438-
->method('getAttributeMetadata')
439-
->with($attributeCode)
440-
->willReturn($attributeMetadata);
441-
$this->assertEquals($isMetadataExists, $this->helper->isAttributeRequired($attributeCode));
442-
}
443-
444418
/**
445419
* Data provider for test testIsAttributeRequire
446420
*

0 commit comments

Comments
 (0)