Skip to content

Instantly share code, notes, and snippets.

@jasonevans1
Last active May 19, 2022 21:38
Show Gist options
  • Save jasonevans1/11bc0f8fce2130fbcd8b12298d2446ee to your computer and use it in GitHub Desktop.
Save jasonevans1/11bc0f8fce2130fbcd8b12298d2446ee to your computer and use it in GitHub Desktop.
Magento 2 Create Customer Attribute Patch
<?php
/**
* Copyright © All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Abc\Customer\Setup\Patch\Data;
use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetup;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Eav\Model\Entity\Attribute\Set;
use Magento\Eav\Model\Entity\Attribute\SetFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchRevertableInterface;
use Magento\Framework\Setup\Patch\PatchVersionInterface;
class AddActivatedCustomerAttribute implements DataPatchInterface, PatchRevertableInterface, PatchVersionInterface
{
/**
* @var ModuleDataSetupInterface
*/
private $moduleDataSetup;
/**
* @var CustomerSetup
*/
private $customerSetupFactory;
/**
* @var SetFactory
*/
private $attributeSetFactory;
/**
* Constructor
*
* @param ModuleDataSetupInterface $moduleDataSetup
* @param CustomerSetupFactory $customerSetupFactory
* @param SetFactory $attributeSetFactory
*/
public function __construct(
ModuleDataSetupInterface $moduleDataSetup,
CustomerSetupFactory $customerSetupFactory,
SetFactory $attributeSetFactory
) {
$this->moduleDataSetup = $moduleDataSetup;
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
}
/**
* {@inheritdoc}
*/
public function apply()
{
$this->moduleDataSetup->getConnection()->startSetup();
/** @var CustomerSetup $customerSetup */
$customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
$customerEntity = $customerSetup->getEavConfig()->getEntityType(Customer::ENTITY);
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
/** @var $attributeSet Set */
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
$customerSetup->addAttribute(
Customer::ENTITY,
'activated',
[
'label' => 'Activated',
'input' => 'boolean',
'type' => 'int',
'source' => '',
'required' => false,
'position' => 333,
'visible' => true,
'system' => false,
'default' => 0,
'is_used_in_grid' => false,
'is_visible_in_grid' => false,
'is_filterable_in_grid' => false,
'is_searchable_in_grid' => false,
'backend' => \Magento\Customer\Model\Attribute\Backend\Data\Boolean::class
]
);
$attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'activated');
$attribute->addData([
'used_in_forms' => [
'adminhtml_customer'
]
]);
$attribute->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId
]);
$attribute->save();
$this->moduleDataSetup->getConnection()->endSetup();
}
public function revert()
{
$this->moduleDataSetup->getConnection()->startSetup();
/** @var CustomerSetup $customerSetup */
$customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
$customerSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, 'activated');
$this->moduleDataSetup->getConnection()->endSetup();
}
/**
* {@inheritdoc}
*/
public function getAliases()
{
return [];
}
/**
* {@inheritdoc}
*/
public static function getDependencies()
{
return [
];
}
public static function getVersion()
{
return '1.0.1';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment