Skip to content

Commit 0768a9f

Browse files
committed
ACP2E-675: Telephone required on all websites when set to be optional on some
1 parent 0763e06 commit 0768a9f

File tree

4 files changed

+101
-8
lines changed

4 files changed

+101
-8
lines changed

app/code/Magento/Customer/Controller/Adminhtml/Address/Save.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@
77
namespace Magento\Customer\Controller\Adminhtml\Address;
88

99
use Magento\Backend\App\Action;
10+
use Magento\Customer\Api\CustomerRepositoryInterface;
11+
use Magento\Customer\Model\Config\Share;
12+
use Magento\Customer\Model\CustomerRegistry;
1013
use Magento\Framework\App\Action\HttpPostActionInterface;
14+
use Magento\Framework\App\ObjectManager;
1115
use Magento\Framework\Controller\Result\Json;
1216
use Magento\Framework\Controller\Result\JsonFactory;
1317
use Magento\Framework\Exception\LocalizedException;
1418
use Magento\Framework\Exception\NoSuchEntityException;
19+
use Magento\Store\Model\StoreManagerInterface;
1520
use Psr\Log\LoggerInterface;
1621

1722
/**
@@ -62,6 +67,21 @@ class Save extends Action implements HttpPostActionInterface
6267
*/
6368
private $resultJsonFactory;
6469

