Hiding the help button III

Hiding the help button in the Manager with CSS


In the previous article, we used a plugin to hide the "Help" button that appears on the panel for creating and updating various MODX objects like Resources, Elements, System Settings, and Media Sources, from everyone. In this article we'll look at a way to select which users the button is hidden from based on the User Groups they belong to.


MODX logo

The Old Plugin

This is the plugin from the previous article (see the previous article for the steps to create the plugin):

/* HideHelp Plugin */
$css = '
    <style>
        #modx-abtn-help {
            display: none !important;
        }
    </style>
';

$modx->regClientCSS($css);

return '';

The New Plugin

It takes a very small modification of the plugin to selectively hide the Help button based on the current user's User Group. The isMember() method can be used to tell if a user is a member of a given User Group (or multiple User Groups). We just need to wrap the code that hides the button in an if statement. This version of the plugin will will hide the Help button from anyone who is not a member of the Administrator user group:

/* HideHelp Plugin */
if (! $modx->user->isMember('Administrator') ) {
    $css = '
    <style>
        #modx-abtn-help {
            display: none !important;
        }
    </style>
    ';

    $modx->regClientCSS($css);
}

return '';

The exclamation point in the if statement can be read as "not", so the code inside the outer curly braces will only execute for users who are *not* members of the Administrator group. Here is another way to do the same thing:

/* HideHelp Plugin */
if ($modx->user->isMember('Administrator') ) {
    return '';
}

$css = '
<style>
    #modx-abtn-help {
        display: none !important;
    }
</style>
';

$modx->regClientCSS($css);

return '';

In this second version, if the user is a member of the Administrator group, the plugin returns without doing anything at all. For non-Administrators, it continues on and executes the code that hides the button. It's a common principle to have only one return statement, which appears at the end of your code. It makes the code cleaner and easier to maintain. I violate this principle occasionally if it will speed things up, though in this case there would be no significant time difference between the two versions, so I would probably use the first one.

While we're on the subject of coding practices, here's an example of something I see often that you should never do:

/* HideHelp Plugin */

$css = '
<style>
    #modx-abtn-help {
        display: none !important;
    }
</style>
';

if (! $modx->user->isMember('Administrator') ) {
    $modx->regClientCSS($css);
}

return '';

Do you see what's wrong with this code? It would work, and it seems logical that you would only inject the CSS if the user was not an Administrator. But why execute the code at the top of the plugin to set up the content of the $css variable if it's never going to be used. It's just wasting processor time and memory (in this case, only a tiny bit — but why waste anything?). If you have an if statement that tests for a condition under which some operations will be performed, put *everything* that will be performed based on the results inside the if statement.


Test for Multiple Groups

The isMember() method can take an array of User Group names as its argument. This version will hide the button from everyone who is not a member of either the Administrator or Editor Groups:

/* HideHelp Plugin */
$AllowedGroups = array(
    'Administrator',
    'Editor',
);

if (! $modx->user->isMember($allowedGroups)) {
$css = '
<style>
    #modx-abtn-help {
        display: none !important;
    }
</style>
';

$modx->regClientCSS($css);
}

return '';

For completeness, I should mention that isMember() takes a second argument that is seldom used. If set to true, it tests whether the user belongs to *all* groups in the array. If you wanted to show the button only to people who were both Administrators and Editors, you'd do this:

/* HideHelp Plugin */
$AllowedGroups = array(
    'Administrator',
    'Editor',
);

if (! $modx->user->isMember($allowedGroups, true)) {
$css = '
<style>
    #modx-abtn-help {
        display: none !important;
    }
</style>
';

$modx->regClientCSS($css);
}

return '';

Coming Up

In the next article, we'll see how to hide some other page elements using the same CSS injection technique we used to hide the Help button.


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 A2 hosting. (More information in the box below.)



Comments (0)


Please login to comment.

  (Login)