Page 2 of 2
The Helper and the template
Now that we have the entry point complete we need to define the helper. This simply returns the "hello world" string or whatever data you really want in a real module:
return 'Hello, World!';
Of course the class has to be called mymoduleHelper because that’s the name you use in the entry module and similarly the method has to be getHello - this is an interaction that you create.
You should be able to see how it all fits together but feel free to do it a different way if you think that it is better.
To try it all out create a new ZIP file containing all of the files including the XML file and install the module in the usual way - remembering to uninstall any earlier versions. (If you are unsure about how to do this refer to the article on Building a Joomla component .) Also remember to use the Module Manager to enable the module and assign it to a position on the page.
When you look at one of your sites pages you should see the "Hello World" output as with the first version of the module.
A module has access to most of the basic facilities that the Joomla system provides - such as the database, parameters etc - and coding these facilities within a module is the same task as coding them into a component.
The only unique element is the availability of the JModuleHelper class. This has a number of methods that allow you to work with modules from within your PHP code.
You can use JModuleHelper from within a module to get details of the module or you can use it within general PHP code to load and run a module for whatever purposes you have in mind. As well as being a Joomla module a module is also a chunk of PHP code that can be executed in a different context - if you know how.
JModuleHelper has four methods:
- getLayoutPath(module,optional layout) gets the path to the module's layout template.
This is a very simple method which builds the path in using the standard Joomla global variables and then either uses the file name specified by the optional layout parameter or default.php.
The only sophistication is that if the Joomla page template defines an a layout override for the component then it is this that takes precedence over the default or the specified name. This seems to be a little used feature but it could be used to customise any component's output according to the page template in use. In short if you want to allow the user to override your template's layout code then use getLayoutPath. If you don't then you can just as well build the path yourself.
- getModule(name,optional title) gets the module using either its name and the user assigned title or its name with mod_ prefixing it.
Notice that there can be multiple instances of a module each with a different title within a Joomla site. This method can be used to return a reference either to a particular instance according to its assigned title or via its name, e.g. mymodule, or via the folder it is stored in, e.g. mod_mymodule.
- getModules(position) gets a module specified by position on the page.
Notice that it returns all of the modules that display in that position as an array.
- renderModule(module,optional attributes) runs the specified module, providing a context as if the module was being run within the current page.
This allows one module to obtain the output of another and modify it.
There is also a function
- isEnabled(module) returns true or false depending on the module'
For example, suppose we want to process the output of the Breadcrumbs module. Then to test that it is enabled we could use:
Notice the use of all lower case - Breadcrumbs will fail to find the correct module.
Assuming the Breadcrumbs module is enabled we can obtain the HTML it creates using:
$output = JModuleHelper::renderModule(
Notice that this is the complete HTML that the module produces.
Once you have seen JModuleHelper in action you should be able to think up interesting uses for it.
The two main topics you need to study to write a more complex module or component are parameters and database access.