Exploring MODx From the Inside: How MODx Plugins Work

How MODx Plugins Work

Plugins are custom code that is inserted at specified points in the various processes of the core of MODx. They make use of event functions that are sprinkled throughout the parser and the other processing scripts, such as displaying the forms for creating or editing documents, saving documents, snippets, etc. and at various points during user logins. In this manner it is possible to customize the core behavior of MODx without changing the core scripts.

Plugins are created in the same manner as most other MODx content; there is a form with a few fields for its name, a description, and a textarea field for the plugin code. The key to using a plugin is the System Events tab. This tab contains checkboxes enabling the plugin to "listen" as it were for whatever events are checked. The code in the plugin is executed at the point in the MODx script where that event function is specified.

A number of third-party snippets also use this "event" model to allow customization without actually hacking the original script. The eForm snippet is an example of this. It behaves differently, however, in that it does not make use of plugins; instead it uses functions embedded in custom snippets that must be called before the eForm snippet is called in order to make the custom function available to eForm. Another snippet is the TreasureChest e-commerce snippet. TreasureChest, however, does make use of plugins, by inserting its event names into the system_eventnames table in the MODx database on installation.

Creating a Plugin

To create a plugin, go to Resources, Manage Resources in the main manager menu.

The Configuration tab has four fields:

The System Events tab is where you select the event or events you want your code to intercept. For the most part, the events' naming convention makes it pretty clear at what point they are invoked. However, the Template Service events are worth taking a closer look at.




to be continued...