Octopus
Octopus

Reputation: 165

Magento2: Overwrite custom module template

I have two custom modules. Say for example,

Module_A
Module_B

I have created module.xml of Module B with sequence of Module A. Like this,

<module name="Module_B" setup_version="0.0.1">
    <sequence>
        <module name="Module_A"/>
    </sequence>
</module>

I want to overwrite Module_A's template file in Module_B and I am using luma theme.

I have tried to overwrite it in app/design/frontend/Magento/luma/Module_A/templates/(phtml file). But it doesn't seem to work.

Upvotes: 0

Views: 1676

Answers (1)

Hamo
Hamo

Reputation: 177

To override a module phtml file I would take one of the two following approaches:

-

Override template with XML Layout

Override the phtml file with a xml layout file in Module_B to tell Magento to load the phtml file for the block from Module_B instead of Module_A. This is very effective way even for major changes in layout blocks but you will have to keep track of the blocks names for any changes in future updates.

Please follow this answer to know how to change the phtml file for specific block with xml layout override.

-

Override template with theme files

Create a child theme of Luma (or other themes) and start include the phtml file for module Module_A like this:

app/design/frontend/customtheme/Module_A/{layout,templates,web}/(phtml file)

Personally, I would prefer this approach in case you want to do some quick changes and fixes to Module_A rendered blocks.

You can take Luma as a reference for you because it basically does the same.

Upvotes: 1

Related Questions