This is an ongoing list of best procedures and tips for MODx module development. Modules are used in the MODx manager to help site administrators control custom functionality.
Pasting PHP Code into the Manager
Pasting PHP code into the Module Manager or Snippet Editor can be extremely frustrating. Copy/paste your code into a file under /assets/modules/{module-name}/{module-name}.module.php or assets/snippets/{snippet-name}/{snippet-name}.snippet.php.
For modules, delete all PHP source in the Module Manager editor and paste:
<?php require_once($modx->config['base_path'] . "assets/modules/{module-name}/{module-name}.module.php"); ?>
For snippets, delete all PHP source in the Create/edit Snippet textarea and paste:
<?php require_once($modx->config['base_path'] . "assets/snippets/{snippet-name}/{snippet-name}.snippet.php"); ?>
- If your snippet fails to display after require-ing the helper file. Try using echo to output the snippet’s response.
- This method allows you to continue using your preferred PHP editor while developing modules and takes two steps out of the testing procedure.
Helper Files
Most of the modules I develop have overlapping functionality. I like to move generic/overlapping functions into an external file and include them in my modules and snippets. This practice helps keep my main module and snippet code tidy. If you prefer an object-oriented approach, you can use classes instead of procedural functions.
As an example, my module ‘user_manager.module.php’ includes a ‘user_manager.inc.php’. This inc.php file is included in the admin area module and front-end snippet.
Database Access
The following code snippet will give you a quick overview of how to access the database from a MODx module:
function getUserTotalPoints($web_user_id) { global $modx; $web_user_id = $modx->db->escape($web_user_id); $tp = $modx->db->config['table_prefix']; $fields = 'SUM(tbl_points.points) AS total'; $from = $tp . 'web_user_competency_points AS tbl_points'; $where = 'tbl_points.web_user_id=' . $web_user_id; $result = $modx->db->getRow($modx->db->select($fields, $from, $where)); return intval($result['total']); }
- $modx is the document parser.
- $modx->db is the database connection to the primary MODx database.
- $tp is the table prefix for the database (default is ‘modx_’).
- The example above assumes there will only be one row returned. Since the query will never return more than one row, we can use getRow() on a select() in a single line.
- If your result set will return more than one row, use:
while($row = $modx->db->getRow($result))
Refer to http://wiki.modxcms.com/index.php/API:DBAPI for more information.
Module Sections
A single module may have more than one section or state. I use an ‘opcode’ to keep track of the current module state. For more information visit: http://svn.modxcms.com/docs/display/MODx096/Writing+the+module+code
Your blog post is pretty good regarding the developing the and using external files. I tend to also use external files because I find the lack of syntax and formatting to be extremely difficult to use for programming and future program maintenance.
Do you have any other tips for developing and debugging in MODx?