3

I am trying to override layered navigation template in a Magento2 app. Here is my code:

Layout configuration:

<!-- app/code/MyVendor/MyModule/view/frontend/layout/default.xml -->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceBlock name="catalog.leftnav">
        <arguments>
            <argument name="template" xsi:type="string">MyVendor_MyModule::view.phtml</argument>
        </arguments>
    </referenceBlock>
    <referenceBlock name="catalogsearch.leftnav">
        <arguments>
            <argument name="template" xsi:type="string">MyVendor_MyModule::view.phtml</argument>
        </arguments>
    </referenceBlock>
</body>

Module configuration:

<!-- // app/code/MyVendor/MyModule/etc/module.xml -->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../vendor/magento/framework/Module/etc/module.xsd">
<module name="MyVendor_MyModule" setup_version="2.0.0">
    <sequence>
        <module name="Magento_Catalog"/>
        <module name="Magento_CatalogSearch"/>
        <module name="Magento_Swatches"/>
        <module name="Magento_ConfigurableProduct"/>
        <module name="Magento_Eav"/>
        <module name="Magento_Customer"/>
        <module name="Magento_Store"/>
        <module name="Magento_Backend"/>
        <module name="Magento_MediaStorage"/>
        <module name="Magento_Config"/>
        <module name="Magento_LayeredNavigation"/>
        <module name="Magento_Theme"/>
    </sequence>
</module>

Template file:

// app/code/MyVendor/MyModule/view/frontend/templates/view.phtml
Some template code here

I have cleared the cache (including "generation" folder) and recompiled dependencies, still Magento loads the template from LayeredNavigation module. How do I make it render my custom template instead?

UPDATE:
I was able to override it in my custom theme: app/design/frontend/MyVendor/MyTheme/Magento_LayeredNavigation/templates/layer/view.phtml

So now the question is, how to make a template in custom module override one set by a custom theme?

  • Did you try put your view.phtml template inside /layer folder? – Sushivam Jul 13 '16 at 7:33
  • I did not, but I believe that's not necessary as long as path to template is correctly specified. Anyways, @aaron-allen has answered my question below, it all works now. – Vasily802 Jul 13 '16 at 7:36
  • Yes , but im trying to do the same override for Catalogsearch, with my custom phtml file, i get blank page.. – Sushivam Jul 13 '16 at 7:41
  • Well, best bet would be to post your code in a new question. – Vasily802 Jul 13 '16 at 7:44
  • magento.stackexchange.com/questions/122625/… have updated my code here for the same scenario – Sushivam Jul 13 '16 at 7:52
3

Your xml tags should look like this:

<referenceBlock name="block.name">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">MyVendor_MyModule::view.phtml</argument>
            </action>
</referenceBlock>

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.