A plugin is a piece of code that can be attached to any of the available events within the MODx system. The events cover both front-end actions such as OnWebPagePrerender through to back-end actions such as OnManagerLogout. (as opposed to a snippet that just pushes output onto the webpage (in most cases!), and a module that is a webpage/app that can be run inside the Manager with full access to the MODx API)
To see a full list of the events:
1. Goto: Manager->Manage Resources->Plugins Tab
2. Create a new plugin (or edit one of the existing ones) and switch over to the ’System Events’ tab.
Explaining the power of what plugins can achieve is better served with an example:
In my Autolink plugin, which is designed to dynamically replace keywords on a webpage with hyperlinks, the plugin is assigned to run on the OnWebPagePrerender event (ie. the code will run immediately before the content is outputted to the browser). The plugin parses through the webpage HTML and does the necessary replacements, when the plugin has finished doing what it needs to do normal MODx code execution continues and the page is outputted to the browser.
In MODx 0.9.5+, an additional feature will be that the run order of plugins that are assigned to a particular event can be controlled. So, plugins can be stacked. For Autolink, this may mean that somebody who is using the BBCode plugin can convert the BBCode to HTML and then run the Autolink to replace the keywords. Combining plugins in this way can produce some pretty nifty results.
Also, a plugin can be assigned to more than one event, so a piece of code can be utilised in more than one instance. In this scenario, the event needs to be determined in the plugin code so that the appropriate event code can be run:
// Get a reference to the event
$e = & $modx->Event;
// Run the appropriate event code
switch ($e->name) {
case 'OnWebPagePrerender':
// code here
break;
case 'OnManagerSaveUser':
//code here
break;
default:
return; // do nothing
break;
}
Now, what variables are accessible for the different events varies. For OnWebPagePrerender, there is access to the $modx->documentOutput variable that contains the content for the page, whereas if you were using OnManagerSaveUser, you would have access to the user ID that is being saved. It may be that you don’t need to manipulate this information - for example, a plugin that logs page views would update a value in the database and wouldn’t need to touch the page output.
If you only needed to output some HTML code into the content flow (in the case of events such as OnDocFormPrerender) then the output can pushed out from the plugin using: $e->output($myOutput);
The possibilities are endless ...
Anyway, I hope this helps, it’s a quick skim over some of the possible options and ways of doing things and by no means exhaustive.