Add layout handle based on dynamic value
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;
}
}
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.