Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debug layout file processing by print final merge layout xml ? #10533

Closed
thienphucvx opened this issue Aug 14, 2017 · 21 comments
Closed

Debug layout file processing by print final merge layout xml ? #10533

thienphucvx opened this issue Aug 14, 2017 · 21 comments
Assignees
Labels
Issue: Format is not valid Gate 1 Failed. Automatic verification of issue format is failed Progress: needs update

Comments

@thienphucvx
Copy link

thienphucvx commented Aug 14, 2017

At some point, you want to print all loaded xml files to check whether your custom layout is working or not ?
In order to do that we can write a custom module like this to solve that problem:

  • 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”

`

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

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 and check your handle xml file in var/log/layout_block.xml .

I just wonder if we can have it as a default module in magento2 => So we can turn on/off to debug easily.

Reference:
https://magento.stackexchange.com/questions/97343/how-can-i-debug-layout-file-processing-in-magento-2/98078?noredirect=1#comment261437_98078

@orlangur
Copy link
Contributor

At some point, you want to print all loaded xml files

Or just use xdebug. It allows to check state of any variable in action and not just try to guess what needs to be echoed/printed to file.

@thienphucvx
Copy link
Author

Hi Orlangur,
Actually, there are many ways to do it. My suggested solution is just one of the way to achieve it.

@orlangur
Copy link
Contributor

You don't need this, just learn to debug properly, without need for any hardcoded logging.

@convenient
Copy link
Contributor

I have had to use logging debugging like this before, and debugging like this does have its merit. For a lot of scenarios xdebug and object inspection is the usual debugging tool, but for others dumping to a log file has definitely saved me.

Saying that, I'd still probably roll this logging out bespoke as and when it's needed, as next time when you need to debug different merged xml your layout xml logger won't be useful. I'd never have thought of adding this kind of tool to the core.

@thienphucvx
Copy link
Author

Hi,
As feedbacks from you guys. Looks like it's not needed to add to the core. So think I will close it.

@miguelbalparda
Copy link
Contributor

Whoa I thought this would be a really good addition as a bin/magento command.

@thienphucvx
Copy link
Author

Hi Miguelbalparda,
Maybe we can copy and paste example code => it doestnot take much time.

@convenient
Copy link
Contributor

convenient commented Aug 15, 2017

@miguelbalparda agree that it would be a good addition to the bin/magento command, it'd be similar to the magerun config:dump command.

You could have a have a lot of fun with the config:dump command when you start piping the result into xpath

eg
magerun config:dump | xml sel -t -c "/config/global/full_page_cache"

@thienphucvx thienphucvx reopened this Aug 15, 2017
@thienphucvx
Copy link
Author

Now, I reopen it again... Let see the response from Magento

@miguelbalparda
Copy link
Contributor

miguelbalparda commented Aug 15, 2017

You and I know MageRun @convenient but new devs coming here only know about bin/magento when they start, so adding this to the core might be wise no matter if it is already in MageRun. I used a similar approach in Magento 1 more times than I'd like to admit.

@convenient
Copy link
Contributor

Good point @miguelbalparda :)

@convenient
Copy link
Contributor

@thienphucvx if you want to have a go at implementing the bin/magento command yourself you can look at a PR I put together a few months back to port over another magerun1 command into the Magento2 core.

#7982

@vherasymenko
Copy link

@thienphucvx Thank you for your report.
Please use the issue reporting guidelines to report an issue. Include the steps required to reproduce, the actual result, and the expected result.

@magento-engcom-team
Copy link
Contributor

@thienphucvx we are closing this issue due to inactivity. If you'd like to update it, please reopen the issue.

@torreytsui
Copy link
Contributor

Although this issue is closed already, I came across it and know that there is a workaround, so want to share it here.

Layout is cached by Magento. We can debug the layout by inspecting the layout cache.

To use this workaround:

  • Make sure you're using filesystem cache (so, disable caches like redis cache)
  • Load the page you want to debug with layout cache enabled
  • In var/cache/, search for the dom or any keyword of a layout file that you are debugging

A hit or more will show you the layout cache of the current page being debugging.

screen shot 2018-06-26 at 12 13 36

@LiamTrioTech
Copy link

This would be very useful

@quickshiftin
Copy link
Contributor

The fact that there is no easy way to view the layout on any given page makes debugging the cryptic layout system even more tedious. The spirit of this feature should be understood by the core team, how about a simple way to dump the layout XML for any given page. After all we already have template path hints...

@LiamTrioTech
Copy link

LiamTrioTech commented Sep 14, 2020

Those path hints are not great.
It should be on data attributes or html comments for each area and possibly chrome extension on the inspector they only seem to show phtml file names, should show block name & container as well.

@quickshiftin
Copy link
Contributor

Yup, template path hints are borderline useless. Just pointing out to the core team that there is a built-in debugging tool. Furthermore suggesting a similar simple tool (checkbox in the admin / flag we can set on the cli) to enable dump of layout xml on the frontend would be vastly more useful.

@AntonLee
Copy link

AntonLee commented Oct 5, 2020

There are third party modules attempting to solve the issue. I've found two:

@fd-sturniolo
Copy link

To add to this, Ho_Templatehints is another module that lets you inspect layout via DevTools in a somewhat unobtrusive way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue: Format is not valid Gate 1 Failed. Automatic verification of issue format is failed Progress: needs update
Projects
None yet
Development

No branches or pull requests