|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright (c) 2019 Landofcoder |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be included in all |
| 13 | + * copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | + * SOFTWARE. |
| 22 | + */ |
| 23 | + |
| 24 | +namespace Lof\CustomCustomerInfo\Setup\Patch\Data; |
| 25 | + |
| 26 | +use Magento\Framework\Setup\Patch\DataPatchInterface; |
| 27 | +use Magento\Framework\Setup\Patch\PatchRevertableInterface; |
| 28 | +use Magento\Framework\Setup\ModuleDataSetupInterface; |
| 29 | +use Magento\Customer\Setup\CustomerSetupFactory; |
| 30 | +use Magento\Customer\Setup\CustomerSetup; |
| 31 | + |
| 32 | +class AddAvatarCustomerAttribute implements DataPatchInterface, PatchRevertableInterface |
| 33 | +{ |
| 34 | + |
| 35 | + /** |
| 36 | + * @var ModuleDataSetupInterface |
| 37 | + */ |
| 38 | + private $moduleDataSetup; |
| 39 | + /** |
| 40 | + * @var CustomerSetup |
| 41 | + */ |
| 42 | + private $customerSetupFactory; |
| 43 | + |
| 44 | + /** |
| 45 | + * Constructor |
| 46 | + * |
| 47 | + * @param ModuleDataSetupInterface $moduleDataSetup |
| 48 | + * @param CustomerSetupFactory $customerSetupFactory |
| 49 | + */ |
| 50 | + public function __construct( |
| 51 | + ModuleDataSetupInterface $moduleDataSetup, |
| 52 | + CustomerSetupFactory $customerSetupFactory |
| 53 | + ) { |
| 54 | + $this->moduleDataSetup = $moduleDataSetup; |
| 55 | + $this->customerSetupFactory = $customerSetupFactory; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * {@inheritdoc} |
| 60 | + */ |
| 61 | + public function apply() |
| 62 | + { |
| 63 | + $this->moduleDataSetup->getConnection()->startSetup(); |
| 64 | + /** @var CustomerSetup $customerSetup */ |
| 65 | + $customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]); |
| 66 | + $customerSetup->addAttribute( |
| 67 | + \Magento\Customer\Model\Customer::ENTITY, |
| 68 | + 'avatar', |
| 69 | + [ |
| 70 | + 'type' => 'varchar', |
| 71 | + 'label' => 'avatar', |
| 72 | + 'input' => 'text', |
| 73 | + 'source' => '', |
| 74 | + 'required' => true, |
| 75 | + 'visible' => true, |
| 76 | + 'position' => 333, |
| 77 | + 'system' => false, |
| 78 | + 'backend' => '' |
| 79 | + ] |
| 80 | + ); |
| 81 | + |
| 82 | + $attribute = $customerSetup->getEavConfig()->getAttribute('customer', 'avatar')->addData([ |
| 83 | + 'used_in_forms' => [ |
| 84 | + 'adminhtml_customer', |
| 85 | + 'adminhtml_checkout', |
| 86 | + 'customer_account_create', |
| 87 | + 'customer_account_edit' |
| 88 | + ] |
| 89 | + ]); |
| 90 | + $attribute->save(); |
| 91 | + |
| 92 | + $this->moduleDataSetup->getConnection()->endSetup(); |
| 93 | + } |
| 94 | + |
| 95 | + public function revert() |
| 96 | + { |
| 97 | + $this->moduleDataSetup->getConnection()->startSetup(); |
| 98 | + /** @var CustomerSetup $customerSetup */ |
| 99 | + $customerSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]); |
| 100 | + $customerSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, 'avatar'); |
| 101 | + |
| 102 | + $this->moduleDataSetup->getConnection()->endSetup(); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * {@inheritdoc} |
| 107 | + */ |
| 108 | + public function getAliases() |
| 109 | + { |
| 110 | + return []; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * {@inheritdoc} |
| 115 | + */ |
| 116 | + public static function getDependencies() |
| 117 | + { |
| 118 | + return [ |
| 119 | + |
| 120 | + ]; |
| 121 | + } |
| 122 | +} |
0 commit comments