18

According to magento 2 theme specification about Layout files processing :

The Magento application processes layout files in the following order:

1/ Collects all layout files from modules. The order is determined by the modules order in the module list from app/etc/config.php.

2./ Determines the sequence of inherited themes [, ..., ]

3./ Iterates the sequence of themes from last ancestor to current:

a. Adds all extending theme layout files to the list.

b. Replaces overridden layout files in the list.

4./ Merges all layout files from the list.

How can I debug and view the layout file for every step of this process ?

6
  • Do you mean where to put break points? Or what would you expect?
    – BuskaMuza
    Commented Jan 19, 2016 at 20:48
  • Hi, Thank you. My main purpose is printing "merges all layout file" to see how magento 2 layout render. I have added my answer as the below. Commented Jan 20, 2016 at 2:30
  • @ thienphucvx, I used your code but I am unable to create .xml at var/log . Do I need to change anything ?
    – learner
    Commented Mar 24, 2016 at 19:17
  • Could you try to debug to make sure that you created that module correctly like "var_dump something on excute method of observer" to check whether it's working or not ? Commented Mar 24, 2016 at 23:40
  • 1
    Have a look to this developer app, it may help you in the debug process: github.com/magespecialist/mage-chrome-toolbar Commented Aug 22, 2016 at 7:43

1 Answer 1

21

Finally, I can print all loaded xml layout and know how magento 2 layout is working. It also help us to check whether our custom module layout is loaded or not. The idea is :

  • We listen to an event “layout_generate_blocks_after” and get all loaded tree from that point.

  • Save all loaded tree to an xml file.

1/ We create a new directory Sample/Dev. Create Sample/Dev/registration.php to declare with Magento 2 about our module directory.

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Sample_Dev',
    __DIR__
);

2/ Create Sample/Dev/etc/module.xml : To let Magento 2 know about setup version of our module

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="module.xsd">
    <module name="Sample_Dev" setup_version="1.0.0" schema_version="1.0.0" release_version="1.0.1">
    </module>
</config>

3/ Create Sample/Dev/etc/frontend/events.xml. In this file, we will listen to event “layout_generate_blocks_after”

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">

    <event name="layout_generate_blocks_after">
        <observer name="thienphucvx_layout_generate_blocks_after" instance="Sample\Dev\Model\Layout" />
    </event>

</config>

4/ Create Sample/Dev/Model/Layout.php with the content as below

<?php

namespace Sample\Dev\Model;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
class Layout  implements ObserverInterface
{
    protected $_logger;
    public function __construct ( \Psr\Log\LoggerInterface $logger
    ) {
        $this->_logger = $logger;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $xml = $observer->getEvent()->getLayout()->getXmlString();
        /*$this->_logger->debug($xml);*//*If you use it, check ouput string xml in var/debug.log*/
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/layout_block.xml');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info($xml);
        return $this;
    }
}

5/ Set up new module . In your home website directory. enter CMD command line:

– php bin/magento module:enable Sample_Dev

– php bin/magento setup:upgrade

6/ Refresh the page that you want to see xml file (for example: your home page) and check your handle xml file in var/log/layout_block.xml .

5
  • I just came across this and it is amazing. Would you mind submitting a PR to Magento 2 so everyone can have this feature? Even releasing a module would help tremendously.
    – mbalparda
    Commented Aug 7, 2017 at 17:52
  • Hi Mbalparda, I don't know how to do it. Could you help me to submit it ? Commented Aug 14, 2017 at 2:00
  • Yep pinged you on Twitter.
    – mbalparda
    Commented Aug 14, 2017 at 14:40
  • 1
    where is the issue/pull-request for upvoting ? link ? Commented Nov 7, 2018 at 14:49
  • github.com/magento/magento2/issues/10533
    – jtompl
    Commented May 3, 2020 at 11:23

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.