Writing the module code
There are many ways to roll your own code but for this tutorial we are going to code our module so that it can make use of the MODx APIs.
When a module is first executed the code stored inside the PHP field is evaluated or called from within the execute_module.inc.php file. This means that our module can reference the $modx variable when it's executed.
For example:
$doc = $modx->getDocument(1); echo $doc['pagetitle'];
In the above example we can either use the echo keyword or the return keyword to render text on the screen.
If you where to paste the above code inside the phone book module you would see the page title of document 1 been displayed. In my case you would display the following:
Introduction
We would also rewrite the above as:
$doc = $modx->getDocument(1); return $doc['pagetitle'];
This time we are using the return keyword to render text on the screen.
Using the echo keyword might be more convenient for your specific need.
For example:
echo '<h1>Phone book</h1>'; echo '<h3>Manage your contacts with ease</h3>'; echo '<hr />';
Now that you have gotten the basic idea of how a module works you can proceed to write your module the same way you would any PHP app with exception that your app will be running inside a frame and will have access to the MODx APIs.
Another thing to bear in mind is that the content manager will execute your module for every postback, providing that you send your postbacks to the page where the module was first executed.
In other words if you load or link to an external page after the module have been executed then the MODx APIs will not be available for those pages.
In order to use the MODx APIs you should postback to the initial page. To do this you could just simply wrap my pages inside a single
and use the following postForm function posting form data or calling another page from the module:
<script language="JavaScript" type="text/javascript"> function postForm(opcode){ document.module.opcode.value=opcode; document.module.submit(); } </script> <form name="module" method="post"> <input name="opcode" type="hidden" value="" /> <!-- content goes here --> </form>
Note: The opcode field can be used as a function directive for calling an action from within the module.
When sending information back to the server you can use:
<a href="javascript:;" onclick="postForm('save')">Save</a> <a href="javascript:;" onclick="postForm('delete')">Delete</a> <a href="javascript:;" onclick="postForm('search')">Search List</a>
Here's what the final module code might look like.
/* A simple module */ $opcode = isset($_POST['opcode']) ? $_POST['opcode']:''; // action directive switch($opcode) { case 'save': // save code here echo 'Save action'; break; case 'delete': // delete code here echo 'Delete action'; break; case 'search': // search code here echo 'Search action'; break; default: // display module page echo '<html>'; echo '<head></head>'; echo '<body>'; echo '<script language="JavaScript" type="text/javascript">'; echo ' function postForm(opcode){'; echo ' document.module.opcode.value=opcode;'; echo ' document.module.submit();'; echo ' }'; echo '</script>'; echo '<form name="module" method="post">'; echo '<input name="opcode" type="hidden" value="" />'; echo '<h1>Phone book</h1>'; echo '<h3>Manage your contacts with ease</h3>'; echo '<hr />'; echo '<a href="javascript:;" onclick="postForm(\'save\');return false;">Save</a> | '; echo '<a href="javascript:;" onclick="postForm(\'delete\');return false;">Delete</a> | '; echo '<a href="javascript:;" onclick="postForm(\'search\');return false;">Search List</a> | '; echo '<a href="javascript:;" onclick="postForm(\'\');return false;">Main</a>'; echo '</form>'; echo '</body>'; echo '</html>'; break; }
Suggest an edit to this page on GitHub (Requires GitHub account. Opens a new window/tab).