Goal: After reading this tutorial Magento 2 How to get all xml loaded tree , you can output any layout xml file of any page in magento 2.
Sometime you have a desire to print all xml loaded tree to check whether your custom xml handles are loaded or not ?
For this purpose, I have written a simple module to print all xml loaded tree in a xml file, so checking your custom module xml is more easily.
The general ideas of this extension :
- 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.
It sounds easily right ? Here is how we create it.
The module we will create like the below image:
- We create a new directory Thienphucvx/Dev.
- Create Thienphucvx/Dev/registration.php to declare with Magento 2 about our module directory.
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Thienphucvx_Dev', __DIR__ );
- Create Thienphucvx/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="Thienphucvx_Dev" setup_version="1.0.0" schema_version="1.0.0" release_version="1.0.1"> </module> </config>
- Create Thienphucvx/Dev/etc/fronted/event.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="Thienphucvx\Dev\Model\Layout" /> </event> </config>
- Create Thienphucvx/Dev/Model/Layout.php with the content as below
<?php /** * Copyright © 2015 Thienphucvx.com. All rights reserved. */ namespace Thienphucvx\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);*/ $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; } }
- Set up module
In your home website directory. enter CMD command line:
– php bin/magento module:enable Thienphucvx_Dev
– php bin/magento setup:upgrade
- 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 .
Happy coding ! If you have any questions, please let me know.
Abdelkebir ELHARFALI
January 19
It works for me, there is just a misspelling in Thienphucvx/Dev/etc/fronted/event.xml, it should be Thienphucvx/Dev/etc/frontend/events.xml
Good luck ^^