Debugging modx snippet

Hi, I am trying to debug modx snippets using XDebug in Visual studio Code. VSCode is configured via tutorials and “Listen for Xdebug” works for example for index.php.
But when I insert php code in external file and set snippet as static for this file. Debuging is not working.

Another thing is if I insert xdebug.start_with_request=yes to php.ini (xampp) modx page will not load. Not sure if this config is necessary or not. Please help.

you can set a breakpoint in modscript.class.php with a condition like

$this->_scriptName == 'elements_modsnippet_160'

where 160 is the id of your snippet.
Then step into

$includeResult= include $this->_scriptFilename;

Ok, thanks, but I dont understand at all…
Is this related to vscode?

yes, that’s how its working for me within vscode

Can you be more specific? Where can I find the file modscript.class.php?
where should I put the code $includeResults?

I usually debug snippets by setting a breakpoint in the cached snippet file in core/cache/includes/elements/modx/revolution/modsnippet/.

I use this as well, but I never had problems with MODX not loading.

Great, this works, thanks…

since the cache file is not there at the beginning and after the cache was cleared, I’m setting the breakpoint within modScript
it is that line, where I set the breakpoint

you can add the condition to the breakpoint by rightclicking to the breakpoint in vscode

for MODX 3 it would be

1 Like

I’ve debugged through index.php, but it’s usually pretty tedious to click through all the steps to get to the offending code.

I often debug by instantiating MODX in an external script, and then including, and instantiating instances of classes and calling their methods, or in situations like yours, calling $modx->runSnippet() with an appropriate set of properties.

In both cases, you can step through the problem code and place breakpoints wherever you need them.

You can include the real snippet code in the snippet content with

<?php
return include("/path/to/your/snippet/file.php");

That way you avoid to locate the cached snippet code and you can set the breakpoints directly.

@jako Sounds like a great idea, but how do you get the $scriptProperties array to the snippet?

It behaves the same as if the snippet code is inside the content.

@jako Do you mean that you edit the default properties of the snippet? I don’t usually use them – I put them in the snippet tag.

I don’t understand your question with the $scriptProperties.

The code above will execute the real snippet code with an include of a file. In the cache file, you have only this include code. That way you can set break points in the real snippet code.

Most of my snippets require properties to operate. Unless they’re set in the snippet’s default properties, I don’t see how the snippet would receive them. I hope that makes sense.

The suggested code does not change how the snippet works (and it receives the snippet default properties as before). It just adds another include inside to make remote debugging easier.

I achieve this with the GPM 1 system setting gitpackagemanagement.enable_debug on my development machine (System settings - Git Package Management). The installed packages don’t have this inside.

I get it. I would run this in PhpStorm where xDebug is already enabled. It will do remote debugging, but I usually debug on a local install.

I often need to adjust the code and the properties between debug runs.
What I normally do is have the snippet code in a file, set up the $ScriptProperties array then “include” the file. This will often find problems in the snippet that have nothing to do with the rest of MODX.

If I need to see the snippet interacting with the install, I instantiate MODX (the version in the install), set up the $scriptProperties array, and call $modx->runSnippet();

The down side of your method for me would be having to go to the Manager and manually adjust the snippet’s default properties, though it sounds like a great method when you need to do remote debugging.

You can modify the $scriptProperties array with a second include that runs before including the snippet code.

For me remote debugging is using xDebug locally or with an SSH tunnel to a different host as there is almost no difference. Sorry for the wrong use of the term.

I hadn’t thought of that. It’s a great idea.

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.