1

I created a module and now I want to override a .phtml file from another module. What is the best way to do that?

0

To overwrite a .phtml file from another module in a new module, you must first declare that the new module is dependent on the module you want to overwrite the .phtml 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="YourVendorname_YourModulename" setup_version="1.0.0">
        <sequence>
            <module name="SequenceVendorname_SequenceModulename"/>
        </sequence>
    </module>
</config>

After that, you have to use the layout where the .phtml the file is declared or the Block in which it is declared.

From Layout:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="sequence_block_name">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">YourVendorname_YourModulename::folder/file.phtml</argument>
            </action>
        </referenceBlock>
    </body>
</page>

or

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="sequence_block_name" remove="true"/>
        <referenceContainer name="sequence_container">
            <block class="SequenceVendorname\SequenceModulename\Block\BlockName" name="your_block_name" template="YourVendorname_YourModulename::folder/file.phtml" />
        </referenceContainer>
    </body>
</page>

From Block:

If the .phtml file is declared in a Block, then you must extend that Block in your module and declare the new path of the .phtml file.

1

You can override .phtml template using custom module by adding sequence into module.xml and then overwritting your layout with template.

Here is example 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_ComponentB" setup_version="1.0.0">
    <sequence>
        <module name="Vendor_ComponentA" />
    </sequence> 
  </module>
</config>

Follow this link for more detail instructions: http://devdocs.magento.com/guides/v2.0/extension-dev-guide/build/module-load-order.html

  • I'm new to Magento. Could you explain this more detailled? Thanks so far :-) – k33n Feb 22 '18 at 13:25
  • Have a look at my updated answer :) – Greg Feb 22 '18 at 13:31
  • I hope it helps! – Greg Feb 22 '18 at 13:33
  • Please accept the answer if it helped you solve the problem to keep the community tidy. – Greg Feb 22 '18 at 14:12

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.