Recent Posts


Magento 2’s Poorly Named Layout Argument Nodes


Frustrated by Magento? Then you’ll love Commerce Bug, the must have debugging extension for anyone using Magento. Whether you’re just starting out or you’re a seasoned pro, Commerce Bug will save you and your team hours everyday. Grab a copy and start working with Magento instead of against it.

No Frills Magento Layout is the only Magento front end book you'll ever need. Get your copy today!

Magento 2 introduced a new node type to its Layout XML DSL — the <arguments/> and <argument/> node.

#File: vendor/magento/module-shipping/view/frontend/layout/sales_guest_view.xml
<referenceBlock name="sales.order.view">
    <block class="Magento\Shipping\Block\Tracking\Link" name="tracking-info-link" template="Magento_Shipping::tracking/link.phtml">
            <argument name="label" xsi:type="string" translate="true">
                Track your order

Like so much of Magento — the names of these new nodes hide their purpose from developers. All the <argument/> nodes do ensure a value will be set on the final block object. i.e. The above code will set a label property on the tracking-info-link block (instantiated from the PHP class Magento\Shipping\Block\Tracking\Link). This means you can access the property from your phtml template like this


You can see this in the Magento_Shipping::tracking/link.phtml template file.

#File: vendor/magento/module-shipping/view/frontend/templates/tracking/link.phtml
<span><?= /* @escapeNotVerified */ $block->getLabel() ?></span>   

Behind the scenes, this functionality is implemented by Magento looking for these argument nodes, and then passing additional information into the Block class when instantiating a Block object. That’s why these nodes are called <arguments/> and <argument/> — because the system designer was focused on the implementation details of the system instead of how end user programmers would use the system.

Copyright © Alan Storm 1975 – 2018 All Rights Reserved

Originally Posted: 24th December 2017