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,11 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
121
122
'file ' ,
122
123
];
123
124
125
+ /**
126
+ * @var ContextInterface
127
+ */
128
+ private $ context ;
129
+
124
130
/**
125
131
* @param string $name
126
132
* @param string $primaryFieldName
@@ -143,16 +149,19 @@ public function __construct(
143
149
Config $ eavConfig ,
144
150
FilterPool $ filterPool ,
145
151
FileProcessorFactory $ fileProcessorFactory = null ,
152
+ ContextInterface $ context = null ,
146
153
array $ meta = [],
147
154
array $ data = []
148
- ) {
155
+ )
156
+ {
149
157
parent ::__construct ($ name , $ primaryFieldName , $ requestFieldName , $ meta , $ data );
150
158
$ this ->eavValidationRules = $ eavValidationRules ;
151
159
$ this ->collection = $ customerCollectionFactory ->create ();
152
160
$ this ->collection ->addAttributeToSelect ('* ' );
153
161
$ this ->eavConfig = $ eavConfig ;
154
162
$ this ->filterPool = $ filterPool ;
155
163
$ this ->fileProcessorFactory = $ fileProcessorFactory ?: $ this ->getFileProcessorFactory ();
164
+ $ this ->context = $ context ?: ObjectManager::getInstance ()->get (ContextInterface::class);
156
165
$ this ->meta ['customer ' ]['children ' ] = $ this ->getAttributesMeta (
157
166
$ this ->eavConfig ->getEntityType ('customer ' )
158
167
);
@@ -329,15 +338,37 @@ protected function getAttributesMeta(Type $entityType)
329
338
if (!empty ($ rules )) {
330
339
$ meta [$ code ]['arguments ' ]['data ' ]['config ' ]['validation ' ] = $ rules ;
331
340
}
341
+
332
342
$ meta [$ code ]['arguments ' ]['data ' ]['config ' ]['componentType ' ] = Field::NAME ;
333
343
344
+ if ($ this ->canShowAttribute ($ attribute )) {
345
+ $ meta [$ code ]['arguments ' ]['data ' ]['config ' ]['visible ' ] = true ;
346
+ }
347
+
334
348
$ this ->overrideFileUploaderMetadata ($ entityType , $ attribute , $ meta [$ code ]['arguments ' ]['data ' ]['config ' ]);
335
349
}
336
350
337
351
$ this ->processWebsiteMeta ($ meta );
338
352
return $ meta ;
339
353
}
340
354
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
+
341
372
/**
342
373
* Retrieve Country With Websites Source
343
374
*
0 commit comments