Magento2 – Override Layered Navigation Template

catalogcatalogsearchlayered-navigationmagento2overrides

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?

Best Answer

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>