5

I am trying to create a plugin that fires after \Magento\Sales\Controller\Adminhtml\Shipment\AbstractShipment\PrintAction::execute.
So I have my plugin setup in di.xml and an afterExecute() method, but it doesn't seem to get fired.
Now I know there are some limitations for plugins, but I can't read anything about abstract classes.
Can plugins / interceptors be used on abstract classes?

Marius
199k55 gold badges431 silver badges837 bronze badges
asked Nov 8, 2016 at 14:34

2 Answers 2

5

Just to answer the real question here: Yes, it is indeed possible to define plugins / interceptors on abstract classes. Doing this works similar to other plugin definitions: An Interceptor class will be generated for each subclass of the abstract class.

answered Feb 7, 2020 at 15:03
0

Not 100% sure about this, but I think the plugins work for classes that get instantiated.
The object manager instantiates the my\class\Interceptor instead of my\class when there are plugins for that class.
Since the abstract classes never get instantiated you won't have an \Interceptor class generated that should call the plugins.
But...
I assume there is a class that extends \Magento\Sales\Controller\Adminhtml\Shipment\AbstractShipment\PrintAction and that one is actually instantiated and used.
You can try to create the plugin for that class.
Even if that class does not contain the execute method, it is still available (via parent class) so this means you should be able to add a plugin to it.

answered Nov 8, 2016 at 14:45
5
  • Thanks for the tip. I moved my intercepter one level 'lower' but it's not yet getting picked up. It picks my plugin, but as soon as magento/framework/Interception/Interceptor.php::146 is executed (which is the call to the original method $result = parent::$method(...array_values($arguments));), the headers already get sent and the PDF is sent. Effectively ignoring my LISTENER_AFTER. Commented Nov 8, 2016 at 16:02
  • maybe you should try an around plugin and call the original method inside your plugin. Commented Nov 8, 2016 at 16:05
  • Already tried around. Before the callable the code gets executed, but as soon as I place it after the callable it doesn't work. I'm trying to fetch the result of the fileFactory. Could it be that it outputs the header and exits the script? Since I can't find I way to execute code after the PDF generating, I'm now executing it on before (which works) but it feels wrong since I only want to execute the code if I'm sure the PDF generation works. Commented Nov 8, 2016 at 16:09
  • Got it! Turns out that Magento\Framework\App\Response\Http\FileFactory::create() is checking if $content !== null and if it's not it indeed starts sending headers and (as I suggested) is doing an exit(0), practically making it impossible for plugins to hook into after for this method. I think I'm going to report this as an issue... Commented Nov 8, 2016 at 16:12
  • Filed: github.com/magento/magento2/issues/7356 Commented Nov 8, 2016 at 16:19

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.