3

I am accessing observer (controller_action_layout_render_before) through this:

app/code/Vendor/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="Vendor\Module\Observer\LayoutRender" />
    </event>
</config>

My observer file

app/code/Vendor/Module/Observer/LayoutRender.php

<?php
namespace Vendor\Module\Observer;

class LayoutRender 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();

        if (//PHP Check here) {
            $pageConfig = $resultPage->getConfig();
             $pageConfig->setPageLayout('1column');
        }
    }
}

My question is, how can I check if this is accessing on search page?

  • No answer... :( – Asim Ali Mar 21 at 16:23
1

Add a check in your execute function:

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) {
        $pageConfig = $resultPage->getConfig();
        $pageConfig->setPageLayout('1column');
    }
}

This will check if you are on search page

  • Thanks Shoaib Munir for the short and to the point answer – Asim Ali Mar 22 at 14:47
2

You can check the module, controller & action name in observer file in below way.

Pass the below class in observer constructor

\Magento\Framework\App\RequestInterface $request

Get module name, controller name and action name in below way.

$moduleName     = $this->request->getModuleName(); 
$controllerName = $this->request->getControllerName(); 
$actionName     = $this->request->getActionName();

Now you can perform your if condition

if ( $mmodulename =='catalogsearch' && $controllername = 'advanced' && $actionName == 'index') {
    // Your code part //
}

Below is the full code.

namespace Devi\Customcheck\Observer;

class LayoutRender implements \Magento\Framework\Event\ObserverInterface
{
    protected $_resultPageFactory;

    public function __construct(
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        \Psr\Log\LoggerInterface $logger,
        \Magento\Framework\App\RequestInterface $request
    ){

        $this->_resultPageFactory = $resultPageFactory;
        $this->_logger = $logger;
        $this->request = $request;
    }


    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $resultPage = $this->_resultPageFactory->create();

        // Get the module, controller and action name  //
        $moduleName     = $this->request->getModuleName(); 
        $controllerName = $this->request->getControllerName(); 
        $actionName     = $this->request->getActionName();

        if ( $moduleName =='catalogsearch' && $controllerName = 'advanced' && $actionName == 'index') {
            $pageConfig = $resultPage->getConfig();
            $pageConfig->setPageLayout('1column');
        }

        // $this->_logger->debug('My Observer Check =>'.$routeName.' / '.$moduleName .' /'.$controllerName .' / '.$actionName); 
    }
}

Hope, you may get some idea now.

  • You are right, this can be done by this process. I can mark both answer, otherwise I will :) voted for your answer. Thanks – Asim Ali Mar 22 at 14:48
  • Nice detailed answer. +1 – Shoaib Munir Mar 23 at 16:44

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.