Mage Check Facebook iconFacebook MAGE CHECK Mage Check Linkedin iconLinkedin MAGE CHECK Mage Check Skype iconSkype MAGE CHECK

Magento 2 How to create a Plugin

$ Magento 2 Plugin is a technical plugin for your better writing code.

General information

Interception Plugin is referred to a little Magento 2 extension that allows editing the behavior of any public class or method by intercepting a function call and running code either before or after or around the function call. By using this Magento 2 Plugin Interception, you can modify the behavior of a class while there is no need to change the class directly.

Maybe you still think the observers can help you do that fluently but some differences need to be pointed out. Particularly, not only create functions of a class using dependency injection but Interception plugin can also be confirmed with a sortOrder, that allows checking the chain and order on which plugins run. That is the reason why this plugin doesn’t make the class change and doesn't have any conflict with one another.


For a module developer as you, Magento 2 Interception plugin allows:


What options Magento 2 Interception plugin doesn't work with?

Create Plugin

Step 1: Declaring a Plugin

<config xmlns:xsi='' xsi:noNamespaceSchemaLocation='../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd'> <type name='MageCheck\Tutorial\Controller\Index\Example'> <plugin name='MageCheck_Tutorial_Plugin' type='MageCheck\Tutorial\Plugin\ExamplePlugin' sortOrder='10' disabled='false' /> </type> </config>

File path: app/code/MageCheck/Tutorial/etc/di.xml

Step 2: Create Plugin Controller

<?php namespace MageCheck\Tutorial\Controller\Index; class Example extends \Magento\Framework\App\Action\Action { protected $title; public function execute() { echo $this->setTitle('Welcome'); echo $this->getTitle(); } public function setTitle($title) { return $this->title = $title; } public function getTitle() { return $this->title; } }

File path: app/code/MageCheck/Tutorial/Controller/Index/Example.php

Step 3: Defining a Plugin

A plugin is a great way to expand or edit a public method’s behavior by using code before, after or around method.

First of all, please get an object that provides permission to all public methods of the observed method's class.

Before methods

<?php namespace MageCheck\Tutorial\Plugin; class ExamplePlugin { public function beforeSetTitle(\MageCheck\Tutorial\Controller\Index\Example $subject, $title) { $title = $title . ' to '; echo __METHOD__ . '</br>'; return [$title]; } }

Before methods are the first methods to run in an observed method, and these methods must have the same name to the observed one’s name while the prefix label is before.To apply the before methods for modifying the arguments of an observed method, you can return a modified argument. If there are multiple arguments, the returning will be carried out according to a range of those arguments. If the returning is invalid, that means the arguments for the observed method should not be modified.

File path: app/code/MageCheck/Tutorial/Controller/Index/Example.php

After methods

<?php namespace MageCheck\Tutorial\Plugin; class ExamplePlugin { public function afterGetTitle(\MageCheck\Tutorial\Controller\Index\Example $subject, $result) { echo __METHOD__ . '</br>'; return '<h1>'. $result . '' .'</h1>'; } }

After methods start running right after the observed method is finished, and these methods must have the same name to the observed one’s name while the prefix label is “after”. After methods take a responsibility of editing the results of an observed method in the correct way and being required to have a return value.

File path: app/code/MageCheck/Tutorial/Controller/Index/Example.php

Around methods

<?php namespace MageCheck\Tutorial\Plugin; class ExamplePlugin { public function aroundGetTitle(\MageCheck\Tutorial\Controller\Index\Example $subject, callable $proceed) { echo __METHOD__ . ' - Before proceed() </br>'; $result = $proceed(); echo __METHOD__ . ' - After proceed() </br>'; return $result; } }

Around methods allows the code to run before and after the observed method, so you can override a method. These methods must have the same name to the observed one’s name while the prefix label is “around”.
Before the arrange of the original method’s argument, a callable from around methods will be called to the next method in the chain, that means the next plugin or the observed function is also called.
Note: In case the callable is not declared, the calling to neither the next plugin nor the original method is achieved.

File path: app/code/MageCheck/Tutorial/Controller/Index/Example.php

This is the sample way to create a Plugin in Magento 2!

Contact us

Have you decided on an online Business Plan website?