Skip to content

Commit 70c8334

Browse files
author
Sergii Kovalenko
authored
Merge pull request #1333 from magento-south/MAGETWO-69521
[south] MAGETWO-69521: Customer attribute with "Show on Storefront" set to No does not appear in backend
2 parents 07c18d1 + 6be9794 commit 70c8334

File tree

2 files changed

+517
-42
lines changed

2 files changed

+517
-42
lines changed

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

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,24 @@
99
use Magento\Customer\Api\CustomerMetadataInterface;
1010
use Magento\Customer\Api\Data\AddressInterface;
1111
use Magento\Customer\Api\Data\CustomerInterface;
12+
use Magento\Customer\Model\Address;
1213
use Magento\Customer\Model\Attribute;
14+
use Magento\Customer\Model\Customer;
1315
use Magento\Customer\Model\FileProcessor;
1416
use Magento\Customer\Model\FileProcessorFactory;
1517
use Magento\Customer\Model\ResourceModel\Address\Attribute\Source\CountryWithWebsites;
18+
use Magento\Customer\Model\ResourceModel\Customer\Collection;
19+
use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory as CustomerCollectionFactory;
1620
use Magento\Eav\Api\Data\AttributeInterface;
1721
use Magento\Eav\Model\Config;
1822
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
1923
use Magento\Eav\Model\Entity\Type;
20-
use Magento\Customer\Model\Address;
21-
use Magento\Customer\Model\Customer;
2224
use Magento\Framework\App\ObjectManager;
2325
use Magento\Framework\Session\SessionManagerInterface;
26+
use Magento\Framework\View\Element\UiComponent\ContextInterface;
27+
use Magento\Framework\View\Element\UiComponent\DataProvider\FilterPool;
2428
use Magento\Ui\Component\Form\Field;
2529
use Magento\Ui\DataProvider\EavValidationRules;
26-
use Magento\Customer\Model\ResourceModel\Customer\Collection;
27-
use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory as CustomerCollectionFactory;
28-
use Magento\Framework\View\Element\UiComponent\DataProvider\FilterPool;
2930

3031
/**
3132
* Class DataProvider
@@ -121,6 +122,18 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
121122
'file',
122123
];
123124

125+
/**
126+
* @var ContextInterface
127+
*/
128+
private $context;
129+
130+
/**
131+
* Allow to manage attributes, even they are hidden on storefront
132+
*
133+
* @var bool
134+
*/
135+
private $allowToShowHiddenAttributes;
136+
124137
/**
125138
* @param string $name
126139
* @param string $primaryFieldName
@@ -130,8 +143,10 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
130143
* @param Config $eavConfig
131144
* @param FilterPool $filterPool
132145
* @param FileProcessorFactory $fileProcessorFactory
146+
* @param ContextInterface $context
133147
* @param array $meta
134148
* @param array $data
149+
* @param bool $allowToShowHiddenAttributes
135150
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
136151
*/
137152
public function __construct(
@@ -144,7 +159,9 @@ public function __construct(
144159
FilterPool $filterPool,
145160
FileProcessorFactory $fileProcessorFactory = null,
146161
array $meta = [],
147-
array $data = []
162+
array $data = [],
163+
ContextInterface $context = null,
164+
$allowToShowHiddenAttributes = true
148165
) {
149166
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
150167
$this->eavValidationRules = $eavValidationRules;
@@ -153,6 +170,8 @@ public function __construct(
153170
$this->eavConfig = $eavConfig;
154171
$this->filterPool = $filterPool;
155172
$this->fileProcessorFactory = $fileProcessorFactory ?: $this->getFileProcessorFactory();
173+
$this->context = $context ?: ObjectManager::getInstance()->get(ContextInterface::class);
174+
$this->allowToShowHiddenAttributes = $allowToShowHiddenAttributes;
156175
$this->meta['customer']['children'] = $this->getAttributesMeta(
157176
$this->eavConfig->getEntityType('customer')
158177
);
@@ -329,7 +348,9 @@ protected function getAttributesMeta(Type $entityType)
329348
if (!empty($rules)) {
330349
$meta[$code]['arguments']['data']['config']['validation'] = $rules;
331350
}
351+
332352
$meta[$code]['arguments']['data']['config']['componentType'] = Field::NAME;
353+
$meta[$code]['arguments']['data']['config']['visible'] = $this->canShowAttribute($attribute);
333354

334355
$this->overrideFileUploaderMetadata($entityType, $attribute, $meta[$code]['arguments']['data']['config']);
335356
}
@@ -338,6 +359,47 @@ protected function getAttributesMeta(Type $entityType)
338359
return $meta;
339360
}
340361

362+
/**
363+
* Check whether the specific attribute can be shown in form: customer registration, customer edit, etc...
364+
*
365+
* @param Attribute $customerAttribute
366+
* @return bool
367+
*/
368+
private function canShowAttributeInForm(AbstractAttribute $customerAttribute)
369+
{
370+
$isRegistration = $this->context->getRequestParam($this->getRequestFieldName()) === null;
371+
372+
if ($customerAttribute->getEntityType()->getEntityTypeCode() === 'customer') {
373+
return is_array($customerAttribute->getUsedInForms()) &&
374+
(
375+
(in_array('customer_account_create', $customerAttribute->getUsedInForms()) && $isRegistration) ||
376+
(in_array('customer_account_edit', $customerAttribute->getUsedInForms()) && !$isRegistration)
377+
);
378+
} else {
379+
return is_array($customerAttribute->getUsedInForms()) &&
380+
in_array('customer_address_edit', $customerAttribute->getUsedInForms());
381+
}
382+
}
383+
384+
/**
385+
* Detect can we show attribute on specific form or not
386+
*
387+
* @param Attribute $customerAttribute
388+
* @return bool
389+
*/
390+
private function canShowAttribute(AbstractAttribute $customerAttribute)
391+
{
392+
$userDefined = (bool) $customerAttribute->getIsUserDefined();
393+
if (!$userDefined) {
394+
return $customerAttribute->getIsVisible();
395+
}
396+
397+
$canShowOnForm = $this->canShowAttributeInForm($customerAttribute);
398+
399+
return ($this->allowToShowHiddenAttributes && $canShowOnForm) ||
400+
(!$this->allowToShowHiddenAttributes && $canShowOnForm && $customerAttribute->getIsVisible());
401+
}
402+
341403
/**
342404
* Retrieve Country With Websites Source
343405
*

0 commit comments

Comments
 (0)