I am trying to override a Magento 2 block with the help of di.xml and Plugin(Interceptor). But I am getting an error message
[2017-09-27 05:17:33] main.CRITICAL: Packagename\Modulename\Plugin\Block\Export doesn'n extend \Magento\Framework\Data\Form\Element\AbstractElement [] []
My di.ml is as below
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\OfflineShipping\Block\Adminhtml\Form\Field\Export"> <plugin name="zipcodeExport" type="Packagename\Modulename\Plugin\Block\Export" /> </type> </config>
and my plugin file code is as below
namespace Packagename\Modulename\Plugin\Block; class Export { public function beforeGetElementHtml(\Magento\OfflineShipping\Block\Adminhtml\Form\Field\Export $subject) { $logger = \Magento\Framework\App\ObjectManager::getInstance()->get('\Psr\Log\LoggerInterface'); $logger->debug(__METHOD__ . ' -111- ' . __LINE__); } }
Try your plugin with extend AbstractElement.php file ,
namespace Packagename\Modulename\Plugin\Block; class Export extends \Magento\Framework\Data\Form\Element\AbstractElement { public function beforeGetElementHtml(\Magento\OfflineShipping\Block\Adminhtml\Form\Field\Export $subject) { $logger = \Magento\Framework\App\ObjectManager::getInstance()->get('\Psr\Log\LoggerInterface'); $logger->debug(__METHOD__ . ' -111- ' . __LINE__); } }
if issue solved, Click Kudos/Accept as solutions.
Hi Rakesh,
Thanks for your reply.
I tried your solution and it shows another error
[2017-09-27 08:15:25] main.CRITICAL: Recoverable Error: Argument 1 passed to Magento\Framework\Data\Form\Element\AbstractElement::__construct() must be an instance of Magento\Framework\Data\Form\Element\Factory, instance of Magento\Framework\ObjectManager\ObjectManager given, called in D:\xampp\htdocs\Magento217\vendor\magento\framework\ObjectManager\Factory\AbstractFactory.php on line 93 and defined in D:\xampp\htdocs\Magento217\vendor\magento\framework\Data\Form\Element\AbstractElement.php on line 72 [] []
In the Magento document(http://devdocs.magento.com/guides/v2.0/extension-dev-guide/plugins.html) it describes that we don't need to extend the plugin as it uses d1.xml file for overriding a block file which was already extended to a Magento file