Make MODX Plugins Execute only in Specific Contexts

How to make your MODX plugin execute, or not execute in one or more specific contexts


In an earlier article, we looked at how to create and call custom MODX system events in your code. In this one, we'll look at making plugins fire or not fire in specific contexts.


MODX logo

The Problem

Imagine that, on a multi-context site, you have a plugin tied to a front-end event like OnWebPagePrerender. The plugin makes sense in all but one context. In that context, you don't want the plugin to execute, either because it would be a waste of time and resources, or because it would mess things up.

Without some special code, your plugin will execute any time a resource in any context is visited. Luckily, it's very easy to make sure the plugin only executes when you want it to.


The Solution

To restrict the plugin to a single context, you just need to check the key of the current context. Suppose you have a French language context with the key fr where you don't want the plugin to execute. This code at the top of your plugin would do it:

if (isset($modx->context)) {
    if ($modx->context->get('key') === 'fr') {
        return;
    }
}

If the user is in the fr context, the plugin will return without doing anything.

We can use the strict equality operator here (===), which requires that both sides be of the same type, because the context's key field will always be a string.


Limiting the Plugin to One Context

Suppose you want the plugin to execute *only* in the fr context. A very slight modification of the code above will handle that:

if (isset($modx->context)) {
    if ($modx->context->get('key') !== 'fr') {
        return;
    }
}

By changing the first = to !, we've made the plugin return without doing anything unless we're in the fr context.


Multiple Contexts

Imagine that you have a number of contexts, but you want your plugin to execute for only three of them: fr, de and en. Here's how to do that:

$allowedContexts = array('fr', 'de', 'en');
if (isset($modx->context)) {
    $key = $modx->context->get('key');
    if (! in_array($key, $allowedContexts)) {
        return;
    }
}

If you wanted the code to execute in all contexts *except* those three, you'd do this:

$disAllowedContexts = array('fr', 'de', 'en');
if (isset($modx->context)) {
    $key = $modx->context->get('key');
    if (in_array($key, $disAllowedContexts)) {
        return;
    }
}

Notice that we've changed $allowedContexts to $disallowedContexts to make the code clear in addition to removing the ! sign in the test.


What About the Manager Context

You might think that the mgr context should be in the list of disallowed contexts to make sure that the plugin will never execute in the MODX Manager. There's no reason to do that, though, because OnWebPagePrerender will never fire in the Manager. It only fires in the front end.

There might be times, though, when you want to restrict the execution of a plugin that responds to a back-end event like onDocFormPrerender. We'll look at that situation in the next article.

Coming Up

In the next article, we'll see how to disable or enable plugins in the Manager based on the context of a resource.



Looking for high-quality, MODX-friendly hosting? As of May 2016, Bob's Guides is hosted at A2 hosting. (More information in the box below.)



Comments (0)


Please login to comment.

  (Login)