0

lets say I've created a custom module that will extend some functionality as plugins from Magento_Catalog core module and lets call it MyModule. MyModule has already been registered and activated.

I have defined my module and the sequence for Magento_Catalog in module.xml I have created the plugins and declared them in etc/adminhtml/di.xml file

1) Does the Plugin that extend the core Magento module class has to be in the same directory structure as the parent that it extend? For example, plugin for MyVendor\MyModule\Block\Adminhtml\Product\Edit.php to be in

namespace MyVendor\MyModule\Block\Adminhtml\Product\MyEdit.php

2) I am not entirely sure what exactly should I do so MyModule gets invoked when I go to the page ?

http://mydomain/admin/catalog/product/edit/id/8

Thank you.

3

1) Does the Plugin that extend the core Magento module class has to be in the same directory structure as the parent that it extend? For example:

No, plugins are stand-alone classes.

2) I am not entirely sure what exactly should I do so MyModule gets invoked when I go to the page ?

This question doesn't quite make sense. A plugin is basically a "listener" for calls on other public class methods in Magento. If you have a plugin for class A, you can setup before, after, and "around" listeners for specific methods on class A. The before listener will allow you to change parameters to the method call. the after listener will allow you to change the value of the results, and the around listener will allow you to do extra things before the method is called, or cancel the method call entirely. You may want to review my object manager series for all the features of Magento 2's object system. This should help you decide how you want to customize Magento's system behavior with your plugin.

  • yes, I will do that, thank you. I understand why it doesn't make sense, i will fix the question part that is confusing – Lachezar Raychev Feb 25 '16 at 15:16
2

you need to add in the di.xml file of your module this

<preference for="Magento\Catalog\Block\Adminhtml\Product\Edit" type="MyVendor\MyModule\Block\Adminhtml\Product\MyEdit" />

This will tell Magento to use your class instead of the core one.

  • isn't preference used to like totally re-write the class and plugin just to extend it ? can't the Magento\Catalog\Block\Adminhtml\Product\Edit be used and my plugin to be used as well ? Do I have to do something in the adminhtml/routes.xml file ? – Lachezar Raychev Feb 25 '16 at 14:08
  • sorry. I don't understand. You want your class to be used instead of the original class or you don't? – Marius Feb 25 '16 at 14:24
  • I want to inherit the class functionality without rewriting it, and use my new class to like add a new tab in the settings and attaching a template to it – Lachezar Raychev Feb 25 '16 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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