1

I need to change search page layout 1-column while Search page is empty. any idea please share.

asked Nov 25, 2017 at 9:12

1 Answer 1

2

you can update the page layout for empty result page by observing controller_action_layout_render_before event in your custom module as below.

app/code/{Namespace}/{Module}/etc/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
 <event name="controller_action_layout_render_before">
 <observer name="layout_render_before" instance="{Namespace}\{Module}\Observer\LayoutRenderBefore" />
 </event>
</config>

app/code/{Namespace}/{Module}/Observer/LayoutRenderBefore.php

<?php
namespace {Namespace}\{Module}\Observer;
class LayoutRenderBefore implements \Magento\Framework\Event\ObserverInterface
{
 protected $_resultPageFactory;
 public function __construct(\Magento\Framework\View\Result\PageFactory $resultPageFactory) {
 $this->_resultPageFactory = $resultPageFactory;
 }
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
 $resultPage = $this->_resultPageFactory->create();
 $block = $resultPage->getLayout()->getBlock('search.result');
 if ($block instanceof \Magento\CatalogSearch\Block\Result && !$block->getResultCount()) {
 $pageConfig = $resultPage->getConfig();
 $pageConfig->setPageLayout('1column');
 }
 }
}
answered Nov 25, 2017 at 11:06
4
  • Thank for your help. Commented Nov 25, 2017 at 11:19
  • Happy to hear that it worked for you. Commented Nov 25, 2017 at 11:21
  • Using above codes selected attribute result showing two times in left panel. Any idea bout this. Commented Nov 27, 2017 at 5:36
  • Below line is affect for that : $resultPage = $this->_resultPageFactory->create(); Commented Nov 27, 2017 at 5:42

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.