9
9
use Magento \Customer \Api \CustomerMetadataInterface ;
10
10
use Magento \Customer \Api \Data \AddressInterface ;
11
11
use Magento \Customer \Api \Data \CustomerInterface ;
12
+ use Magento \Customer \Model \Address ;
12
13
use Magento \Customer \Model \Attribute ;
14
+ use Magento \Customer \Model \Customer ;
13
15
use Magento \Customer \Model \FileProcessor ;
14
16
use Magento \Customer \Model \FileProcessorFactory ;
15
17
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 ;
16
20
use Magento \Eav \Api \Data \AttributeInterface ;
17
21
use Magento \Eav \Model \Config ;
18
22
use Magento \Eav \Model \Entity \Attribute \AbstractAttribute ;
19
23
use Magento \Eav \Model \Entity \Type ;
20
- use Magento \Customer \Model \Address ;
21
- use Magento \Customer \Model \Customer ;
22
24
use Magento \Framework \App \ObjectManager ;
23
25
use Magento \Framework \Session \SessionManagerInterface ;
26
+ use Magento \Framework \View \Element \UiComponent \ContextInterface ;
27
+ use Magento \Framework \View \Element \UiComponent \DataProvider \FilterPool ;
24
28
use Magento \Ui \Component \Form \Field ;
25
29
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 ;
29
30
30
31
/**
31
32
* Class DataProvider
@@ -121,6 +122,18 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
121
122
'file ' ,
122
123
];
123
124
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
+
124
137
/**
125
138
* @param string $name
126
139
* @param string $primaryFieldName
@@ -130,8 +143,10 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
130
143
* @param Config $eavConfig
131
144
* @param FilterPool $filterPool
132
145
* @param FileProcessorFactory $fileProcessorFactory
146
+ * @param ContextInterface $context
133
147
* @param array $meta
134
148
* @param array $data
149
+ * @param bool $allowToShowHiddenAttributes
135
150
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
136
151
*/
137
152
public function __construct (
@@ -144,7 +159,9 @@ public function __construct(
144
159
FilterPool $ filterPool ,
145
160
FileProcessorFactory $ fileProcessorFactory = null ,
146
161
array $ meta = [],
147
- array $ data = []
162
+ array $ data = [],
163
+ ContextInterface $ context = null ,
164
+ $ allowToShowHiddenAttributes = true
148
165
) {
149
166
parent ::__construct ($ name , $ primaryFieldName , $ requestFieldName , $ meta , $ data );
150
167
$ this ->eavValidationRules = $ eavValidationRules ;
@@ -153,6 +170,8 @@ public function __construct(
153
170
$ this ->eavConfig = $ eavConfig ;
154
171
$ this ->filterPool = $ filterPool ;
155
172
$ this ->fileProcessorFactory = $ fileProcessorFactory ?: $ this ->getFileProcessorFactory ();
173
+ $ this ->context = $ context ?: ObjectManager::getInstance ()->get (ContextInterface::class);
174
+ $ this ->allowToShowHiddenAttributes = $ allowToShowHiddenAttributes ;
156
175
$ this ->meta ['customer ' ]['children ' ] = $ this ->getAttributesMeta (
157
176
$ this ->eavConfig ->getEntityType ('customer ' )
158
177
);
@@ -329,7 +348,9 @@ protected function getAttributesMeta(Type $entityType)
329
348
if (!empty ($ rules )) {
330
349
$ meta [$ code ]['arguments ' ]['data ' ]['config ' ]['validation ' ] = $ rules ;
331
350
}
351
+
332
352
$ meta [$ code ]['arguments ' ]['data ' ]['config ' ]['componentType ' ] = Field::NAME ;
353
+ $ meta [$ code ]['arguments ' ]['data ' ]['config ' ]['visible ' ] = $ this ->canShowAttribute ($ attribute );
333
354
334
355
$ this ->overrideFileUploaderMetadata ($ entityType , $ attribute , $ meta [$ code ]['arguments ' ]['data ' ]['config ' ]);
335
356
}
@@ -338,6 +359,47 @@ protected function getAttributesMeta(Type $entityType)
338
359
return $ meta ;
339
360
}
340
361
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
+
341
403
/**
342
404
* Retrieve Country With Websites Source
343
405
*
0 commit comments