Magento 2 PHP – Overriding Problem in Magento 2

layoutmagento2PHPsearchtemplate

I'm trying to extending Magento 2.x Search Box, But it's not overriding my module.

My code is:

Learning/CustomSearch/view/frontend/layout/default.xml

<?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>    
        <referenceContainer name="header-wrapper">
            <referenceBlock name="top.search">
                <arguments>
                    <argument name="template" xsi:type="string">Learning_CustomSearch::form.mini.phtml</argument>
                </arguments>
            </referenceBlock>
        </referenceContainer>
    </body>
</page>

Learning/CustomSearch/view/frontend/templates/form.min.phtml

<?php

/** @var $this \Magento\Framework\View\Element\Template */
/** @var $helper \Magento\Search\Helper\Data */
$helper = $this->helper('Magento\Search\Helper\Data');
?>
<div class="has-toggle search-sections">

  <div class="inner-toggle clearfix">
    <div class="block block-search">
        <div class="block block-title"><strong><?php echo __('Search'); ?></strong></div>
        <div class="block block-content">
            <form class="form minisearch" id="search_mini_form" action="<?php echo $helper->getResultUrl() ?>" method="get">
                <div class="field search">
                    <div class="control">
                        <input id="search"
                               data-mage-init='{"quickSearch":{
                                    "formSelector":"#search_mini_form",
                                    "url":"<?php echo $this->getUrl('search/ajax/suggest'); ?>",
                                    "destinationSelector":"#search_autocomplete"}
                               }'
                               type="text"
                               name="<?php echo $helper->getQueryParamName() ?>"
                               value="<?php echo $helper->getEscapedQueryText() ?>"
                               placeholder="<?php echo __('Search entire store here...'); ?>"
                               class="input-text"
                               autocomplete="off"/>
                        <div id="search_autocomplete" class="search-autocomplete"></div>

                        <?php echo $this->getChildHtml() ?>
                    </div>
                    <!-- .control -->

                    <button type="submit" value="Zoeken" class="btn btn-search"><?php echo __('Search'); ?></button>

                </div> <!-- .field .search -->
                <div class="actions"></div>
            </form>
        </div>
        <!-- .block-content -->
    </div>
    <!-- .block-search -->
  </div>
  <!-- .inner-toggle -->
</div>
<!-- .search-sections -->

Could you please tell me where I went wrong?

Best Answer

As far as I know, you don't need to nest the <referenceBlock name="top.search">...</reference> inside the <referenceContainer name="header-wrapper">...</reference>; you can move it directly under the <body> tag.

What's more, there are other references to top.search block in Magento layout update XML files, be sure your module depends on the modules that declare those updates, that are:

  • Magento_CatalogSearch
  • Magento_Checkout
  • Magento_Multishipping

This way your rules will be merged after those of above mentioned modules.

Your module.xml should like something like the following:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <!-- adjust setup_version according to your numbering -->
    <module name="Learning_CustomSearch" setup_version="1.0.0">
        <sequence>
            <module name="Magento_CatalogSearch"/>
            <module name="Magento_Checkout"/>
            <module name="Magento_Multishipping"/>
        </sequence>
    </module>
</config>

Hope it helps.

EDIT

Provided you have the form.mini.phtml under the view/frontend/templates folder of your module, the layout should look something like:

<?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="top.search">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">Learning_CustomSearch::form.mini.phtml</argument>
            </action>
        </referenceBlock>
    </body>
</page>
Related Topic