Categories


Archives


Recent Posts


Categories


Magento 2: Layout Arguments vs Action Methods

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!

Although the technique still works, various members of Magento’s engineering team have discouraged using Magento 1’s <action method="..."/> syntax to call methods on blocks from layout update XML files.

<block ...>
    <action method="addTab">
        <argument name="name" xsi:type="string">info_section</argument>
        <argument name="block" xsi:type="string">integration_edit_tab_info</argument>
    </action>    
</block>

Other members of the engineering team have suggested an <arguments/> node as a replacement.

<referenceBlock name="block_name">
    <arguments>
        <argument name="field1" xsi:type="string">foo</argument>
        <argument name="field_two" xsi:type="string">bar</argument>
    </arguments>
</referenceBlock>

Whenever you see an <argument/> or <arguments/> node in a bit of Magento 2 XML, it’s usually to configure values that the object manager (or similar system) will pass into an object when it’s instantiated. How this works is dependent on what sort of XML file the nodes are in.

In layout update XML files, Magento will parse the values in these arguments and pass them into the block object’s constructor as the $data parameter. This means you’ll be able to access the values via the block’s magic getter methods or via the getData method

$this->getField1();
$this->getFieldTwo();

$this->getData('field1');
$this->getData('field_two');

While not a full replacement for the <action method="..."/> nodes of Magento 1, these block data arguments will allow you to develop block features that are dependent on certain data being set.

Copyright © Alan Storm 1975 – 2019 All Rights Reserved

Originally Posted: 9th August 2016