controller - removeAttribute magento 2 -
how remove product attribute in backand controller in magento 2.1?
in magento 1.* was:
$setup = mage::getresourcemodel('catalog/setup','catalog_setup'); $setup->removeattribute('catalog_product','my_attribute');
edit: not offer me use install/uninstall methods. read attention question: "remove attribute in backand controller"
edit 2: find answer
namespace company\module\controller\adminhtml\shoptheme; //optional use magento\eav\setup\eavsetup; use magento\eav\setup\eavsetupfactory; use magento\framework\setup\moduledatasetupinterface; class removeattribute extends \magento\backend\app\action { private $datasetup; private $eavsetupfactory; public function __construct( \magento\backend\app\action\context $context, \magento\framework\setup\moduledatasetupinterface $datasetup, \magento\eav\setup\eavsetupfactory $eavsetupfactory ) { $this->datasetup = $datasetup; $this->eavsetupfactory = $eavsetupfactory; parent::__construct($context); } public function execute() { $eavsetup = $this->eavsetupfactory->create(['setup' => $this->datasetup]); $eavsetup->removeattribute( \magento\catalog\model\product::entity, 'prod_special_descr'); $this->messagemanager->addsuccess('attribute removed'); $this->_redirect('admin/dashboard/'); } }
you can remove attribute using below setup script :
<?php namespace namespace\company\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; class installdata implements installdatainterface { private $eavsetupfactory; public function __construct(eavsetupfactory $eavsetupfactory) { $this->eavsetupfactory = $eavsetupfactory; } public function install(moduledatasetupinterface $setup, modulecontextinterface $context) { $eavsetup = $this->eavsetupfactory->create(['setup' => $setup]); $eavsetup->removeattribute( \magento\catalog\model\product::entity, 'my_attribute'); } }
Comments
Post a Comment