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.
4 Answers 4
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.
-
1In 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::generateDaniel– Daniel2022年07月29日 10:20:54 +00:00Commented Jul 29, 2022 at 10:20
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
- remember to have your module enabled
- clear cache after you created plugin (di.xml caching)
- have the directory generated/metadata empty
- 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.
- 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.
- developer mode probably needs to be set :)
Most probably there is an error in your class.
Check the constructor for typos or non-existing dependencies.
It will not automatically be generated when you are using production mode. Please check your mode by command:
php bin/magento deploy:mode:showEnable this setting:
Stores => Configuration => Advanced => Developer => Template Settings
-
1already checked it is developmentDaniel– Daniel2019年04月18日 16:53:37 +00:00Commented Apr 18, 2019 at 16:53
Explore related questions
See similar questions with these tags.
bin/magento setup:di:compileworks just fine but I am in developer mode so it should not be necessary to trigger it manually