Hi am working on a custom module that works based on custom product attribute. below is the code of installdata.php file
<?php
namespace Vendor\Module\Setup;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
/**
* @codeCoverageIgnore
*/
class InstallData implements InstallDataInterface
{
/**
* EAV setup factory
*
* @var EavSetupFactory
*/
private $eavSetupFactory;
/**
* Init
*
* @param EavSetupFactory $eavSetupFactory
*/
public function __construct(EavSetupFactory $eavSetupFactory)
{
$this->eavSetupFactory = $eavSetupFactory;
}
/**
* {@inheritdoc}
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
/** @var EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
/**
* Add attributes to the eav/attribute
*/
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'no_free_shipping',
[
'group' => 'General',
'type' => 'int',
'backend' => '',
'frontend' => '',
'label' => 'no_free_shipping',
'input' => 'boolean',
'class' => '',
'source' => \Magento\Eav\Model\Entity\Attribute\Source\Boolean::class,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => false,
'default' => '1',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'used_in_product_listing' => false,
'unique' => false,
'apply_to' => 'simple,configurable,bundle'
]
);
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'no_flat_rate',
[
'group' => 'General',
'type' => 'int',
'backend' => '',
'frontend' => '',
'label' => 'no_flat_rate',
'input' => 'boolean',
'class' => '',
'source' => \Magento\Eav\Model\Entity\Attribute\Source\Boolean::class,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => false,
'default' => '1',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'used_in_product_listing' => false,
'unique' => false,
'apply_to' => 'simple,configurable,bundle'
]
);
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'no_table_rate',
[
'group' => 'General',
'type' => 'int',
'backend' => '',
'frontend' => '',
'label' => 'no_table_rate',
'input' => 'boolean',
'class' => '',
'source' => \Magento\Eav\Model\Entity\Attribute\Source\Boolean::class,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => false,
'default' => '1',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'used_in_product_listing' => false,
'unique' => false,
'apply_to' => 'simple,configurable,bundle'
]
);
}
}
What i want is to remove/unassigned these attributes automatically when the magento module is removed or disable but unfortunately am not able to find a solution for this. any suggestion please guide THANKS IN ADVANCE
1 Answer 1
You can use unistall script to remove/unassigned the attributes automatically when the magento module is uninstalled.
To uninstall any module in magento, we use the below command :
$ magento module:uninstall [--backup-code] [--backup-media] [--backup-db] [-r|--remove-data] [-c|--clear-static-content] \
{ModuleName} ... {ModuleName}
where {ModuleName} specifies the module name in <VendorName>_<ModuleName> format
If --remove-data is specified, removes the database schema and data defined in the module’s Uninstall classes.
Check the docs for more details : http://devdocs.magento.com/guides/v2.2/install-gde/install/cli/install-cli-uninstall-mods.html#instgde-cli-uninst-mod-uninst
So this indicates for a module to run a script at uninstall, the module should be uninstalled using the --remove-data option, or shorthand -r. So if the users of your module do not use that option, the script won't run.
Create Uninstall.php at path app/code/Company/MyModule/Setup/Uninstall.php
<?php
namespace Company\MyModule\Setup;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\Db\Select;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\UninstallInterface as UninstallInterface;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
/**
* Class Uninstall
*/
class Uninstall implements UninstallInterface
{
/**
* EAV setup factory
*
* @var EavSetupFactory
*/
private $_eavSetupFactory;
private $_mDSetup;
/**
* Init
*
* @param EavSetupFactory $eavSetupFactory
*/
public function __construct(
EavSetupFactory $eavSetupFactory,
ModuleDataSetupInterface $mDSetup
)
{
$this->_eavSetupFactory = $eavSetupFactory;
$this->_mDSetup = $mDSetup;
}
public function uninstall(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$installer = $setup;
$installer->startSetup();
/** @var AdapterInterface $connection */
$connection = $installer->getConnection();
$connection->dropTable('hella_test'); // remove table hella_test
$installer->endSetup();
/** @var EavSetup $eavSetup */
$eavSetup = $this->_eavSetupFactory->create(['setup' => $this->_mDSetup]);
$eavSetup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY, 'test_uninstall'); // removing the installed attribute
}
}
NOTE : Uninstall command works only with a module installed using a composer or defined as Composer packages.