Argument Replacement in Magento2

Argument Replacement in Magento2

Hello Friends,

In this blog I am going to cover Argument Replacement in Magento2. So please read my this blog till end so you don’t miss any important steps

If you want to change default value or class of an constructor argument then you can do that easily using argument replacement feature of magento2 via di.xml file

Take a look into below example to understand it.

First we will create a simple example of addition

First of all you need to create module , if you don’t know how to create module ready my previous blog

This example we will understanding by console command

Create a Class file as below

<?php
namespace Mageacademy\ArgumentReplacement\Console;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class FirstCommand extends Command
{
    const NAME = 'name';

    protected $a;

    protected $b;
    
    public function __construct(        
        $a = 7,
        $b = 3,
        string $name = null)
    {
        $this->a = $a;
        $this->b = $b;         
        parent::__construct($name);
    }

    /**
     * @inheritDoc
     */
    protected function configure()
    {
        $this->setName('mageacademy:argumentreplacement:showresult');
        $this->setDescription('Argument Replacement');

        parent::configure();
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $c = $this->a + $this->b;

        $output->writeln($this->a .' + '. $this->b.' = '. $c);
    }
}

Now we will run console command as below

php bin/magento mageacademy:argumentreplacement:showresult

We will see below result

7 + 3 = 10

Now we will replace argument default’s value using di.xml file as below

<type name="Mageacademy\ArgumentReplacement\Console\FirstCommand">
        <arguments>
            <argument name="a" xsi:type="string">20</argument>
        </arguments>
    </type>

Now we will clear cache and run below command

php bin/magento mageacademy:argumentreplacement:showresult

We will see below result

20 + 3 = 23

You can see instead of taking constructor argument value it tooks value from di.xml as we have override the default value using argument replacement feature via di.xml

Now we will take 1 more example to replace class object

Create a class as below

20 + 3 = 23

<?php
namespace Mageacademy\ArgumentReplacement\Console;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class FirstCommand extends Command
{
    const NAME = 'name';

    protected $a;

    protected $b;

    protected $helperClass;

    public function __construct(
        \Mageacademy\ArgumentReplacement\Helper\HelperClassA $helperClass,
        $a = 7,
        $b = 3,
        string $name = null)
    {
        $this->a = $a;
        $this->b = $b;
        $this->helperClass = $helperClass;
        parent::__construct($name);
    }

    /**
     * @inheritDoc
     */
    protected function configure()
    {
        $this->setName('mageacademy:argumentreplacement:showresult');
        $this->setDescription('Argument Replacement');

        parent::configure();
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $d = $this->helperClass->calculateResult($this->a,$this->b);

        $output->writeln($d);
    }
}

and create HelperClassA as below

<?php

namespace Mageacademy\ArgumentReplacement\Helper;

use Magento\Framework\App\Helper\AbstractHelper;

class HelperClassA extends AbstractHelper
{

    public function calculateResult($a,$b)
    {         
        return $a .' + '.$b . ' = ' .($a + $b);
    }

}

Now we will clear cache and run below command

php bin/magento mageacademy:argumentreplacement:showresult

We will see below result

7 + 3 = 10

Now we will replace class object as below

<type name="Mageacademy\ArgumentReplacement\Console\FirstCommand">
        <arguments>
            <argument name="helperClass" xsi:type="object">Mageacademy\ArgumentReplacement\Helper\HelperClassB</argument>
        </arguments>
    </type>

Here we will replace HelperClassA with HelperClassA and create HelperClassB as below

<?php

namespace Mageacademy\ArgumentReplacement\Helper;

class HelperClassB extends HelperClassA
{
    public function calculateResult($a,$b)
    {
        return $a .' - '.$b . ' = ' .($a + $b);
    }
}

Now we will clear cache and run below command

php bin/magento mageacademy:argumentreplacement:showresult

We will see below result

7 – 3 = 4

So Hope you understand that how argument replacement works in Magento2 , how you can extend magento feature and override business logic.

If you like this blog please share your comment and share with your friends as well.

Leave a Reply
Your email address will not be published. *