We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 9207 ☆ A M B ☆
    • 2,475 Posts
    Forgive me if this is a noob question... I’ve only written a few modules, so I’m not too familiar with them.

    Basically, I have a Snippet that I want to execute for manager users only... in the past I’ve accomplished this type of thing by creating a module, which is nice... you get your own link in the modules tab and you can rest assured that only the manager users with module access could run it.

    However, I’m developing a Snippet that I’d like to run on the front end (as a normal vanilla snippet), but I’d also like to run it on the back-end for manager users (with slightly different parameters).

    I can think of two ways of doing this:
    1. Create a page that belongs to a certain document group that only manager users have access to, then put the Snippet there as per usual. The downside to this approach is that the manager users will have to find this particular page in the document tree, and it’s very likely to get buried.
    2. Create a module which includes a file... the Snippet is including a class file... the module could more or less do the same thing. The downside here is that I’d have virtually the same code in a Snippet and in a Module (both acting as simple accessors to the module code). The upside is that if I create a module, I can use the handy configuration tab to set parameters for the Snippet AND the module, and that could be nice for the users.

    Anyone have thoughts on this?

    Thanks, as always!
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Well, if you put the functioning code in an external file, you can simply include() it in both the snippet and a module.

      I would be inclined to use a separate config file for configuration, in case I want different configuration parameters for the front-end and the back end. The only configuration in the module or the snippet call would the the path to the desired config file.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 28439
        • 222 Posts
        I think I know what Everett means:

        <?php
        class BackenFrontend {
          
          public function doSomething($param1, $param2) {
            $result = '';
            
            if ($modx->isBackend()) {
            	$result = 'Backend result';
            } elseif ($modx->isFrontend()) {
              $result = 'Frontend result';
            }
            
            return $reseult;
          } // doSomething
          
          public function doAnotherthing($param) {
            $result = '';
            
            if (isset($_SESSION['mgrValidated']) {
            	$result = 'Backend user login';
            }
            
            return $result;
          } // doAnotherthing
        } // BackenFrontend
        ?>

          Gone away and found a better place to stay