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
    Where can I find good info on how and why to write a MODx module? The official documentation has some severe holes in it sad

    http://www.sottwell.com/manager/ has some nice stuff on it... the tasty modules (e.g. "Database -- manage external database tables") are locked, though.

    The biggest omission in the documentation is that nobody says WHEN A MODULE ACTUALLY RUNS. I’m going nuts. I can’t even make "Hello World" if I don’t know when or how to run a module. http://sottwell.com/create-module.html says simply "Run your new module!"... the official documentation at http://www.modxcms.com/module-code.html also leaves that bit out. Eeep.

    Can anyone help a brother out?
      • 26903
      • 1,336 Posts
      Well, I wrote the Auditor module as a module because :-
      It doesn’t depend on any triggering events, i.e. its not a plugin.
      It needs to be accessible from within the manager only, as an add-on to the manager itself, not in a snippet that needs to be run from a page etc.

      It’s invoked when you press the Auditor menu item in the Modules section, i.e. each module runs when you invoke it from the modules menu.
      It first sets up path data, checks perms then displays the current state of the Auditor system, subsequent key presses are directed back to the module code by MODx, so you end up with a case statement of ’isset’s deciding what internal function has been invoked.

      Does this help a bit, looking at existing module code should help also.
        Use MODx, or the cat gets it!
        • 9207 ☆ A M B ☆
        • 2,475 Posts
        Ok, to summarize:

        Write a Snippet if your code needs to execute when a page loads on the front-end. Example: displaying current date/time to user.

        Write a Plug-in if your code needs to execute when certain system events occur. Example: special action when a web-user logs in (OnWebLogin event)

        Write a Module if your code only needs to be accessible within the manager control panel. It is executed on an "as-needed" basis. Example: CRUD access to custom database table.

        Is that too overly simple?
          • 26903
          • 1,336 Posts
          Is that too overly simple?
          I think it summarizes nicely.
            Use MODx, or the cat gets it!
            • 9207 ☆ A M B ☆
            • 2,475 Posts
            One other trivial difference:

            Snippets require the opening and closing PHP tags:
            <?php 
                echo "This is my Snippet";
            ?>


            Plugins and Modules omit these tags:
                echo "This is my Plugin or Module";
              • 22303 MODX Staff
              • 10,725 Posts
              Quote from: Everett at Jan 06, 2009, 01:08 PM

              One other trivial difference:

              Snippets require the opening and closing PHP tags:
              <?php 
                  echo "This is my Snippet";
              ?>


              Plugins and Modules omit these tags:
                  echo "This is my Plugin or Module";

              I think this is simply an inconsistency in the forms; the opening and closing php tags are trimmed from the content before being placed in the database and are supposed to be put back for proper display in code editor plugins.
                • 9207 ☆ A M B ☆
                • 2,475 Posts
                Ah... thanks. That explains some weird behavior I saw. I guess to be thorough, I should have looked at what was actually being written to the database.