70+
/**
71+
* @var Share
72+
*/
73+
private $shareConfig;
74+
75+
/**
76+
* @var StoreManagerInterface
77+
*/
78+
private $storeManager;
79+
80+
/**
81+
* @var CustomerRegistry
82+
*/
83+
private $customerRegistry;
84+
6585
/**
6686
* @param Action\Context $context
6787
* @param \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
@@ -71,6 +91,10 @@ class Save extends Action implements HttpPostActionInterface
7191
* @param \Magento\Customer\Api\Data\AddressInterfaceFactory $addressDataFactory
7292
* @param LoggerInterface $logger
7393
* @param JsonFactory $resultJsonFactory
94+
* @param Share|null $shareConfig
95+
* @param StoreManagerInterface|null $storeManager
96+
* @param CustomerRegistry|null $customerRegistry
97+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
7498
*/
7599
public function __construct(
76100
Action\Context $context,
@@ -80,7 +104,10 @@ public function __construct(
80104
\Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
81105
\Magento\Customer\Api\Data\AddressInterfaceFactory $addressDataFactory,
82106
LoggerInterface $logger,
83-
JsonFactory $resultJsonFactory
107+
JsonFactory $resultJsonFactory,
108+
?Share $shareConfig = null,
109+
?StoreManagerInterface $storeManager = null,
110+
?CustomerRegistry $customerRegistry = null
84111
) {
85112
parent::__construct($context);
86113
$this->addressRepository = $addressRepository;
@@ -90,6 +117,12 @@ public function __construct(
90117
$this->addressDataFactory = $addressDataFactory;
91118
$this->logger = $logger;
92119
$this->resultJsonFactory = $resultJsonFactory;
120+
$this->shareConfig = $shareConfig
121+
?? ObjectManager::getInstance()->get(Share::class);
122+
$this->storeManager = $storeManager
123+
?? ObjectManager::getInstance()->get(StoreManagerInterface::class);
124+
$this->customerRegistry = $customerRegistry
125+
?? ObjectManager::getInstance()->get(CustomerRegistry::class);
93126
}
94127

95128
/**
@@ -104,6 +137,10 @@ public function execute(): Json
104137

105138
$error = false;
106139
try {
140+
$customerModel = $this->customerRegistry->retrieve($customerId);
141+
if (!$this->shareConfig->isGlobalScope() && $customerModel->getStoreId()) {
142+
$this->storeManager->setCurrentStore($customerModel->getStoreId());
143+
}
107144
/** @var \Magento\Customer\Api\Data\CustomerInterface $customer */
108145
$customer = $this->customerRepository->getById($customerId);
109146

app/code/Magento/Customer/Controller/Adminhtml/Address/Validate.php

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@
77
namespace Magento\Customer\Controller\Adminhtml\Address;
88

99
use Magento\Backend\App\Action;
10+
use Magento\Customer\Api\CustomerRepositoryInterface;
11+
use Magento\Customer\Model\Config\Share;
1012
use Magento\Framework\App\Action\HttpGetActionInterface;
1113
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
14+
use Magento\Framework\App\ObjectManager;
1215
use Magento\Framework\Controller\Result\Json;
1316
use Magento\Framework\DataObject;
17+
use Magento\Store\Model\StoreManagerInterface;
1418

1519
/**
1620
* Class for validation of customer address form on admin.
@@ -34,19 +38,46 @@ class Validate extends Action implements HttpPostActionInterface, HttpGetActionI
3438
*/
3539
private $formFactory;
3640

41+
/**
42+
* @var Share
43+
*/
44+
private $shareConfig;
45+
46+
/**
47+
* @var CustomerRepositoryInterface
48+
*/
49+
private $customerRepository;
50+
51+
/**
52+
* @var StoreManagerInterface
53+
*/
54+
private $storeManager;
55+
3756
/**
3857
* @param Action\Context $context
3958
* @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
4059
* @param \Magento\Customer\Model\Metadata\FormFactory $formFactory
60+
* @param Share|null $shareConfig
61+
* @param CustomerRepositoryInterface|null $customerRepository
62+
* @param StoreManagerInterface|null $storeManager
4163
*/
4264
public function __construct(
4365
Action\Context $context,
4466
\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
45-
\Magento\Customer\Model\Metadata\FormFactory $formFactory
67+
\Magento\Customer\Model\Metadata\FormFactory $formFactory,
68+
?Share $shareConfig = null,
69+
?CustomerRepositoryInterface $customerRepository = null,
70+
?StoreManagerInterface $storeManager = null
4671
) {
4772
parent::__construct($context);
4873
$this->resultJsonFactory = $resultJsonFactory;
4974
$this->formFactory = $formFactory;
75+
$this->shareConfig = $shareConfig
76+
?? ObjectManager::getInstance()->get(Share::class);
77+
$this->customerRepository = $customerRepository
78+
?? ObjectManager::getInstance()->get(CustomerRepositoryInterface::class);
79+
$this->storeManager = $storeManager
80+
?? ObjectManager::getInstance()->get(StoreManagerInterface::class);
5081
}
5182

5283
/**
@@ -82,8 +113,13 @@ public function execute(): Json
82113
private function validateCustomerAddress(DataObject $response): DataObject
83114
{
84115
$addressForm = $this->formFactory->create('customer_address', 'adminhtml_customer_address');
116+
if ($this->getRequest()->getParam('parent_id')) {
117+
$customer = $this->customerRepository->getById($this->getRequest()->getParam('parent_id'));
118+
if (!$this->shareConfig->isGlobalScope() && $customer->getStoreId()) {
119+
$this->storeManager->setCurrentStore($customer->getStoreId());
120+
}
121+
}
85122
$formData = $addressForm->extractData($this->getRequest());
86-
87123
$errors = $addressForm->validateData($formData);
88124
if ($errors !== true) {
89125
$messages = $response->hasMessages() ? $response->getMessages() : [];

app/code/Magento/Customer/Model/Address/DataProvider.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
namespace Magento\Customer\Model\Address;
88

99
use Magento\Customer\Api\CustomerRepositoryInterface;
10+
use Magento\Customer\Model\Config\Share;
1011
use Magento\Customer\Model\ResourceModel\Address\CollectionFactory;
1112
use Magento\Eav\Model\Config;
1213
use Magento\Eav\Model\Entity\Type;
1314
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
15+
use Magento\Framework\App\ObjectManager;
1416
use Magento\Framework\View\Element\UiComponent\ContextInterface;
1517
use Magento\Customer\Model\Address;
1618
use Magento\Customer\Model\FileUploaderDataResolver;
@@ -41,7 +43,7 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
4143
*/
4244
private $allowToShowHiddenAttributes;
4345

44-
/*
46+
/**
4547
* @var ContextInterface
4648
*/
4749
private $context;
@@ -72,6 +74,11 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
7274
*/
7375
private $attributeMetadataResolver;
7476

77+
/**
78+
* @var Share|null
79+
*/
80+
private $shareConfig;
81+
7582
/**
7683
* DataProvider constructor.
7784
* @param string $name
@@ -86,6 +93,7 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
8693
* @param array $meta
8794
* @param array $data
8895
* @param bool $allowToShowHiddenAttributes
96+
* @param Share|null $shareConfig
8997
* @throws \Magento\Framework\Exception\LocalizedException
9098
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
9199
*/
@@ -101,7 +109,8 @@ public function __construct(
101109
AttributeMetadataResolver $attributeMetadataResolver,
102110
array $meta = [],
103111
array $data = [],
104-
$allowToShowHiddenAttributes = true
112+
$allowToShowHiddenAttributes = true,
113+
?Share $shareConfig = null
105114
) {
106115
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
107116
$this->collection = $addressCollectionFactory->create();
@@ -111,6 +120,7 @@ public function __construct(
111120
$this->context = $context;
112121
$this->fileUploaderDataResolver = $fileUploaderDataResolver;
113122
$this->attributeMetadataResolver = $attributeMetadataResolver;
123+
$this->shareConfig = $shareConfig ?? ObjectManager::getInstance()->get(Share::class);
114124
$this->meta['general']['children'] = $this->getAttributesMeta(
115125
$eavConfig->getEntityType('customer_address')
116126
);
@@ -210,9 +220,17 @@ private function getDefaultData(): array
210220
private function getAttributesMeta(Type $entityType): array
211221
{
212222
$meta = [];
213-
$attributes = $entityType->getAttributeCollection();
223+
$parentId = $this->context->getRequestParam('parent_id');
224+
$customer = $this->customerRepository->getById($parentId);
225+
/** @var \Magento\Customer\Model\ResourceModel\Address\Attribute\Collection $sharedCollection */
226+
$sharedCollection = $entityType->getAttributeCollection();
227+
$collection = clone $sharedCollection;
228+
if (!$this->shareConfig->isGlobalScope()) {
229+
$collection->setWebsite($customer->getWebsiteId());
230+
}
231+
214232
/* @var AbstractAttribute $attribute */
215-
foreach ($attributes as $attribute) {
233+
foreach ($collection as $attribute) {
216234
if (\in_array($attribute->getFrontendInput(), $this->bannedInputTypes, true)) {
217235
continue;
218236
}

app/code/Magento/Eav/Model/Config.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,9 @@ private function setAttributeData($attribute, $entityType): AbstractAttribute
954954
private function initAttributesFromCache(Type $entityType)
955955
{
956956
$entityTypeCode = $entityType->getEntityTypeCode();
957-
$cacheKey = self::ATTRIBUTES_CACHE_ID . $entityTypeCode;
957+
$attributeCollection = $this->_universalFactory->create($entityType->getEntityAttributeCollection());
958+
$websiteId = $attributeCollection instanceof Collection ? $this->getWebsiteId($attributeCollection) : 0;
959+
$cacheKey = self::ATTRIBUTES_CACHE_ID . '-' . $entityTypeCode . '-' . $websiteId ;
958960
if ($this->isCacheEnabled() && ($attributes = $this->_cache->load($cacheKey))) {
959961
$attributes = $this->serializer->unserialize($attributes);
960962
if ($attributes) {

0 commit comments

Comments
 (0)