MAGENTO 2 HOW TO GET ALL XML LOADED TREE

MAGENTO 2 HOW TO GET ALL XML LOADED TREE

In this post we will show you How to get all xml loaded tree in nmagento 2 , we can output any xml file layout of any page(for any file) in magento 2.

Sometime you have got a want to print all xml loaded tree to see whether or not your custom xml handles ar loaded or not ?

For this purpose, I actually have written a straightforward module to print all xml loaded tree in an exceedingly xml file, thus checking your custom module xml is a lot of simply.

General ideas for this extension 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(any page).

It sounds easy ans sily right. Here we go to create it.

The module we will create/add like the following image:

module we will create for XML LOADED TREE MAGENTO 2
module we will create for XML LOADED TREE MAGENTO 2

1) We need to create a new directory onlinecode/Dev.

2) Create “registration.php” in onlinecode/Dev/registration.php to declare with Magento 2 about our module directory.

<?php
	/*
    * "Onlinecode_Dev"  add module directory
	*/
    \Magento\Framework\Component\ComponentRegistrar::register(
        \Magento\Framework\Component\ComponentRegistrar::MODULE,
        'Onlinecode_Dev',
        __DIR__
    );

3) Create “module.xml” at onlinecode/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="Onlinecode_Dev" setup_version="1.0.0" schema_version="1.0.0" release_version="1.0.1">
    </module>
</config>    

4) Create “event.xml” at onlinecode/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="onlinecode\Dev\Model\Layout" />
    </event>
</config>

5) Create “Layout.php” at onlinecode/Dev/Model/Layout.php with the content as below

<?php
    /**
     * Create "Layout.php" at onlinecode/Dev/Model/Layout.php
    */
    namespace onlinecode\Dev\Model;
    use Magento\Framework\Event\Observer;
    use Magento\Framework\Event\ObserverInterface;
    class Layout  implements ObserverInterface
    {
        protected $set_logger;
        public function __construct ( \Psr\Log\LoggerInterface $logger_cons ) 
		{
            $this->set_logger = $logger_cons;
        }

        public function execute(\Magento\Framework\Event\Observer $observer)
        {
            $get_xml = $observer->getEvent()->getLayout()->getXmlString();
            /*
			*  $this->set_logger->debug($get_xml);
			*/
            $get_writer = new \Zend\Log\Writer\Stream(BP . '/var/log/layout_block.xml');
            $get_logger = new \Zend\Log\Logger();
            $get_logger->addWriter($get_writer);
            $get_logger->info($get_xml);
            return $this;
        }
    }

6) Set up module :: In your home website directory. enter CMD command line:

  • php bin/magento module:enable Onlinecode_Dev
  • php bin/magento setup:upgrade

Refresh the page that you simply need to visualize xml file (example for : your website home page) and check your handle xml file in var/log/layout_block.xml.

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  11  =  14

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US