2

I have a base plugin which accepts blockName(String) as a parameter.

Based on that value I have created three virtual type plugins and used them.

Below are the codes.

di.xml

 <virtualType name="Vendor\ModuleName\Plugin\FirstPlugin"
              type="Vendor\ModuleName\Plugin\BasePlugin">
     <arguments>
         <argument name="blockName" xsi:type="string">dummyone</argument>
     </arguments>
 </virtualType>

 <virtualType name="Vendor\ModuleName\Plugin\SecondPlugin"
              type="Vendor\ModuleName\Plugin\BasePlugin">
     <arguments>
         <argument name="blockName" xsi:type="string">dummytwo</argument>
     </arguments>
 </virtualType>

 <virtualType name="Vendor\ModuleName\Plugin\ThirdPlugin"
              type="Vendor\ModuleName\Plugin\BasePlugin">
     <arguments>
         <argument name="blockName" xsi:type="string">dummy</argument>
     </arguments>
 </virtualType>

 <type name="Some\Core\Class">
     <plugin name="Vendor_ModuleName::FirstPlugin"
                type="Vendor\ModuleName\Plugin\FirstPlugin" sortOrder="1"/>
 </type>

 <type name="Some\Other\Core\Class">
        <plugin name="Vendor_ModuleName::SecondPlugin"
                type="Vendor\ModuleName\Plugin\SecondPlugin" sortOrder="1"/>
 </type>

 <type name="Some\Another\Core\Class">
        <plugin name="Vendor_ModuleName::ThirdPlugin"
                type="Vendor\ModuleName\Plugin\ThirdPlugin" sortOrder="1"/>
 </type>

BasePlugin.php

class BasePlugin
{
    public $blockNames = [];
    public $layout;
    public $resultJsonFactory;

    public function __construct(JsonFactory $resultJsonFactory, Layout $layout, string $blockName)
    {
        $this->resultJsonFactory    = $resultJsonFactory;
        $this->layout               = $layout->getLayout();
        $this->blockNames        [] = $blockName;
    }

    public function afterExecute($subject, $result)
    {
     //After plugin code.
    }

The code is executing as expected, in the front end there are no errors. The Base plugin code is being execute every time with the correct constructor parameters for the three virtual plugins.

But when I run setup:di:compile, I am getting the error stating Plugin class Vendor\ModuleName\Plugin\FirstPlugin doesn't exist.

Why is that happening? Is there something wrong in my way of declaring the virtual types?

Any help is appreciated.

4
  • Did you check namespace declaration for possibly a typo? Commented Dec 21, 2020 at 6:38
  • That virtual type class is not existing as a hard coded file. It is being generated on the go. So providing a namespace doesn't really matter. Also, whenever the main class execution is happening, the flow of the code comes into the BasePlugin.php with the correct parameters. Commented Dec 21, 2020 at 6:41
  • Not sure what do you try to archieve with dependency plugin? But as i remembered magento announcement plugin not work with virtual type
    – mrtuvn
    Commented Dec 31, 2020 at 18:35
  • @mrtuvn yes I know that plugins will not work for virtual types but magento didn't mention anything about creating virtual type plugins. The entire plugin is a virtual created for a very real core class. Commented Sep 11, 2021 at 9:59

1 Answer 1

0

UPD: the solution below does not work because of a Magento bug https://github.com/magento/magento2/issues/33980

So there is no error while doing setup:di:compile but the plugins actually do not get generated in the proper way.


I ran into exactly the same problem and fortunately managed to resolve it.

My virtual type declaration in app/code/Vendor/Module/etc/frontend/di.xml was like this:

...
<virtualType name="HideBlockIfCheckoutDisabledPlugin" type="\Vendor\Common\Plugin\Framework\View\Element\AbstractBlock">
    <arguments>
        <argument name="condition" xsi:type="object">\Vendor\Checkout\Condition\CheckoutEnabled</argument>
    </arguments>
</virtualType>
...

<type name="Magento\Checkout\Block\Cart\Sidebar">
    <plugin name="vendor_checkout_block_hide_minicart" type="HideBlockIfCheckoutDisabledPlugin"/>
</type>
...

It worked fine and as expected but threw an error on setup:di:compile.

Once I moved the virtual type declaration to the global scope DI file app/code/Vendor/Module/etc/di.xml the compilation went through without errors. So I guess that was it: virtual type plugins must be declared in the global scope.

It looks like a Magento bug to me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.