Magento – Override template phtml file in magento 2

magento2overridesphtmltemplatexml

First
I have created a module and place it in a file I want to override

\app\code\Vendor\Module\view\adminhtml\templates\widget\grid\extended.phtml

The original location is

vendor\magento\module-backend\view\adminhtml\templates\widget\grid\extended.phtml

Second
I added backend_widget_grid.xml located in

\app\code\BodyMod\OrdersPopupButton\view\adminhtml\layout\backend_widget_grid.xml

And the content of the xml file is:

<?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="widget.extended">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">Vendor_Module::widget/grid/extended.phtml</argument>
            </action>
        </referenceBlock>
    </body>
</page>

I don't see my edits so can somebody tell me where is my mistake?

Best Answer

Override Magento\Backend\Block\Widget\Grid\Extended like this:

<?php

namespace Vendor\Module\Block\Adminhtml\Widget\Grid;

class Extended extends \Magento\Backend\Block\Widget\Grid\Extended
{
    protected $_template = 'Vendor_Module::widget/grid/extended.phtml';
}

and in di.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework/ObjectManager/etc/config.xsd">
    <preference for="Magento\Backend\Block\Widget\Grid\Extended" type="Vendor\Module\Block\Adminhtml\Widget\Grid\Extended" />
</config>