Understand Magento type node and how to use for own benefits.

Hey folks,
Today we will going to learn fundamental of <type /> node which used in di.xml.

So let's start without time waste let's talk about <type /> node.

To understand this concept we will use an example for that we will create sample module.
So let's do this together. 

If you already know how to create a module you can skip this intro.

To create a module there is two files are required.

  1. registration.php
  2. module.xml
So let's create a registration.php

path : app/code/Extrembler/Base


\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Extrembler_Base',
    __DIR__
);


Second we need to create module.xml
path : app/code/Extrembler/Base/etc

<?xml version="1.0"?>
<!--
/**
* @category Extrembler
* @package  Extrembler_Base
* @author   Extrembler <gaurangpadhiyar1993@gmail.com>
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Extrembler_Base" setup_version="1.0.0"/>
</config>


Your module is now ready to go so let's enable this module.
To  enable module run below command from root.

bin/magento module:enable Extrembler_Base -c

second it will ask for setup upgrade. To perform this again run below command from root.

bin/magento setup:upgrade

Your module registered now. Now we are ready go ahead.


What does <type /> node actual do?

It is an argument replacer.


Do you want to check? Ohhh!!! Yes, then let's begin the roller coaster.

In the same module we will create block class for this.

path : app/code/Extrembler/Base/Block

namespace Extrembler\Base\Block;
class Example
{
    public function __construct(
      $coffeestring = "Coffee With Magento",
      $coffeearray = array("Coffee","With","foo"=>"Magento",array("abc"=>"You","are","the","test"=>"best")),
    )
    {
       echo $coffeestring;
       print_r($coffeearray);
    }
}

Next step is to call a class (in our case block). For calling a block, let's create layout default.xml and call our block from it.
So let's do it.

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="head.additional">
            <block class="Extrembler\Base\Block\Example" name="argument.replacer" />
        </referenceBlock>
    </body>
</page>

Now clear your cache to check the result.

bin/magento c:f

Now if you run frontend, block will print initial string and array value defined in __construct method.

Coffee With Magento
array(
0=>Coffee
1=>with
foo=>Magento
2=> array(
 "abc"=>"You"
 "0"=>"are"
 "1"=>"the"
 "test"=>"best" 
 )
)

Actual roller coaster ride start from here. So tighten your seat belt.

Next step is to create di.xml at path etc/.
First we will replace a string using <type /> node.

How to replace a string?

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
     <type name="Extrembler\Base\Block\Example">
         <arguments>
            <argument name="coffeestring" xsi:type="string">Let's go on drinks not coffee</argument>
        </arguments>
     </type>
</config>


After creating di.xml, clear your cache and check the result.

It will print now,

Let's go on drinks not coffee

Now break down the code and understand what it means.


<type name="Extrembler\Base\Block\Example">

First tag is <type name="' /> which tells magento which class arguments's we are going to deal with.

<arguments>
<!-- Code goes here -->
</arguments>


Second tag is <arguments />. This outer <arguments /> node tell magento system that we are now dealing with an arguments. There are other sub-nodes also for <type /> therefore need to define with which sub-node we are dealing with.

<argument name="coffeestring" xsi:type="string">
<!-- Code goes here -->
</argument>

Third is <argument />. This inner <argument /> node change the inject value of single __construct method argument.

<argument name="coffeestring" xsi:type="string">
<!-- Code goes here -->
</argument>

Next is name of argument : it is php variable name from __construct method. In our case it is, $coffeewithstring. So remove the dollar sign ($) and put reaming part here as name.

<argument name="coffeestring" xsi:type="string">
<!-- Code goes here -->
</argument>

next xsi:type which tells what sort of value we need to replace with existing value.
In our case it is string.


How to replace an array?

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
     <type name="Extrembler\Base\Block\Example">
         <arguments>
            <argument name="coffeestring" xsi:type="string">Let's go on drinks not coffee</argument>
            <argument name="coffeearray" xsi:type="array">
                  <item name="0" xsi:type="string">Tea</item>
                  <item name="1" xsi:type="string">without</item>
                  <item name="foo" xsi:type="string">Magento 2 demands</item>
            </argument>
        </arguments>
     </type>
</config>

Now, again clear cache and let's check result.


Let's go on drinks not coffee
0=>Tea
1=>without
foo=>Magento 2 demands


Again we will break code and understand how it deals.

First is <argument name="" /> which tells which variable from __construct method we are dealing with. In our case it is $coffeearray from __construct method.

Second is <argument xsi:type="" /> which tells what sort of value need to replace with existing values.

For array use <item /> node as sub-node.

If we see <item /> it has name and xsi:string. 
name = key from array.
xsi:type you already know what it means to use, correct?

You are thinking, it is an only simple array what about nested array. For nested array use <item  /> node as nested. Don't get it let's understand it by below code.



<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
     <type name="Extrembler\Base\Block\Example">
         <arguments>
            <argument name="coffeestring" xsi:type="string">Let's go on drinks not coffee</argument>
            <argument name="coffeearray" xsi:type="array">
                  <item name="0" xsi:type="string">Tea</item>
                  <item name="1" xsi:type="string">without</item>
                  <item name="foo" xsi:type="string">Magento 2 demands</item>
                  <item name="2" xsi:type="array">
                     <item name="abc" xsi:type="string">We</item>
                     <item name="0" xsi:type="string">had</item>
                     <item name="1" xsi:type="string">replaced</item>
                     <item name="test" xsi:type="string">nested array</item>
                  </item>
            </argument>
        </arguments>
     </type>
</config>
  

put xsi:type="array" at <item /> node and create <item /> sub-nodes under it as we did for main array.

I think that's enough for understanding <type /> node Argument Replacement behavior. 

Here is a full list of valid xsi:types


xsi:type="array"
xsi:type="string"
xsi:type="object"
xsi:type="boolean"
xsi:type="const"
xsi:type="number"
xsi:type="string"
xsi:type="init_parameter"
xsi:type="null"

If like efforts, Please share, comment and subscribe for future posts and inspire more.

Also please let us know what next you need to read and understand.

Comments

Popular posts from this blog

Hosting and Goodies

Magento 2 Import Scripts

How to add product attribute value in minicart magento 2