In a previous articles, we looked at a technique to hide the "Help" button on the Create/Edit Resource page. The plugin was somewhat flawed, however, since if it executed on other pages in the Manager, it might well hide things that shouldn't be hidden. In this article, we'll see a way to limit the execution of the plugin to specific actions.
The Method
If you look at the URL in your browser's address bar for various pages in the MODX Manager, you'll see that it almost always ends with something like this:
/manager/?a=element/plugin/update&id=5446
The part after the question mark is called the "query string" and it contains variables sent as part of a get request. That means that the variables and their values will be contained in the $_REQUEST array (which includes the $_GET array). You can see the full $_REQUEST array in the error log by putting this code in a plugin attached to OnManagerPageBeforeRender, and viewing any Manager page in the browser:
$modx->log(modX::LOG_LEVEL_ERROR, 'Request Array: ' . print_r($_REQUEST, true));
When you go to a page in the Manager where you would update an existing resource, this would appear in the error log:
[2016-07-12 01:58:18] (ERROR @ /addons/manager/index.php) Request Array: Array
(
[a] => resource/update
[id] => 5446
)
Notice the first member of the array. It contains the action MODX is performing — in this case resource/update. The Manager action will always be available in the plugin in the variable $_REQUEST['a']. We can use that to make sure the plugin will only execute on the appropriate pages. The second member of the array is the ID of the element to be operated on (not the Manager page you're viewing).
The Improved Plugin
This plugin will only execute for the Create/Edit Resource panel:
$action = $_REQUEST['a'];
if ($action !== 'resource/create' && $action !== 'resource/update') {
return;
}
if (! $modx->user->isMember('Administrator')) {
$css = '
<style>
#limenu-about {
display: none !important;
}
</style>
';
$modx->regClientCSS($css);
}
return '';
The plugin code checks the action before executing. 'resource/create' or 'resource/update'. If it's not 'resource/create' and it's not 'resource/update', the plugin returns without doing anything.
Here is an alternate version with doesn't have two return statements (a good practice in general):
$action = $_REQUEST['a'];
if ($action == 'resource/create' || $action == 'resource/update') {
if (! $modx->user->isMember('Administrator')) {
$css = '
<style>
#limenu-about {
display: none !important;
}
</style>
';
$modx->regClientCSS($css);
}
}
return '';
Notice that we've changed both equality tests to == and we've changed the logical connector from && (AND) to || (OR).
Here's yet another version that's a little more efficient:
$action = $_REQUEST['a'];
if (strpos($action, 'resource') !== false) {
if (! $modx->user->isMember('Administrator')) {
$css = '
<style>
#limenu-about {
display: none !important;
}
}
</style>
';
$modx->regClientCSS($css);
}
}
return '';
The first if statement in the code above checks to see if the action contains 'resource'. The code inside it will only execute if it does. Since 'resource' appears in both 'resource/create' and 'resource/update', and no other actions on Manager pages will contain 'resource', the code should have the same effect as the versions above.
Coming Up
In my next article, we'll see how show some of the hidden Resource fields on the Create/Edit Resource panel in the MODX Manager.
About Bob Ray
Bob Ray is the author of the MODX: The Official Guide and dozens of MODX Extras including QuickEmail, NewsPublisher, SiteCheck, GoRevo, Personalize, EZfaq, MyComponent and many more. His website is Bob’s Guides. It not only includes a plethora of MODX tutorials but there are some really great bread recipes there, as well.
Learn more about Bob Ray.