0

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?

  • remove Learning/CustomSearch/templates/form.mini.phtml from xml and place Vender_module::form.mini.phtml in your xml. – Anand Ontigeri Mar 28 '16 at 6:49
  • @AnandOntigeri, I have updated my layout as your above suggestion like Learning_CustomSearch::form.min.phtml, but still no luck. could you please post the layout xml code here. see my thread updated. – Bojjaiah Mar 28 '16 at 7:12
2

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>
| improve this answer | |
  • I have updated my layout as your above suggestion like Learning_CustomSearch::form.min.phtml, but still no luck. could you please post the layout xml code here. see my thread updated. – Bojjaiah Mar 28 '16 at 7:06
  • please, see the EDIT section of my answer – Alessandro Ronchi Mar 28 '16 at 7:29
  • Hi Alessandro, i need help for the following similar post, magento.stackexchange.com/questions/122625/… – Sushivam Jul 4 '16 at 10:53

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.