Hello Friends,
I am trying to rewrite Admin Product Grid Block via below code as i need to rewrite method _prepareMassaction() but rewrite is not working.
I have place a below code in
Magento/Demo/etc/adminhtml/di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Magento\Catalog\Block\Adminhtml\Product\Grid" type="Magento\Demo\Block\Adminhtml\Product\Grid" /> </config>
and
Magento\Demo\Block\Adminhtml\Product\Grid.php
namespace Magento\Demo\Block\Adminhtml\Product; class Grid extends \Magento\Catalog\Block\Adminhtml\Product\Grid { protected $_logger; protected $_eventManager; public function __construct(\Psr\Log\LoggerInterface $logger, \Magento\Framework\Event\ManagerInterface $eventManager,) { $this->_objectManager = $objectManager; $this->_logger = $logger; $this->_eventManager = $eventManager; //parent::__construct($data); } protected function _prepareMassaction() { parent::_prepareMassaction(); $this->_eventManager->dispatch('product_grid_massaction_event', ['grid' => $this]); } }
I need to add custom Mass Actions by using above method.
Thanks for you answer in advance.
You should not rewrite the admin product grid, because it will break (and will be broken by) all the other extensions, which try to do the same.
Use the plugins instead: http://devdocs.magento.com/guides/v2.0/extension-dev-guide/plugins.html
Hello @Dmitry_Fedyuk
Thanks for your answer.
I have used the below code in di.xml file.
<type name="Magento\Catalog\Block\Adminhtml\Product\Grid"> <plugin name="magento-catalog-adminhtml-product-grid-plugin" type="Magento\Demo\Block\Adminhtml\Product\Grid" sortOrder="10"/>
</type>
I want to rewrite protected function _prepareMassaction() of
Magento\Catalog\Block\Adminhtml\Product\Grid
Please tell me what i will use to rewrite above method in
Magento\Demo\Block\Adminhtml\Product\Grid
Let me know as above class is protected.
It impossible to plug-in to a protected method, but the method fires an event, and you can handle it.
How to add a custom massaction to the Magento 2 backend product grid? https://mage2.pro/t/997
The events is another good way to add a new functionality because you are not breaking another extensions.
Hello @Dmitry_Fedyuk
Can you please give me one example of https://mage2.pro/t/topic/997 as how can i use it to my extension?
Thanks in advnace.