Create MODX User Groups in a Plugin

As I said in the previous blog article, plugins can be a great way to automate repetitive tasks in MODX. In this article, we’ll look at an example that puts users into a user group based on their zip code (or whatever is in the zip field of the user profile), creating the user group if necessary.

MODX logo

General Approach

In our plugin, which will only execute for new users, we’ll first get the content of the zip field of the user profile. If it’s not empty, we’ll check to see if there is a user group by that name (after adding the "Group." prefix). If not we’ll create it. Finally, we’ll add the user to the group.

The Code

/* UserGroupFromZip plugin --
   attached to OnUserFormSave event */

/* Do nothing if it's not a new user */
if ($mode !== modSystemEvent::MODE_NEW) {
    return;
}

/* Get the User Profile */
$profile = $user->getOne('Profile');

/* Make sure the user has a profile */
if ($profile) {
    $zip = $profile->get('zip');
    /* get the content of the 'zip' field, if any */
    if (! empty($zip)) {
        /* add our prefix */
        $groupName = 'Group' . $zip;
        /* See if the group already exists */
        $userGroup = $modx->getObject('modUserGroup',
            array('name' => $groupName));
        /* Create the group if necessary */
        if (! $userGroup) {
            $userGroup = $modx->newObject('modUserGroup');
            $userGroup->set('name', $groupName);
            $userGroup->save();
        }
        $user->joinGroup($groupName);
    }
}
return;

Setting $groupName as a variable is not strictly necessary, but it’s a good programming practice, since we use the name in several places. Without the variable, one of those instances could contain a mistake that would make the plugin fail. It might take some time to find the problem. In addition, if you decide you want to change the form of the group name, with the variable, you only have to change it in one place.

Many MODX methods will let you identify the object you want either by name or by id. The joinGroup() method, for example, will let you send the name of the user group or its ID. In order to prevent confusion, it’s a good general principle not to use names that start with a number. It makes it absolutely clear what we’re sending. That’s why our example prefixes the zip-code group names with "Group." Many MODX extras use the PHP function is_numeric() to figure out if they’re getting a name or an ID and any name composed entirely of numbers will be interpreted as an ID.

Creating the Plugin

  • Right-click on the "Plugins" folder in the Elements tree and select "New Plugin"
  • Name the Plugin UserGroupFromZip
  • Paste in the code above
  • On the "System Events tab, put a checkmark next to OnUserFormSave
  • Save the plugin

Once the plugin has been saved, all new users will be placed in a user group based on the content of the zip field.

Customization

The plugin described above could be modified to perform a variety of user-group tasks. You could, for example, create a new user group based on the user’s name, so each user would be in his or her own user group. You could group users by city, telephone prefix, gender, date-of-birth, or anything else that’s available in the user profile. Note that for some fields, you may have to add a little code to create the group name. For the telephone prefix, you’d have to make sure the number has a prefix and extract it. For the gender field, the value is a 1 or a 0, so you’d probably want to translate that.

For our plugin above, you’ll want to add some code that validates and normalizes the zip code value. We’ll look at how to do that in the next article.

Other Uses

If you connect the plugin to another event and modify it, you could do lots of other things with it. In a future article, for example, we’ll look at how to set the value of resource fields and template variables in a similar plugin attached to the OnDocFormSave event.

 


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 quality MODX Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , , | Posted under MODX, MODX | RSS 2.0

Author Spotlight

Bob Ray

Bob Ray

I am the author of MODX: The Official Guide and over 30 MODX add-on components. I host Bob's Guides, a source of valuable information for MODX users, and I've been very active in the MODX Forums with over 14,000 posts.

Leave a Reply

Your email address will not be published. Required fields are marked *


1 × = 2

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>