MODX Community

Plugin Help - Showing different Templates for contexts

I’m struggling to find documentation to see if this would work but I can’t find anything related.

This has to do with the manager side of things. Just want to control what end users are able to choose as a template while in the backend using contexts. I tried to use ACL for elements but nothing was working and I was thoroughly confused. Any help would be awesome.

<?php
if (isset($resource) && $resource instanceof modResource) {
    if ($resource->get('context_key') == 'some Context Key') {
       // only show template ids (4,5)
    
    return;
    
    } elseif ($resource->get('context_key') == 'some Context Key') {
        // only show template ids (2, 3)
        
    return;
    
    } else {
        // default template id (1)
        
    return;
    
    }
}
?>

I don’t think there is an applicable system event for your plugin code.

I guess you have to use ACLs. ACL for elements are based on Categories. So maybe it works if you put your templates in different categories. The user needs the permission list to see the template in the combobox.

As halftrainedharry says, it can’t really be solved by a plugin, mainly because the dropdown is populated by JavaScript on the fly and not until you click on the template list heading.

It might be possible to use JavaScript to intercept the click and populate the list yourself with your own JavaScript, but it would be extremely difficult.

Putting the desired templates in separate categories, then putting the users in different groups and connecting the correct category to the correct user group with a Category Access ACL entry might well work.

Thank you both for the response. I did get this work and I need to point out something that changed.

Mysteriously after 2.7.3 upgrade, users can no longer login. The policy changed somehow. I just spent a several hours to try and figure this out here is what I did to get around this.

Editing User Groups

  • Context Access -> using “Access Policy” -> “Context” (user account denied when logging in).
  • Context Access -> using “Access Policy” -> “Administrator” (everything back normal users can log in)

I’m not sure what happened and I realize I need to tone down that policy but “Context” is not working at all.