Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CustomerSetup.php
Go to the documentation of this file.
1 <?php
9 
15 use Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory;
16 
21 class CustomerSetup extends EavSetup
22 {
28  protected $eavConfig;
29 
39  public function __construct(
41  Context $context,
42  CacheInterface $cache,
43  CollectionFactory $attrGroupCollectionFactory,
45  ) {
46  $this->eavConfig = $eavConfig;
47  parent::__construct($setup, $context, $cache, $attrGroupCollectionFactory);
48  }
49 
57  public function installCustomerForms()
58  {
59  $customer = (int)parent::getEntityTypeId('customer');
60  $customerAddress = (int)parent::getEntityTypeId('customer_address');
61 
62  $attributeIds = [];
63  $select = $this->getSetup()->getConnection()->select()->from(
64  ['ea' => $this->getSetup()->getTable('eav_attribute')],
65  ['entity_type_id', 'attribute_code', 'attribute_id']
66  )->where(
67  'ea.entity_type_id IN(?)',
69  );
70  foreach ($this->getSetup()->getConnection()->fetchAll($select) as $row) {
71  $attributeIds[$row['entity_type_id']][$row['attribute_code']] = $row['attribute_id'];
72  }
73 
74  $data = [];
75  $entities = $this->getDefaultEntities();
76  $attributes = $entities['customer']['attributes'];
77  foreach ($attributes as $attributeCode => $attribute) {
78  $attributeId = $attributeIds[$customer][$attributeCode];
79  $attribute['system'] = isset($attribute['system']) ? $attribute['system'] : true;
80  $attribute['visible'] = isset($attribute['visible']) ? $attribute['visible'] : true;
81  if ($attribute['system'] != true || $attribute['visible'] != false) {
82  $usedInForms = ['customer_account_create', 'customer_account_edit', 'checkout_register'];
83  if (!empty($attribute['adminhtml_only'])) {
84  $usedInForms = ['adminhtml_customer'];
85  } else {
86  $usedInForms[] = 'adminhtml_customer';
87  }
88  if (!empty($attribute['admin_checkout'])) {
89  $usedInForms[] = 'adminhtml_checkout';
90  }
91  foreach ($usedInForms as $formCode) {
92  $data[] = ['form_code' => $formCode, 'attribute_id' => $attributeId];
93  }
94  }
95  }
96 
97  $attributes = $entities['customer_address']['attributes'];
98  foreach ($attributes as $attributeCode => $attribute) {
99  $attributeId = $attributeIds[$customerAddress][$attributeCode];
100  $attribute['system'] = isset($attribute['system']) ? $attribute['system'] : true;
101  $attribute['visible'] = isset($attribute['visible']) ? $attribute['visible'] : true;
102  if (false === ($attribute['system'] == true && $attribute['visible'] == false)) {
103  $usedInForms = [
104  'adminhtml_customer_address',
105  'customer_address_edit',
106  'customer_register_address',
107  ];
108  foreach ($usedInForms as $formCode) {
109  $data[] = ['form_code' => $formCode, 'attribute_id' => $attributeId];
110  }
111  }
112  }
113 
114  if ($data) {
115  $this->getSetup()->getConnection()
116  ->insertMultiple($this->getSetup()->getTable('customer_form_attribute'), $data);
117  }
118  }
119 
126  public function getDefaultEntities()
127  {
128  $entities = [
129  'customer' => [
131  'entity_model' => \Magento\Customer\Model\ResourceModel\Customer::class,
132  'attribute_model' => \Magento\Customer\Model\Attribute::class,
133  'table' => 'customer_entity',
134  'increment_model' => \Magento\Eav\Model\Entity\Increment\NumericValue::class,
135  'additional_attribute_table' => 'customer_eav_attribute',
136  'entity_attribute_collection' => \Magento\Customer\Model\ResourceModel\Attribute\Collection::class,
137  'attributes' => [
138  'website_id' => [
139  'type' => 'static',
140  'label' => 'Associate to Website',
141  'input' => 'select',
142  'source' => \Magento\Customer\Model\Customer\Attribute\Source\Website::class,
143  'backend' => \Magento\Customer\Model\Customer\Attribute\Backend\Website::class,
144  'sort_order' => 10,
145  'position' => 10,
146  'adminhtml_only' => 1,
147  ],
148  'store_id' => [
149  'type' => 'static',
150  'label' => 'Create In',
151  'input' => 'select',
152  'source' => \Magento\Customer\Model\Customer\Attribute\Source\Store::class,
153  'backend' => \Magento\Customer\Model\Customer\Attribute\Backend\Store::class,
154  'sort_order' => 20,
155  'visible' => false,
156  'adminhtml_only' => 1,
157  ],
158  'created_in' => [
159  'type' => 'static',
160  'label' => 'Created From',
161  'input' => 'text',
162  'required' => false,
163  'sort_order' => 20,
164  'position' => 20,
165  'adminhtml_only' => 1,
166  ],
167  'prefix' => [
168  'type' => 'static',
169  'label' => 'Name Prefix',
170  'input' => 'text',
171  'required' => false,
172  'sort_order' => 30,
173  'visible' => false,
174  'system' => false,
175  'position' => 30,
176  ],
177  'firstname' => [
178  'type' => 'static',
179  'label' => 'First Name',
180  'input' => 'text',
181  'sort_order' => 40,
182  'validate_rules' => '{"max_text_length":255,"min_text_length":1}',
183  'position' => 40,
184  ],
185  'middlename' => [
186  'type' => 'static',
187  'label' => 'Middle Name/Initial',
188  'input' => 'text',
189  'required' => false,
190  'sort_order' => 50,
191  'visible' => false,
192  'system' => false,
193  'position' => 50,
194  ],
195  'lastname' => [
196  'type' => 'static',
197  'label' => 'Last Name',
198  'input' => 'text',
199  'sort_order' => 60,
200  'validate_rules' => '{"max_text_length":255,"min_text_length":1}',
201  'position' => 60,
202  ],
203  'suffix' => [
204  'type' => 'static',
205  'label' => 'Name Suffix',
206  'input' => 'text',
207  'required' => false,
208  'sort_order' => 70,
209  'visible' => false,
210  'system' => false,
211  'position' => 70,
212  ],
213  'email' => [
214  'type' => 'static',
215  'label' => 'Email',
216  'input' => 'text',
217  'sort_order' => 80,
218  'validate_rules' => '{"input_validation":"email"}',
219  'position' => 80,
220  'admin_checkout' => 1,
221  ],
222  'group_id' => [
223  'type' => 'static',
224  'label' => 'Group',
225  'input' => 'select',
226  'source' => \Magento\Customer\Model\Customer\Attribute\Source\Group::class,
227  'sort_order' => 25,
228  'position' => 25,
229  'adminhtml_only' => 1,
230  'admin_checkout' => 1,
231  ],
232  'dob' => [
233  'type' => 'static',
234  'label' => 'Date of Birth',
235  'input' => 'date',
236  'frontend' => \Magento\Eav\Model\Entity\Attribute\Frontend\Datetime::class,
237  'backend' => \Magento\Eav\Model\Entity\Attribute\Backend\Datetime::class,
238  'required' => false,
239  'sort_order' => 90,
240  'visible' => false,
241  'system' => false,
242  'input_filter' => 'date',
243  'validate_rules' => '{"input_validation":"date"}',
244  'position' => 90,
245  'admin_checkout' => 1,
246  ],
247  'password_hash' => [
248  'type' => 'static',
249  'input' => 'hidden',
250  'backend' => \Magento\Customer\Model\Customer\Attribute\Backend\Password::class,
251  'required' => false,
252  'sort_order' => 81,
253  'visible' => false,
254  ],
255  'rp_token' => [
256  'type' => 'static',
257  'input' => 'hidden',
258  'required' => false,
259  'sort_order' => 115,
260  'visible' => false,
261  ],
262  'rp_token_created_at' => [
263  'type' => 'static',
264  'input' => 'date',
265  'validate_rules' => '{"input_validation":"date"}',
266  'required' => false,
267  'sort_order' => 120,
268  'visible' => false,
269  ],
270  'default_billing' => [
271  'type' => 'static',
272  'label' => 'Default Billing Address',
273  'input' => 'text',
274  'backend' => \Magento\Customer\Model\Customer\Attribute\Backend\Billing::class,
275  'required' => false,
276  'sort_order' => 82,
277  'visible' => false,
278  ],
279  'default_shipping' => [
280  'type' => 'static',
281  'label' => 'Default Shipping Address',
282  'input' => 'text',
283  'backend' => \Magento\Customer\Model\Customer\Attribute\Backend\Shipping::class,
284  'required' => false,
285  'sort_order' => 83,
286  'visible' => false,
287  ],
288  'taxvat' => [
289  'type' => 'static',
290  'label' => 'Tax/VAT Number',
291  'input' => 'text',
292  'required' => false,
293  'sort_order' => 100,
294  'visible' => false,
295  'system' => false,
296  'validate_rules' => '{"max_text_length":255}',
297  'position' => 100,
298  'admin_checkout' => 1,
299  ],
300  'confirmation' => [
301  'type' => 'static',
302  'label' => 'Is Confirmed',
303  'input' => 'text',
304  'required' => false,
305  'sort_order' => 85,
306  'visible' => false,
307  ],
308  'created_at' => [
309  'type' => 'static',
310  'label' => 'Created At',
311  'input' => 'date',
312  'required' => false,
313  'sort_order' => 86,
314  'visible' => false,
315  'system' => false,
316  ],
317  'gender' => [
318  'type' => 'static',
319  'label' => 'Gender',
320  'input' => 'select',
321  'source' => \Magento\Eav\Model\Entity\Attribute\Source\Table::class,
322  'required' => false,
323  'sort_order' => 110,
324  'visible' => false,
325  'system' => false,
326  'validate_rules' => '[]',
327  'position' => 110,
328  'admin_checkout' => 1,
329  'option' => ['values' => ['Male', 'Female']],
330  ],
331  'disable_auto_group_change' => [
332  'type' => 'static',
333  'label' => 'Disable Automatic Group Change Based on VAT ID',
334  'input' => 'boolean',
335  'backend' => \Magento\Customer\Model\Attribute\Backend\Data\Boolean::class,
336  'position' => 28,
337  'required' => false,
338  'adminhtml_only' => true
339  ]
340  ],
341  ],
342  'customer_address' => [
344  'entity_model' => \Magento\Customer\Model\ResourceModel\Address::class,
345  'attribute_model' => \Magento\Customer\Model\Attribute::class,
346  'table' => 'customer_address_entity',
347  'additional_attribute_table' => 'customer_eav_attribute',
348  'entity_attribute_collection' =>
349  \Magento\Customer\Model\ResourceModel\Address\Attribute\Collection::class,
350  'attributes' => [
351  'prefix' => [
352  'type' => 'static',
353  'label' => 'Name Prefix',
354  'input' => 'text',
355  'required' => false,
356  'sort_order' => 10,
357  'visible' => false,
358  'system' => false,
359  'position' => 10,
360  ],
361  'firstname' => [
362  'type' => 'static',
363  'label' => 'First Name',
364  'input' => 'text',
365  'sort_order' => 20,
366  'validate_rules' => '{"max_text_length":255,"min_text_length":1}',
367  'position' => 20,
368  ],
369  'middlename' => [
370  'type' => 'static',
371  'label' => 'Middle Name/Initial',
372  'input' => 'text',
373  'required' => false,
374  'sort_order' => 30,
375  'visible' => false,
376  'system' => false,
377  'position' => 30,
378  ],
379  'lastname' => [
380  'type' => 'static',
381  'label' => 'Last Name',
382  'input' => 'text',
383  'sort_order' => 40,
384  'validate_rules' => '{"max_text_length":255,"min_text_length":1}',
385  'position' => 40,
386  ],
387  'suffix' => [
388  'type' => 'static',
389  'label' => 'Name Suffix',
390  'input' => 'text',
391  'required' => false,
392  'sort_order' => 50,
393  'visible' => false,
394  'system' => false,
395  'position' => 50,
396  ],
397  'company' => [
398  'type' => 'static',
399  'label' => 'Company',
400  'input' => 'text',
401  'required' => false,
402  'sort_order' => 60,
403  'validate_rules' => '{"max_text_length":255,"min_text_length":1}',
404  'position' => 60,
405  ],
406  'street' => [
407  'type' => 'static',
408  'label' => 'Street Address',
409  'input' => 'multiline',
410  'backend' => \Magento\Eav\Model\Entity\Attribute\Backend\DefaultBackend::class,
411  'sort_order' => 70,
412  'multiline_count' => 2,
413  'validate_rules' => '{"max_text_length":255,"min_text_length":1}',
414  'position' => 70,
415  ],
416  'city' => [
417  'type' => 'static',
418  'label' => 'City',
419  'input' => 'text',
420  'sort_order' => 80,
421  'validate_rules' => '{"max_text_length":255,"min_text_length":1}',
422  'position' => 80,
423  ],
424  'country_id' => [
425  'type' => 'static',
426  'label' => 'Country',
427  'input' => 'select',
428  'source' => \Magento\Customer\Model\ResourceModel\Address\Attribute\Source\Country::class,
429  'sort_order' => 90,
430  'position' => 90,
431  ],
432  'region' => [
433  'type' => 'static',
434  'label' => 'State/Province',
435  'input' => 'text',
436  'backend' => \Magento\Customer\Model\ResourceModel\Address\Attribute\Backend\Region::class,
437  'required' => false,
438  'sort_order' => 100,
439  'position' => 100,
440  ],
441  'region_id' => [
442  'type' => 'static',
443  'label' => 'State/Province',
444  'input' => 'hidden',
445  'source' => \Magento\Customer\Model\ResourceModel\Address\Attribute\Source\Region::class,
446  'required' => false,
447  'sort_order' => 100,
448  'position' => 100,
449  ],
450  'postcode' => [
451  'type' => 'static',
452  'label' => 'Zip/Postal Code',
453  'input' => 'text',
454  'sort_order' => 110,
455  'validate_rules' => '[]',
456  'data' => \Magento\Customer\Model\Attribute\Data\Postcode::class,
457  'position' => 110,
458  'required' => false,
459  ],
460  'telephone' => [
461  'type' => 'static',
462  'label' => 'Phone Number',
463  'input' => 'text',
464  'sort_order' => 120,
465  'validate_rules' => '{"max_text_length":255,"min_text_length":1}',
466  'position' => 120,
467  ],
468  'fax' => [
469  'type' => 'static',
470  'label' => 'Fax',
471  'input' => 'text',
472  'required' => false,
473  'sort_order' => 130,
474  'validate_rules' => '{"max_text_length":255,"min_text_length":1}',
475  'position' => 130,
476  ],
477  ],
478  ],
479  ];
480  return $entities;
481  }
482 
488  public function getEavConfig()
489  {
490  return $this->eavConfig;
491  }
492 
499  public function upgradeAttributes(array $entityAttributes)
500  {
501  foreach ($entityAttributes as $entityType => $attributes) {
502  foreach ($attributes as $attributeCode => $attributeData) {
503  $attribute = $this->getEavConfig()->getAttribute($entityType, $attributeCode);
504  foreach ($attributeData as $key => $value) {
505  $attribute->setData($key, $value);
506  }
507  $attribute->save();
508  }
509  }
510  }
511 }
__construct(ModuleDataSetupInterface $setup, Context $context, CacheInterface $cache, CollectionFactory $attrGroupCollectionFactory, Config $eavConfig)
$customer
Definition: customers.php:11
$value
Definition: gender.phtml:16
$attributeCode
Definition: extend.phtml:12
upgradeAttributes(array $entityAttributes)
$attributes
Definition: matrix.phtml:13
$setup
Definition: trigger.php:12
$customerAddress