6

I need to add a specific layout handle for all the pages when a certain value is present in the session.
To make it more clear, if $session->getMyValue() returns true I need to load a specific layout handle called my_specific_layout_handle on every page I visit.
In the code above $session is an instance of any subclass of Magento\Framework\Session\SessionManager (not really relevant).
Is there any event I can use or any method I can pluginize?

Note: I'm not concerned with full page cache for now. Let's say it does not exist.

7

For Adding Layout Handles You Can Use Event Also:

In Your events.xml

<event name="layout_load_before">
   <observer name="load_custom_handler" instance="[Vendor]\[Module]\Observer\LayoutLoadBefore" />
</event>

In Your LayoutLoadBefore.php

<?php

namespace [Vendor]\[Module]\Observer;

use [Vendor]\[Module]\Model\Session; //this is a custom session that extends `\Magento\Framework\Session\SessionManager`
use Magento\Framework\View\Result\Layout;

class LayoutLoadBefore implements \Magento\Framework\Event\ObserverInterface
{
    const MY_SPECIFIC_LAYOUT_HANDLE = 'my_specific_layout_handle';
    /**
     * @var Session
     */
    private $session;

    /**
     * LayoutPlugin constructor.
     * @param Session $session
     */

    public function __construct(
        Session $session
    )
    {
        $this->session = $session;        
    }

    /**
     * @param \Magento\Framework\Event\Observer $observer
     * @return $this
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
       if ($this->session->getMySpecificSessionValue()) { // YourCondition
           $layout = $observer->getLayout();
           $layout->getUpdate()->addHandle(self::MY_SPECIFIC_LAYOUT_HANDLE);
       }

       return $this;
    }
}
| improve this answer | |
  • 2
    Thanks. It works. This looks more elegant than my solution because I'm sure that my layout handle is loaded last. – Marius Dec 5 '17 at 8:42
  • 1
    @Marius, You are always welcome :), and thanks to you for your inputs in community – Rizwan Khan Dec 5 '17 at 9:41
  • Hello, but it would be great if you create a custom layout file. layout/category_custom_layout.xml and add this line in body tag <attribute name="class" value="category-custom-layout" /> – Pratik Mehta Oct 18 '19 at 7:03
3

I was able to do it by pluginizing Magento\Framework\View\Result\Layout::addDefaultHandle.

I added this in di.xml of my module

<type name="Magento\Framework\View\Result\Layout">
    <plugin name="my-plugin-name" type="[Vendor]\[Module]\Plugin\Result\LayoutPlugin" />
</type>

Then created the file. [Vendor]/[Module]/Plugin/Result/LayoutPlugin.php

<?php
namespace [Vendor]\[Module]\Plugin\Result;

use [Vendor]\[Module]\Model\Session; //this is a custom session that extends `\Magento\Framework\Session\SessionManager`
use Magento\Framework\View\Result\Layout;

class LayoutPlugin
{
    const MY_SPECIFIC_LAYOUT_HANDLE = 'my_specific_layout_handle';
    /**
     * @var Session
     */
    private $session;

    /**
     * LayoutPlugin constructor.
     * @param Session $session
     */
    public function __construct(
        Session $session
    ) {
        $this->session = $session;
    }

    /**
     * @param Layout $layout
     * @param $response
     * @return bool
     */
    public function afterAddDefaultHandle(Layout $layout, $response)
    {
        if ($this->session->getMySpecificSessionValue()) {
            $layout->addHandle(self::MY_SPECIFIC_LAYOUT_HANDLE);
        }
        return $response;
    }
}

This seems to work, but I am open for other suggestions.

| improve this answer | |
  • You can use event also , if you need then I can give you – Murtuza Zabuawala Dec 5 '17 at 7:31
  • 1
    sure. It wouldn't hurt to try – Marius Dec 5 '17 at 7:38
  • @Marius, I added answer using event as Murtuza Suggested – Rizwan Khan Dec 5 '17 at 7:41

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.