In the previous article, we looked at a technique to hide elements on the Create/Edit Resource page. The plugin was seriously 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 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 element 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 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>
#ext-gen113 {
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 for code purists who don't think a bit of code should have two return statements:
$action = $_REQUEST['a'];
if ($action == 'resource/create' || $action == 'resource/update') {
if (! $modx->user->isMember('Administrator')) {
$css = '
<style>
#ext-gen113 {
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>
#ext-gen113 {
display: none !important;
}
</style>
';
$modx->regClientCSS($css);
}
}
return '';
The first if statement 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 the next article, we'll see how to hide the Static field for elements (Snippets, Plugins, Templates, TVs, and Chunks).
For more information on how to use MODX to create a web site, see my web site Bob's Guides, or better yet, buy my book: MODX: The Official Guide.
Looking for high-quality, MODX-friendly hosting? As of May 2016, Bob's Guides is hosted at Hosting.com (formerly A2 Hosting). (More information in the box below.)

Comments (2)
Laurent Laroudie — Sep 28, 2016 at 10:55 AM
Hello Bob,
once again you save me !
I had this problem with Lingua and correct hiding of elements :
https://github.com/goldsky/Lingua/issues/52#issuecomment-210551670
This solve the problem, but…
How to personalize this by template
ie :
- template A => i want to always hide menutitle
- template B => i want to hide published and long title too
- etc.
so the question is :
How, in a plugin, what is the correct syntax to get the template of the resource i'm working on ?
Thanks again,
and have a nice day.
/.
Bob Ray — Mar 23, 2017 at 11:22 PM
Sorry for the long delay. I thought I got notifications of new comments, but I don't.
You've probably long since solved this, but for others: In most of the events you would use for resources, the resource is available as $resource, so you can get the template ID for the resource's template with $resource->get('template').
Please login to comment.