ClassExtender - how to limit display to specific users / resources

We’re using the excellent Class Extender plug in by Bob Ray. Does anyone know if there is a way to limit the display of the extended fields?
For example:

  1. The chunk MyExtraResourceFields displays the extended fields on each resource. Can this be limited to display the extended fields only on child resources of Parent X?
    or
  2. The chunk MyExtraUserFields displays the extended fields on all users. Can this be limited to display the extended fields on users belonging to User Group Y?
    Thanks

You probably have to change the pluginsExtraResourceFields” and “ExtraUserFields” for that.
It’s most likely a good idea to duplicate the plugins first and then deactivate the original ones.

At the start of the plugin query the values of the variables $resource (ExtraResourceFields) or $user (ExtraUserFields) to determine if the rest of the plugin should run.

Maybe something like this works:

if (isset($user) && ($user instanceof modUser)) {
    if (!$user->isMember('MyUserGroup')){
        return;
    }
}

//Rest of the code
...

I’ve never used ClassExtender before, so this might not apply here, but in general you can also limit user access by managing the chunks category through the Element Category Access within the ACL settings for a specific role.

Thanks for the replies. Halftrainedharry, presumably the plugins “ExtraResourceFields” and “ExtraUserFields” need editing prior to the initial implementation via viewing ‘Extend modUser’ and ‘Extend modResource’ pages?
I tried it without, just adding into the plugin, but didn’t seem to have any effect - although it looks like the plugin runs OnUserFormRender

I don’t think so.
These pages are to create the schema.

In the plugin you just determine when to show/save the additional fields.

  • What exactly did you change in the plugin?
  • Did you clear the cache after changing the plugin?

Thanks for the pointers, HalfTH.

So this works:
Duplicate ‘ExtraUserFields’ to ’ MyExtraUserFields’. In the original ’ ExtraUserFields’, tick ‘Inactive Plugin’ and save. Same process for ’ ExtraResourceFields’

To limit display of MyExtraUserFields to a specific user group, edit MyExtraUserFields to:

/* Make sure we have an extUser object to work with */
if (isset($user) && ($user instanceof  modUser)) {
        if (!$user->isMember('UserGroupName')){
        return;
        }
        else {
        $data = $modx->getObject('userData', array('userdata_id' => $user->get('id')));
        }
}

To limit MyExtraResourceFields to resources with a specific parent (e.g. id =19), edit MyExtraResourceFields to:

/** @var $resource modResource */
if ($resource && $resource instanceof modResource) {
    $parent = 19;
    $p = $resource->get('parent');
        if ($parent != $p) {
            return;
        }
        else {
        $data = $modx->getObject('resourceData',
        array('resourcedata_id' => $resource->get('id')));
        }
}```

With these changes not every resource (or user) has now a corresponding entry in the database table ext_resource_data (or ext_user_data).

Depending on how you output your data this may (or may not) create a problem.

Say you move an existing resource in the resource-tree, so that it is now a child of the resource 19. This resource only gets an ext_resource_data entry, when you save it the next time in the manager.
The same is true for existing users, that you newly assign to your user group ‘UserGroupName’.


If the current implementation creates problems, then maybe you have to change the plugin, so that it just creates an empty database-entry for every resource/user.
Or maybe the plugin could additionally run on system events like OnUserAddToGroup or OnResourceSort to react to relevant changes of the user/resource.

We can’t see this creating problems for our use case. The site is up and running for some time and there’s little or no moving of resources or changing user roles. Many users and resources already don’t have corresponding entries in ext_resource_data or ext_user_data, it’s only those users / resources that need the new data adding who get the entries.

We’ll need to ensure any additional snippets we create are checking that the data exists before processing to avoid errors, so thanks for the heads up.