2

I want to override the register.phtml in Magento_Customer module. But the setTemplate method fails to override the original register.phtml because the register.phtml was overridden in Magento_CustomerCustomAttributes

<referenceBlock name="customer_form_register">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">VendorName_ModuleName::form/register.phtml</argument>
            </action>
        </referenceBlock>

In this case, how can I replace the template, or how can I insert a set of radio button into that template? since my intention is to add radio button to the template. Thanks in advance.

3

The action node is deprecated in magento 2. You should change your xml to this

<referenceBlock name="customer_form_register">
    <arguments>
        <argument name="template" xsi:type="string">VendorName_ModuleName::form/register.phtml</argument>
    </arguments>
</referenceBlock>

If you want to override a template that a magento 2 core module is already overriding, you need to override that override. To do this you should add the module that is already doing the override as a sequence in your module.xml file

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_module" setup_version="100.0.0">
        <sequence>
            <module name="Magento_CustomerCustomAttributes" />
        </sequence>
    </module>
</config>

No flush your cache and reload your page and you should see your template getting loaded.

UPDATE: The devdocs state that "The <action> instruction is deprecated." but in all cases I have encountered, the instruction is the only method that can successfully override a template. This github issue says this is currently a bug.

  • 1
    thanks for your answer, but it does not work. – Ricky.C Jan 26 '16 at 6:03
  • 1
    Ricky / for others' reference: Nathan is incorrect about <action method="..."> being deprecated. The layout XML given in the question should be exactly correct. He is correct about the sequence tag, though; that should ensure that your XML gets parsed after the other module(s) you reference. – Ryan Hoerr Mar 23 '16 at 19:39
  • 1
    @RyanH. I was going off of the M2 Dev Doc's (goo.gl/laVP35) which states "The <action> instruction is deprecated." Unfortunately there is a bug with the <arguments> tag (goo.gl/diV0Fx) preventing my code above from working properly. For now it is acceptable to use action as it is the only method that works for overriding a template in layout xml at the moment. I will update my original post to make note of this fact. – Nathan Toombs Mar 23 '16 at 19:51
0

Step 1) Enable the extension to specify config.php file from app/etc folder

    <?php
    return array (
      'modules' => 
      array (
            'Learning_RewriteSales' => 1,
      ),
    );

Step 2) Create Custom.php Class in Block

    <?php
    namespace Learning\RewriteSales\Block\Adminhtml\Order\View;

    class Custom extends \Magento\Backend\Block\Template
    {

    } 

Step 3) Create Info.php file to extend core Info.php

    <?php
    namespace Learning\RewriteSales\Block\Adminhtml\Order\View;

    class Info extends \Magento\Sales\Block\Adminhtml\Order\View\Info
    {

    }

Step 4) Create di.xml file to specify which class you where extended ( Dependency Injection )

    <?xml version="1.0"?>
       <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
<preference for="Magento\Sales\Block\Adminhtml\Order\View\Info" type="Learning\RewriteSales\Block\Adminhtml\Order\View\Info"/>
      </config>

Step 5) Create a module.XML file to specify the setup version

      <?xml version="1.0"?>
          <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
               <module name="Learning_RewriteSales" setup_version="2.0.0">
                      <sequence>
               <module name="Magento_Sales"/>
                      </sequence>
               </module>
          </config>

Step 6) Create sales_order_view.xml file from Learning/RewriteSales/view/layout folder and write the below code

          <?xml version="1.0"?>
                <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-2columns-left" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
                     <body>
                         <referenceBlock name="order_info">
                               <action method="setTemplate">
                                   <argument name="template" translate="true" xsi:type="string">order/view/info.phtml</argument>
                               </action>
                        </referenceBlock>
                        <referenceBlock name="order_info">
                              <block class="Learning\RewriteSales\Block\Adminhtml\Order\View\Custom" name="sales_order_view_custom" template="order/view/custom.phtml" />
                        </referenceBlock>
                     </body>
                  </page>

Step 7) Create the Custom.phtml and Info.phtml file from Learning/RewriteSales/view/layout and insert below code.

Custom.phtml

        <h1>Hi, I am here!</h1>

Info.phtml

First Copy the code from Magento\Sales\view\adminhtml\templates\order\view\info.phtml file and past here after that add one more line

        <?php echo $block->getChildHtml('sales_order_view_custom');?>

Step 8) Finally clean the cache and to see your custom code Sales Order View Page. reference: How to extend backend template files in Magento 2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.