We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7155
    • 160 Posts
    Firstly, thanks for the great CMS. am still learning it and appreciating its power.

    Now...to the point!:

    I am developing a custom module for a client and would like to know what the best practices are on the following:

    • The Module files (snippets,configs and module code)
      -Is it best practice to put ALL my snippets,chunks and module code in my module directory e.g.:
      for snippets: /assets/modules/mycustommodule/snippets
      for chunks: /assets/modules/mycustommodule/chunks
      and likewise for everything else
      OR
      - For snippets, should I create a folder /assets/snippets/mycustommodule/ ...etc?

    • Module configuration
      - I have gone through some code of popular/existing modules in the modx repository. Modules like txnewsletters and easypoll to gain insight on how they were developed. I noticed that some have a config file. Is this necessary when there is a Module Configuration option in the ModX module manager? (http://www.modxcms.com/module-configuration.html)
      What if I want my own configuration section of the module. Is that advisable? Is a configuration file necessary? Would that be re-inventing the wheel? Does that defeat the advantage of the modx module configuration? I want to make the best use of the API using recommended practices.
    • Events
      - Can I register and invoke my own events? I dont know why I’m asking but am curious...thats why I guess

    Finally is there an existing or an example of a well-written module that has snippets,chunks...etc?

    Thanks again for the great product. Please excuse my ignorance, am a newbie to modx
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      First of all you’ll need to get the terminology straightened out to avoid confusion.

      Snippets are bits (not always small bits!) of PHP code for front-end dynamic behavior, similar to having <?php...?> tags in an HTML document. Wayfinder, for generating lists and menus, is a good example of a snippet. Any included or support files for snippets are stored in assets/snippets/snippetname.

      Modules are extensions to the Manager, adding new features such as newsletter management. Included and support files for modules are stored in assets/modules/modulename.

      Plugins are inserted code to modify processing at specific events, such as when a user logs in, or at certain points during the parsing of a document to generate a web page. Again, includes and support files are stored in assets/plugins/pluginname.

      The latter two are used for adding or modifying core and Manager behavior without modifying the core scripts themselves.
        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
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        For modules and plugins it really doesn’t matter if you include a configuration file or use the Configuration tab. Both ways the name/value pairs will be passed in to the application at run-time. Having them in the Configuration tab makes it a bit easier for users to configure them after installation.

        For snippets, it’s a different matter. For example, take the Wayfinder snippet. You could use the Configuration tab to specify the startId for all instances of Wayfinder calls. But menus are generated at all different levels of websites, requiring different values for the startId, so a global setting like this doesn’t make a lot of sense. In this case, using parameters in the snippet’s tags for each individual call makes more sense. On the other hand, snippets like Wayfinder, Ditto and eForm can have very long parameter lists. So it makes even more sense to just have one or two parameters in the snippet tags, and have one of those parameters specify a config file, where all the other settings are defined with simple PHP variable definitions. You can even have the templates for their various elements defined in the config file instead of using chunks, thus saving some database activity as well as saving space in the site cache 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
          • 7155
          • 160 Posts
          Quote from: sottwell at Aug 06, 2008, 07:20 AM

          First of all you’ll need to get the terminology straightened out to avoid confusion.

          Snippets are bits (not always small bits!) of PHP code for front-end dynamic behavior, similar to having <?php...?> tags in an HTML document. Wayfinder, for generating lists and menus, is a good example of a snippet. Any included or support files for snippets are stored in assets/snippets/snippetname.

          Modules are extensions to the Manager, adding new features such as newsletter management. Included and support files for modules are stored in assets/modules/modulename.

          Plugins are inserted code to modify processing at specific events, such as when a user logs in, or at certain points during the parsing of a document to generate a web page. Again, includes and support files are stored in assets/plugins/pluginname.

          The latter two are used for adding or modifying core and Manager behavior without modifying the core scripts themselves.
          thank you for clarifying on the terminology! very clear explanation

          am still trying to grasp the definition of plugins though
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            If you look at the file manger/includes/document.parser.class.inc.php, you’ll see that this is the main parser script for generating web pages out of templates, snippets, chunks, template variables and documents. If you do a search for "invokeEvent", you’ll see a number of these events sprinkled through the code. They also exist in the manager/action files, as well as the manager/processor files. Plugins can intercept these "events" and add their own code to modify whatever process is going on, without having to modify the original script code.

            For example, I’m researching LDAP with the thought of adding LDAP compatibility layer to MODx. I will need to use events involving creating new users, validating users, in fact anything having to do with users, in order to insert the code to add the user data to the LDAP database, and to validate the user against the LDAP database, while still having all of the MODx user management functionality. I’ll have a plugin that "listens" for several of these events in the user management scripts and does whatever necessary with the LDAP database, then returns processing to the MODx script where it left off, perhaps with a couple of extra variables assigned for it to work with.

            If you’ve ever worked with a program like CubeCart, and all of its "mods", you’ll know what I mean. Having to hack the core code to add or modify functionality is a nightmare. Pretty soon you’ve got hacks of hacks, then there’s a major security update and you’re SOL because you don’t even remember which hacks went where!

            Snippets can also use this feature. A commonly used case of a snippet with events is the eForm snippet. It is a basic form validation and mailing script, originally intended for contact forms, that sort of got out of hand. Now it has several events where you can add your own functions to add more validation, get data from a database to fill a select drop-down field for the initial form generation, save form POST data to a file or the database, or modify the data before it gets mailed off, or just about anything else you can think of to do with a form. All this, with no need to hack the original eForm code. In this case, instead of using plugins, you use a snippet to define your function and make sure the snippet is called before the eForm snippet, so the function will be available.

            The TreasureChest ecommerce module/snippet combination that ScottyDelicious is working on uses events, but he’s registered them in the database along with the other MODx script’s events, so you can use plugins with it. All that means is that the TreasureChest installer adds the events to the system_events table in the database. Then they’ll show up on the plugin form’s System Events tab, and the parser will insert the code at the appropriate point in the script’s processing.
              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
              • 7155
              • 160 Posts
              Susan, thank-you for the time you take to explain all that information. It’s really helpful
                • 7231
                • 4,205 Posts
                The wealth of information that Sottwell shares is fantastic. grin
                  [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

                  Something is happening here, but you don&#39;t know what it is.
                  Do you, Mr. Jones? - [bob dylan]
                  • 16183
                  • 1,390 Posts
                  Quote from: dev_cw at Aug 07, 2008, 06:54 AM

                  The wealth of information that Sottwell shares is fantastic. grin

                  Ditto that Shane (no pun intended) wink