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.

  • registration.php
  • module.xml

So let’s create a registration.php

path : app/code/Your/Module

Second we need to create module.xml

path : app/code/Your/Module/etc

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

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

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.

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?

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

It will print now,

Now break down the code and understand what it means.

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

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.

Third is <argument />. This inner <argument /> node change the inject value of single __construct method 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.

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?

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

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.

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

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.

Leave a Reply