0

I am trying to look for a proper document where it is clearly told about where to place my interceptor code for continuous programming

This document https://devdocs.magento.com/guides/v2.2/extension-dev-guide/plugins.html explains clearly about before around after methods but it does not say where to properly place code to continuously program and test

should I place my code inside vendor folder and keep on programming ? will it auto load? or should I place into to the "/" location of the Magento installation ?

If I keep on changing code, will it detect?

My setup is d:\dev\xampp\htdocs\magento where will place the code

1 Answer 1

0

You should create a custom module for your code, which can either be placed in app/code/Your_Namespace/Your_Module or loaded via composer, in which case it will exist in vendor/yournamespace/yourmodule.

See https://devdocs.magento.com/videos/fundamentals/create-a-new-module/ for more details on creating a module.

Regardless which you choose, you must generate the necessary interceptor files by running bin/magento setup:di:compile once you have added the plugin to di.xml. Once the interceptor files have been generated, you can modify and test the code without compiling so long as you do not change the constructor or namespace of your interceptor method. There are likely other cases in which you must regenerate, so try that first when debugging issues.

Technically, Plugin (Interceptor) classes can be placed anywhere you want within the namespace root of your module, but they are typically placed in the Your_Namespace/Your_Module/Plugin/ directory.

Here is an example of a plugin:


Your_Namespace/Your_Module/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsinoNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Model\Product">
        <plugin name="afterGetName" type="Your_Namespace\Your_Module\Plugin\AfterGetName" sortOrder="1" disabled="false"/>
    </type>
</config>

the types "name" argument being the class containing the public method you would like to intercept. Plugin "name" argument can be whatever you want, but should make sense. Plugin "type" argument being the path to your new plugin class. Plugin "sortOrder" determines the order in which interceptors of the same method are executed. Plugin "disabled" determines whether the interceptor is included in the execution at all.

Your_Namespace/Your_Module/Plugin/AfterGetName.php

<?php

namespace Your_Namespace\Your_Module\Plugin;

class AfterGetName
{
    public function afterGetName(
        \Magento\Catalog\Model\Product $subject,
        $result
    ) {
        if ($result) {
            return 'Awesome ' . $result;
        }
    }
}

This after plugin (interceptor) will intercept the getName() method of the $subject argument and execute this code after the original method has finished it's execution. The result of the original method is passed in as the $result argument. Here, we check that a $result was returned, and if so we prepend the string with 'Awesome'. So now, when we call $product->getName() we will get the result 'Awesome Yoga Ball', assuming $product is an instance of Magento\Catalog\Model\Product, and the product's name is 'Yoga Ball'.

5
  • I think you are trying the explain the same thing which told in the documentation. my setup is d:\dev\xampp\htdocs\magento where should I place that code ? I want to keep on coding by simply doing a screen refresh like a normal php code Commented Jun 22, 2018 at 19:14
  • You should create a custom module for your code, which can either be placed in app/code/YourNamespace/YourModule or loaded via composer, in which case it will exist in vendor/yournamespace/yourmodule. Regardless which you choose, you must generate the necessary interceptor files by running 'bin/magento setup:di:compile' once you have added the plugin to di.xml. Once the interceptor has been added, you can modify and test the code without compiling as long as you do not change the constructor or namespace of your interceptor method. Commented Jun 22, 2018 at 19:49
  • yes, I will try this, please replace your answer with this comment as answer, then I will accept it once it gets success Commented Jun 24, 2018 at 12:46
  • @Dickens Answer updated. Hope this helps! Commented Jun 25, 2018 at 13:46
  • @Dickens did this help you? If it did, please accept as the answer. Commented Jul 3, 2018 at 22:52

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.