dqg63264 2019-03-28 06:19
浏览 31

使用插件方法覆盖核心块

I have created a module in which i am overwriting core functionality of product listing widget. I can achieve this using preference but i want to understand plugin method.. Below is my code for frontend/di.xml

<?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\CatalogWidget\Block\Product\ProductsList">
        <plugin name="widget_product_listing_add_attribute" type="Test\CatalogWidget\Plugin\Block\Product\ProductsListAddAttributePlugin" sortOrder="1"/>
    </type>
</config>

and below is my code for plugin block.

namespace Test\CatalogWidget\Plugin\Block\Product;

class ProductsListAddAttributePlugin
{
    public function beforeCreateCollection(\Magento\CatalogWidget\Block\Product\ProductsList $subject, \Magento\Catalog\Model\ResourceModel\Product\Collection $result)
    {
        /**
        * @var \Magento\CatalogWidget\Block\Product\ProductsList $subject
        * @var \Magento\Catalog\Model\ResourceModel\Product\Collection $result
        */

        die('ProductsListAddAttributePlugin before....');

    }
}

after installing module and running di:compile.. when i reload the page i am getting below error.

Fatal error: Uncaught ArgumentCountError: Too few arguments to function Test\CatalogWidget\Plugin\Block\Product\ProductsListAddAttributePlugin::beforeCreateCollection(), 1 passed in C:\xampp\htdocs\projects\hello\vendor\magento\framework\Interception\Interceptor.php on line 121 and exactly 2 expected in C:\xampp\htdocs\projects\hello\app\code\Test\CatalogWidget\Plugin\Block\Product\ProductsListAddAttributePlugin.php:6 Stack trace: #0 C:\xampp\htdocs\projects\hello\vendor\magento\framework\Interception\Interceptor.php(121): Test\CatalogWidget\Plugin\Block\Product\ProductsListAddAttributePlugin->beforeCreateCollection(Object(Magento\CatalogWidget\Block\Product\ProductsList\Interceptor))

1 C:\xampp\htdocs\projects\hello\vendor\magento\framework\Interception\Interceptor.php(153):

Magento\CatalogWidget\Block\Product\ProductsList\Interceptor->Magento\Framework\Interception{closure}()

2 C:\xampp\htdocs\projects\hello\generated\code\Magento\CatalogWidget\Block\Product\ProductsList\Interceptor.php(26):

Mag in C:\xampp\htdocs\projects\hello\app\code\Test\CatalogWidget\Plugin\Block\Product\ProductsListAddAttributePlugin.php on line 6

  • 写回答

2条回答 默认 最新

  • dsaxw4201 2019-03-28 07:00
    关注

    Have you run setup:di:compile and clear the cache? after updating the beforeCreateCollection method?

    It looks like you're correctly passing two arguments through, but it might be referring to an older DI.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答