12

I am trying to implement after plugin to the following method.

public function getCategoryUrl($category)
{
    if ($category instanceof ModelCategory) {
        return $category->getUrl();
    }
    return $this->_categoryFactory->create()->setData($category->getData())->getUrl();
}

Please note the $category parameter passed to the above method.

As the resolution, I have implemented below code.

public function afterGetCategoryUrl(\Magento\Catalog\Helper\Category $subject, $result)
{
    return $result;
} 

Now, my question is: How do I get the $category parameter passed in parent method to my plugin? I just want to modify result based on the certain value in $category object.

3 Answers 3

22

If you need input parameters and you also need to change output, you can use an around plugin or an after plugin:

Example using around:

public function aroundGetCategoryUrl(
    \Magento\Catalog\Helper\Category $subject,
    \Closure $proceed,
    $category
) {
   $originalResult = $proceed($category);
   
   if (...) {
      ...
      return $otherResult;
   }

   return $originalResult;
} 

Example using after:

public function afterGetCategoryUrl(
    \Magento\Catalog\Helper\Category $subject,
    $result,
    $category
) {
   if (...) {
      ...
      return $otherResult;
   }

   return $result;
} 

Just a note:

Please notice that if you are going to change an internal behaviour, a preference could be a better option that a plugin. It depends on what you are going to do.

7
  • I just want to modify the result. Apr 21, 2017 at 14:10
  • See my updated post. Apr 21, 2017 at 14:12
  • Yes that (AroundPlugin) Works but it would be great if we can achieve this using (AfterPlugin). Apr 21, 2017 at 14:22
  • There is no way with "after" plugins since it is not intended to work this way, what you need can only be done with "around" plugins. Apr 21, 2017 at 14:24
  • Thanks for your quick response. i am fine with "around" as well Apr 21, 2017 at 14:30
15

Since Magento 2.2 it is possible to have input parameters in after plugin

namespace My\Module\Plugin;

class AuthLogger
{
    private $logger;

    public function __construct(\Psr\Log\LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    /**
     * @param \Magento\Backend\Model\Auth $authModel
     * @param null $result
     * @param string $username
     * @return void
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function afterLogin(\Magento\Backend\Model\Auth $authModel, $result, $username)
    {
        $this->logger->debug('User ' . $username . ' signed in.');
    }
}

See Magento documentation for details https://devdocs.magento.com/guides/v2.2/extension-dev-guide/plugins.html#after-methods

0

Try below after and around plugins

app/code/VendorName/ModuleName/etc

di.xml

<?xml version="1.0"?>
<!--
  Msquare
  @author      Mahesh Makwana
  -->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Helper\Category">
        <plugin name="plugin_with_params"
                type="VendorName\ModuleName\Plugin\CategoryPlugin" sortOrder="10" />
    </type>
</config>

Around Plugin

app/code/VendorName/ModuleName/Plugin

CategoryPlugin.php

<?php
/*
 * Msquare
 * @author      Mahesh Makwana
 */
namespace VendorName\ModuleName\Plugin\Controller\Adminhtml;

use Magento\Catalog\Helper\Category;
use Psr\Log\LoggerInterface;

class CategoryPlugin
{
    protected $logger;

    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    public function aroundGetCategoryUrl(Category $subject, callable $proceed, $category)
    {
        $result = $proceed($category);
        // return $result;
        
        //$this->logger->debug('Plugin  called');
        ...................................
        ..... Add Your Logic Here .........
        ...................................

        return $result;
    }
}

After Plugin

app/code/VendorName/ModuleName/Plugin

CategoryPlugin.php

<?php
/*
 * Msquare
 * @author      Mahesh Makwana
 */
namespace VendorName\ModuleName\Plugin\Controller\Adminhtml;

use Magento\Catalog\Helper\Category;
use Psr\Log\LoggerInterface;

class CategoryPlugin
{
    protected $logger;

    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    public function afterGetCategoryUrl(Category $subject, $result, $category)
    {
        // return $result;  -> Original Result

        //$this->logger->debug('Plugin  called');
        ...................................
        ..... Add Your Logic Here .........
        ...................................

        return $result;
    }
}

Note: Please be aware that when considering changes to internal behavior in class, using a preference might be a more suitable option than a plugin. The choice between preference and plugin depends on the specific task or modification you intend to make within the class.

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.