Magento 2 getting those layout handles

Magento 1

In Magento 1 fetching the layout handles for the current request was the simple task of calling the following:

print_r(Mage::app()->getLayout()->getUpdate()->getHandles());

A quick place to add this is to the end of index.php, just remember to remove it once you’ve found the handle you’re after.

Magento 2

In Magento 2 it’s a little more complicated as the Mage god class has gone. What you want to gain access to is the ProcessorInterface which has the getHandles() method.

Magento\Framework\View\Layout\ProcessorInterface

The default preference for the above interface is Magento\Framework\View\Model\Layout\Merge

 <preference for="Magento\Framework\View\Layout\ProcessorInterface" 
type="Magento\Framework\View\Model\Layout\Merge" />

So, with the help of dependency injection, you could add this to the constructor (assuming it’s called $layout) and for instance call the following:

print_r($this->layout->getHandles());

Or send it to a log file, set a breakpoint etc.

I need to emphasise, this is temporary edit in order to find the handle you’re after. The changes should then be reverted.

A method I’ve used on CMS pages, and it depends on the kind of response being sent back, but if you’re response class also provides access to getLayout() then you can do the following or log it to a file, analyse it with a breakpoint etc.

die(print_r($resultPage->getLayout()->getUpdate()->getHandles()));
Array
(
    [0] => default
    [1] => cms_page_view
    [2] => cms_page_view_id_about-us
)

0 comment

    No comments found
Leave a comment