0

We are going to use controller_action_layout_generate_blocks_before frontend event in M2. please note same event which is working on M1 but it is not working please review my below code and share your ideas

app/code/Demo/Stackexchange/etc/frontend/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_generate_blocks_before">
        <observer name="Demo_check_for_vision_user_session" instance="Demo\Stackexchange\Observer\CheckForStackexchangeUserSession" />
    </event>
</config>

app/code/Demo/Stackexchange/Observer/CheckForStackexchangeUserSession.php

<?php
namespace Demo\Stackexchange\Observer;
use Psr\Log\LoggerInterface;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Session\SessionManagerInterface;
/**
 * Class CheckForStackexchangeUserSession
 * @package Demo\Stackexchange\Observer
 */
class CheckForStackexchangeUserSession implements ObserverInterface
{
    /**
     * @var SessionManagerInterface
     */
    private $coreSession;

    /**
     * @var LoggerInterface
     */
    private $logger;  

    /**
     * constructor
     */
    public function __construct(
        LoggerInterface $logger,
        SessionManagerInterface $coreSession
    ) {
        $this->logger = $logger;
        $this->coreSession = $coreSession;
    }

    /**
     * Vision redirects a customer
     *
     * @param Observer $observer
     */
    public function execute(Observer $observer)
    {

       $writer = new \Zend\Log\Writer\Stream(BP.'/var/log/magento2.log');
       $logger = new \Zend\Log\Logger();
       $logger->addWriter($writer);
       $logger->info("Usage for");

    }


}
5
  • You can try with put events.xml to directly into etc folder
    – Pawan
    Commented Nov 5, 2019 at 16:39
  • what is exact version you are using ?
    – Pawan
    Commented Nov 5, 2019 at 16:44
  • what you want to achieve using this event? Commented Nov 5, 2019 at 16:46
  • AFAIK there no event controller_action_layout_generate_blocks_before in magento 2!
    – Pawan
    Commented Nov 5, 2019 at 16:51
  • I am using magento2.3.1 CE Commented Nov 5, 2019 at 17:37

2 Answers 2

2

The name of the event is "layout_generate_blocks_before".

You may refer the following file to check for the event.

vendor/magento/framework/View/Layout/Builder.php

There is this below function in the file that executes the observer.

protected function generateLayoutBlocks()
{
    $this->beforeGenerateBlock();

    Profiler::start('LAYOUT');
    /* dispatch event for adding xml layout elements */
    $this->eventManager->dispatch(
        'layout_generate_blocks_before',
        ['full_action_name' => $this->request->getFullActionName(), 'layout' => $this->layout]
    );
    Profiler::start('layout_generate_blocks');

    /* generate blocks from xml layout */
    $this->layout->generateElements();

    Profiler::stop('layout_generate_blocks');
    $this->eventManager->dispatch(
        'layout_generate_blocks_after',
        ['full_action_name' => $this->request->getFullActionName(), 'layout' => $this->layout]
    );
    Profiler::stop('LAYOUT');

    $this->afterGenerateBlock();

    return $this;
}

With above function, you can also get hints of arguments passed in observer that may be useful to you.

I hope it helps.

0

try this event controller_action_layout_render_before controller_action_layout_generate_blocks_before event doesn't exist

1
  • this is not triggering. Commented Nov 6, 2019 at 6:11

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.