Skip to content

Commit 0cc5c4f

Browse files
author
Sergii Kovalenko
committed
MAGETWO-69521: Customer attribute with "Show on Storefront" set to No does not appear in backend
1 parent 711ad81 commit 0cc5c4f

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

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

Lines changed: 37 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,11 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
121122
'file',
122123
];
123124

125+
/**
126+
* @var ContextInterface
127+
*/
128+
private $context;
129+
124130
/**
125131
* @param string $name
126132
* @param string $primaryFieldName
@@ -143,16 +149,19 @@ public function __construct(
143149
Config $eavConfig,
144150
FilterPool $filterPool,
145151
FileProcessorFactory $fileProcessorFactory = null,
152+
ContextInterface $context = null,
146153
array $meta = [],
147154
array $data = []
148-
) {
155+
)
156+
{
149157
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
150158
$this->eavValidationRules = $eavValidationRules;
151159
$this->collection = $customerCollectionFactory->create();
152160
$this->collection->addAttributeToSelect('*');
153161
$this->eavConfig = $eavConfig;
154162
$this->filterPool = $filterPool;
155163
$this->fileProcessorFactory = $fileProcessorFactory ?: $this->getFileProcessorFactory();
164+
$this->context = $context ?: ObjectManager::getInstance()->get(ContextInterface::class);
156165
$this->meta['customer']['children'] = $this->getAttributesMeta(
157166
$this->eavConfig->getEntityType('customer')
158167
);
@@ -329,15 +338,37 @@ protected function getAttributesMeta(Type $entityType)
329338
if (!empty($rules)) {
330339
$meta[$code]['arguments']['data']['config']['validation'] = $rules;
331340
}
341+
332342
$meta[$code]['arguments']['data']['config']['componentType'] = Field::NAME;
333343

344+
if ($this->canShowAttribute($attribute)) {
345+
$meta[$code]['arguments']['data']['config']['visible'] = true;
346+
}
347+
334348
$this->overrideFileUploaderMetadata($entityType, $attribute, $meta[$code]['arguments']['data']['config']);
335349
}
336350

337351
$this->processWebsiteMeta($meta);
338352
return $meta;
339353
}
340354

355+
/**
356+
* Check whether we can show attribute in admin or not
357+
*
358+
* @param Attribute $customerAttribute
359+
* @return bool
360+
*/
361+
private function canShowAttribute(AbstractAttribute $customerAttribute)
362+
{
363+
$isRegistration = is_null($this->context->getRequestParam($this->getRequestFieldName()));
364+
365+
return is_array($customerAttribute->getUsedInForms()) &&
366+
(
367+
(in_array('customer_account_create', $customerAttribute->getUsedInForms()) && $isRegistration) ||
368+
(in_array('customer_account_edit', $customerAttribute->getUsedInForms()) && !$isRegistration)
369+
);
370+
}
371+
341372
/**
342373
* Retrieve Country With Websites Source
343374
*

0 commit comments

Comments
 (0)