Categories


Archives


Recent Posts


Categories


Magento 2’s Poorly Named Layout Argument Nodes

astorm

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.

Updated for Magento 2! 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">
        <arguments>
            <argument name="label" xsi:type="string" translate="true">
                Track your order
            </argument>
        </arguments>
    </block>
</referenceBlock>

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

$block->getLabel();
$block->getData('label');

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 © Alana Storm 1975 – 2023 All Rights Reserved

Originally Posted: 24th December 2017

email hidden; JavaScript is required