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.
- registration.php
- module.xml
So let’s create a registration.php
path : app/code/Your/Module
1 2 3 4 5 6 |
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Your_Module', __DIR__ ); |
Second we need to create module.xml
path : app/code/Your/Module/etc
1 2 3 4 5 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Your_Module" 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 Your_Module -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 of __construct method.
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/Your/Module/Block
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php namespace Your\Module\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.
path : app/code/Your/Module/view/frontend/layout/default.xml
1 2 3 4 5 6 7 8 |
<?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="Your\Module\Block\Example" name="argument.replacer" /> </referenceBlock> </body> </page> |
Now clear your cache using bin/magento c:f to check the result.
Now if you run frontend, block will print initial string and array value defined in __construct method.
1 2 3 4 5 6 7 8 9 10 11 12 |
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?
1 2 3 4 5 6 7 8 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Your\Module\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,
1 |
Let's go on drinks not coffee |
Now break down the code and understand what it means.
1 |
<type name="Your\Module\Block\Example"> |
First tag is <type name=”‘ /> which tells magento which class arguments’s we are going to deal with.
1 2 3 |
<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.
1 2 3 |
<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.
1 2 3 |
<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.
1 2 3 |
<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?
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Your\Module\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.
1 2 3 4 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Your\Module\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
1 2 3 4 5 6 7 8 9 |
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.