Debuggin plugin development

Hi all

I’m trying to create a plugin for jqmath. It is based on the tutorial (creating a random plugin and also in the simplecart plugin).

This plugin will just add additional assets once loaded to the template.

To check variables I enabled the debugger. I get the debugger bar displayed but can’t seem to print any messages or variables.

$grav['debugger']->addMessage("Debugger enabled");

As a test I then changed it to the following, this did not work - and I believe should not because $my_var is not defined. But this did not raise an exception either or produced any other errors.

$grav['debugger']->addMessage($my_var)

Event used: onAssetsInitialized

Environment:
Windows 7
Server xampp stack

I enabled the debugger in the user system.yaml file. I also tried enablling it in the system config file (this I changed back to the default).

I ran bin/grav clear-cache with on success.

Are there any additional steps that I’m missing?

If the debugbar is showing up on your page, then you should be able to add debug messages.

One thing to note, is that in a plugin, by default you would use:

$this->grav['debugger']->addMessage('my test message');

Unless you have already defined a local $grav variable.

Debug messages will show up in Messages panel of the debugbar:

Login | Demo2 2014-12-20 23-13-04

You might want to try putting a debug message in your onPluginsInitialized method to make sure that’s getting processed. If you are still not seeing anything, you might have a typo in the way you named your folder/classes/yaml files,

Thanks for the quick response.

I solved this issue.

Is it possible to get a list of js assets. I see in there is a method in the Assets.php file - getJs() - but it returns an empty array. I would like to check that jquery is available as this plugin rely on the library. Most other functionality rely on jquery but I would like to make sure it’s loaded.

Sorry sould say that I’m interested in the page assets not Grav’s assets. Those assets are displayed.

Pages are only aware of their own images/files/videos, you can get those via {{ page.media }}. JQuery will typically be loaded by the theme itself, and you were on the right path by looking in Grav assets, getJs() is what you want, but the problem you ar having is more likely related to when you are checking.

Most plugins add their own assets during the onTwigSiteVariables event, which is pretty late in the process. Also the theme themselves add CSS, and JS assets in the template process, so that is even later around onOutputGenerated. If you check any earlier, there probably is not going to be anything in there yet.

Check out the Event Hooks docs for more info

You really have two options primary options at this point.

  1. Continue down your route your going, but put your check in onOutputGenerated. However as the Twig has already been processed at this point, you will probably have to use REGEX to add jquery to the generated output. A bit messy :frowning:

  2. I would suggest an easier/cleaner approach. Have a plu gin option that when enabled, it simply adds a plugin-provided JQuery reference to Assets object. By default this could be turned off. Then just use documentation to say that this option can be enabled on the plugin if no JQuery is provided by the theme.

The only other alternative would be if Grav were more aware of JQuery, rather than leaving it up to the themes. This could be something as simple as having a JQuery plugin that could be used by all themes. This would solve your problem as you could simply check/require this plugin, but does open up other possible issues related to themes still providing their own versions/libraries. etc.

Thanks for the input, it seems option two is the simplest so I will use that.

I think its the best option. 99% of themes are going to have JQuery loaded anyway. All the ones so far have it.