The question:
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 ?
The Solutions:
Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.
Method 1
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
MagentoFrameworkComponentComponentRegistrar::register(
MagentoFrameworkComponentComponentRegistrar::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="SampleDevModelLayout" />
</event>
</config>
4/ Create Sample/Dev/Model/Layout.php
with the content as below
<?php
namespace SampleDevModel;
use MagentoFrameworkEventObserver;
use MagentoFrameworkEventObserverInterface;
class Layout implements ObserverInterface
{
protected $_logger;
public function __construct ( PsrLogLoggerInterface $logger
) {
$this->_logger = $logger;
}
public function execute(MagentoFrameworkEventObserver $observer)
{
$xml = $observer->getEvent()->getLayout()->getXmlString();
/*$this->_logger->debug($xml);*//*If you use it, check ouput string xml in var/debug.log*/
$writer = new ZendLogWriterStream(BP . '/var/log/layout_block.xml');
$logger = new ZendLogLogger();
$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
.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0