6

If a class has plugins and the deploy mode is set to developer the ObjectManager is supposed to generate an Interceptor of the class as needed. That's what happens when running bin/magento setup:di:compile.

In my case some Inteceptors are not generated even tho the class has at least one plugin.

I'm trying to add a plugin for a third party class. di.xml and the after method are declared correctly and the target method is called. Due to the missing Interceptor the plugin isn't executed.

If I run bin/magento setup:di:compile the Interceptor is generated and the plugin works as intended.

I tried to debug into \Magento\Framework\Interception\ObjectManager\Config\Developer::getInstanceType but here I don't get an instance of the class I target with the plugin.

Any ideas what the problem could be?

Update:
I've debugged into the index.php and checked the state of $app. _appMode is set to developer. So I don't think the app mode is overwritten somewhere.

3
  • did you try to upgrade first before running compile?
    – fmsthird
    Commented Apr 18, 2019 at 21:31
  • I did run a upgrade. The compile when I run bin/magento setup:di:compile works just fine but I am in developer mode so it should not be necessary to trigger it manually
    – Daniel
    Commented Apr 23, 2019 at 9:59
  • This started happening to me recently with the upgrade to 2.4. If I define a plugin in an area like adminhtml/di.xml instead of the global di.xml, it's not generated on the fly as expected in developer mode. I do have an all docker local environment, so I'm think that may be the cause as none of my co-workers have the same issue.
    – David Tay
    Commented Dec 29, 2020 at 14:57

4 Answers 4

5

It happens because your class contains some faulty statement. In my case it was wrongly lowercased name of one of the classes in the "use" directive, and the same error in the list of constructor's arguments. It was not causing an error during the "setup:di:compile" (strangely enough) but it prevented interceptor from being automatically generated.

If you do not see any faults in your class, try to remove EVERYTHING from your class (except maybe the "execute" method) - the interceptor should start to be automatically generated. Then gradually add the desired functionality and check when intercetor will stop to be automatically generated - it can help you to find the faulty statement.

1
  • 1
    In order do find the exact error message why the class can't be generated it is possible to set a debug breakpoint in \Magento\Framework\Code\Generator\EntityAbstract::generate
    – Daniel
    Commented Jul 29, 2022 at 10:20
2

I'm right now on Magento 2.4.5-p1

Was facing the same issue, in my situation, setup upgrade helped, then I debugged a bit, and it seems you need to clear the cache, probably the di.xml is cached :)

There are several prequsition that you need to know but the most important are those 6

  1. remember to have your module enabled
  2. clear cache after you created plugin (di.xml caching)
  3. have the directory generated/metadata empty
  4. after a interceptor is generated it won't update again, until it's deleted, so you need to do something about it. So assuming you are making a plugin on some magento module, that interceptor is probably already there, so you will need to manually remove it.
  5. Oh and ofc.. functionality from the main class (the one you attached the plugin to) needs to be executed, not sure if class or the method.
  6. developer mode probably needs to be set :)
-1

Most probably there is an error in your class.

Check the constructor for typos or non-existing dependencies.

-3
  • It will not automatically be generated when you are using production mode. Please check your mode by command:

    php bin/magento deploy:mode:show
    
  • Enable this setting:

Stores => Configuration => Advanced => Developer => Template Settings

1
  • 1
    already checked it is development
    – Daniel
    Commented Apr 18, 2019 at 16:53

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.