127 lines
3.7 KiB
PHP
127 lines
3.7 KiB
PHP
<?php
|
|
/**
|
|
* Copyright © All rights reserved.
|
|
* See COPYING.txt for license details.
|
|
*/
|
|
declare(strict_types=1);
|
|
|
|
namespace Stemcell\ChineseDirectory\Setup\Patch\Schema;
|
|
|
|
use Magento\Customer\Model\Indexer\Address\AttributeProvider;
|
|
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\PatchRevertableInterface;
|
|
use Magento\Framework\Setup\Patch\SchemaPatchInterface;
|
|
|
|
class AddCityIdCustomerAddressAttribute implements SchemaPatchInterface, PatchRevertableInterface
|
|
{
|
|
|
|
/**
|
|
* @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(AttributeProvider::ENTITY);
|
|
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
|
|
|
|
/** @var $attributeSet Set */
|
|
$attributeSet = $this->attributeSetFactory->create();
|
|
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
|
|
|
|
$customerSetup->addAttribute(AttributeProvider::ENTITY, 'city_code', [
|
|
'type' => 'static',
|
|
'label' => 'City',
|
|
'input' => 'select',
|
|
'source' => 'Stemcell\ChineseDirectory\Model\Entity\Attribute\Source\CityId',
|
|
'required' => false,
|
|
'visible' => true,
|
|
'position' => 330,
|
|
'system' => false,
|
|
'user_defined' => false,
|
|
'backend' => ''
|
|
]);
|
|
|
|
$attribute = $customerSetup->getEavConfig()->getAttribute(AttributeProvider::ENTITY, 'city_code');
|
|
$attribute->addData([
|
|
'used_in_forms' => [
|
|
'adminhtml_customer_address',
|
|
'customer_address_edit',
|
|
'customer_register_address'
|
|
]
|
|
]);
|
|
$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, 'city_id');
|
|
|
|
$this->moduleDataSetup->getConnection()->endSetup();
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function getAliases()
|
|
{
|
|
return [];
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public static function getDependencies()
|
|
{
|
|
return [
|
|
|
|
];
|
|
}
|
|
}
|
|
|