RSS Feed

Write a simple Joomla 1.6 module from scratch

June 28, 2010 by admin

Create you first Joomla 1.6 module completely from scratch.

Joomla, as of version 1.5 which was a complete rewrite is based on the MVC design pattern which makes creating modules extremely easy and structured. Joomla 1.6 changes a few things, the basics however remain the same.

  1. Create a folder called mod_helloworld in modules
  2. Create a mod_helloworld.php file
  3. Create a mod_helloworld.xml file
  4. Create a helper.php file
  5. Create a template file tmpl/default.php
  6. Discover new modules by going to “Extension Manager” -> “Discover”

The code for mod_helloworld.php

<?php
defined('_JEXEC') or die; // no direct access allowed
 
require_once dirname(__FILE__).DS.'helper.php'; // get helper files
 
$hello = modHelloWorldHelper::getHello($params);
require JModuleHelper::getLayoutPath('mod_helloworld');
?>

The helper.php file

<?php
	class modHelloWorldHelper
	{
	    /**
	     * Retrieves the hello message
	     *
	     * @param array $params An object containing the module parameters
	     * @access public
	     */    
	    function getHello( $params )
	    {
	        return 'Hello, World!';
	    }
	}
?>

Include the template file for the default view

<?php
defined('_JEXEC') or die;
echo $hello; 
?>

The helloworld.xml file

	<?xml version="1.0" encoding="utf-8"?>
	<extension type="module" version="1.6.0" client="site" method="upgrade">
	    <name>Hello World!</name>
	    <author>Reinhold Weber</author>
	    <version>1.6.0</version>
	    <description>Reinholds simple Hello World module.</description>
	    <files>
			 <filename module="mod_helloworld">mod_helloworld.php</filename>
	        <filename>mod_helloworld.xml</filename>
	        <filename>index.html</filename>
	        <filename>helper.php</filename>
	        <filename>tmpl/default.php</filename>
	        <filename>tmpl/index.html</filename>
	    </files>
	    <params>
	    </params>
	</extension>

Create both index.html files to prevent direct directory browsing

<html><body></body></html>

27 Comments »

  1. Daniel Böttner says:

    Sorry to say that … your xml file is not correct.
    The “param”, “params” tags are deprecated.

    The param tags for the configuration are called

    * fields
    * fieldset
    * field

    And what is also correct in your xml, install is now extension.

    greatings,

    Daniel

  2. Zaki Razak says:

    Yes, Daniel Böttner is correct in stating that “params” and “param” tag has been deprecated
    + this would not work on 1.6.

    But this is by far the most informative introduction to creating your own module; perhaps there are others but I haven’t found one yet.

    Kudos to you Reinhold Weber.

    Regards,

    Zaki

  3. Charles says:

    Hi
    Like this tutorial it’s very good.
    Would you let me know please how can I convert a current Joomla 1.5 module into and joomla 1.6 module?
    What has to be updated?

    Many Thanks for the great tutorial and Kind Regards

    Charles

  4. chris j. says:

    Can you post any information on how to convert a Joomla 1.5 module to 1.6?

    Thank you

  5. John Cross says:

    very informative.. but param is deprecated.. nonetheless informative tutorial

  6. James says:

    Hello there. this is just what i needed to start programming joomla. However I have uploaded ti and it doesn’t work, it wont show up in ‘discover’ mode and it wont install when i zip it up and do it the conventional way.

    This is the error I get when I try to install:
    * Failed loading XML file
    * /home/jtperfo1/public_html/tmp/install_4d60904560437/mod_homegrid/mod_homegrid.xml
    * XML: XML declaration allowed only at the start of the document
    * JInstaller: :Install: Cannot find Joomla XML setup file

    In discover mode, nothing happens. Please help.

    Thanks, James

  7. Zubar says:

    mod_helloworld.xml
    {No gaps}

  8. Sri Lanka says:

    Mmm XML file not correct bro. Param -> field .. etc

  9. Luc says:

    Discover!!!!!

    Thanks a lot! I could lose the whole day looking for this function!

  10. MrSteel says:

    Hi,

    I cant make module working. I can’t install. I have the same mistake as James.

    This is the error I get when I try to install:
    * Failed loading XML file
    * /home/jtperfo1/public_html/tmp/install_4d60904560437/mod_homegrid/mod_homegrid.xml
    * XML: XML declaration allowed only at the start of the document
    * JInstaller: :Install: Cannot find Joomla XML setup file

    I don’t understand what did Zubar mean with {No gaps}.

    Thanx for the help.

  11. [...] estava trabalhando em uma lista de “pecados” para programadores PHP, mas o Reinhold Weber se adiantou. Melhor, porque a lista dele é bem maior que a minha Resolvi fazer uma tradução [...]

  12. Brick says:

    It means that when you copied the xml code to your local file, there was gaps or space or tabs in front of the beginning of the code you copied.

    Remove all gaps before the opening <?xml
    and you will be fine.

  13. Vijay says:

    I have created all files mentioned. can u help me how to use that module?

  14. Goeld says:

    I created all files. Joomla discovered the Module but the Module Manager doesn’t show it up. Where is the module gone?

  15. raihan says:

    Thanks.
    It works
    Though I have to change some of your code

  16. mcgeo says:

    In this line of the .xml file

    makes sure the version matches the joomla version of your site.

    Good tutorial. Was very helpful. Thanks.

    • mcgeo says:

      Sorry, auto correct removed the code line:

      make sure — version=” 1.6.4″ — in the extension tag matches the version of your joomla install.

  17. Trikks says:

    Nice!

    I made this for joomla 1.7 a few days ago, it’s a bit extended
    http://trikks.wordpress.com/2011/10/06/joomla-1-7-hello-world-module-evolved/

    Cheers

  18. Heya i am for the primary time here. I came across this board and I find It really useful & it helped me out a lot. I am hoping to offer something back and help others like you helped me.

  19. Hi, i feel that i saw you visited my website thus i came to ?go back the favor?.I am attempting to in finding issues to improve my website!I assume its adequate to use some of your concepts!!

  20. Fantastic beat ! I wish to apprentice while you amend your web site, how could i subscribe for a weblog web site? The account helped me a appropriate deal. I were tiny bit familiar of this your broadcast offered shiny clear idea

  21. joomla says:

    It’s the best time to make some plans for the long run and it is time to be happy. I’ve learn this post and if I may just I desire to suggest you some interesting things or advice. Perhaps you can write subsequent articles regarding this article. I desire to read even more things about it!

  22. Charles says:

    Thanks for the tutorial, would you let me know please,
    how would you add css and jquery to a module?

    Many Thanks

  23. I just like the helpful information you provide for your articles. I’ll bookmark your weblog and test once more right here regularly. I’m relatively certain I’ll be told plenty of new stuff right here! Best of luck for the next!

  24. prashant says:

    u r so good programmer..
    any extra topics link send my email id.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">