Magento – Can’t override a core block using the referenceBlock method

blockslayoutmagento2overridesxml

I was wondering is someone has any idea of why my code is not working. I'm trying to override the sales order view to show an extra item on the "Items Ordered list". For this I created a module that is replacing the blocks on the sales_order_view.xml.

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceBlock class="Magento\Sales\Block\Adminhtml\Order\View\Items" name="order_items" template="Company_Module::order/view/items.phtml">
        <block class="Magento\Sales\Block\Adminhtml\Order\View\Items\Renderer\DefaultRenderer" as="default" template="order/view/items/renderer/default.phtml"/>
        <block class="Magento\Sales\Block\Adminhtml\Items\Column\Qty" name="column_qty" template="items/column/qty.phtml" group="column"/>
        <block class="Magento\Sales\Block\Adminhtml\Items\Column\Name" name="column_name" template="items/column/name.phtml" group="column"/>
        <block class="Magento\Framework\View\Element\Text\ListText" name="order_item_extra_info"/>

    </referenceBlock>
</body>

So far, the order_items template is overridden as expected but I can't not find a way to override the default.phtml one. I have tried with referenceBlock but it doesn't seem to be working. How could I target a block that is not using a name but an alias instead?

Best Answer

With the <referenceBlock> tag I'm pretty sure you can either use the name or the alias.

So in your case, I'm pretty sure you can do the following:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceBlock name="order_items">
        <arguments>
            <argument name="template" xsi:type="string">Company_Module::order/view/items.phtml</argument>
        </arguments>
        <referenceBlock name="default">
            <arguments>
                <argument name="template" xsi:type="string">Company_Module::order/view/items/renderer/default.phtml</argument>
            </arguments>
        </referenceBlock>
    </referenceBlock>
</body>
Related Topic