User

Difference between revisions of "Rvsjoen/tutorial/Developing a Module/Part 05"

From Joomla! Documentation

< User:Rvsjoen‎ | tutorial/Developing a Module
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
= Adding a helper =
 
= Adding a helper =
 +
 +
A helper in the context of a Joomla! extension is usually just a class providing various static helper functions to perform operations. A helper could be used to accomplish a lot of things but common examples are to get a list of items from a database, get the number of currently logged in users, etc... To illustrate how this works, we are going to implement a helper to the module which simple provides the module with yet another string.
  
 
With your favorite editor, create the following file
 
With your favorite editor, create the following file
 +
 
<span id="helper.php">
 
<span id="helper.php">
 
'''<tt>helper.php</tt>'''
 
'''<tt>helper.php</tt>'''
Line 25: Line 28:
 
</source>
 
</source>
 
</span>
 
</span>
 +
 +
As you can see, this is nothing more than a class with a static function in it. The class will need to be loaded in the module entry point.
  
 
With your favorite editor, edit the following file
 
With your favorite editor, edit the following file
Line 30: Line 35:
 
<span id="mod_helloworld.php">
 
<span id="mod_helloworld.php">
 
'''<tt>mod_helloworld.php</tt>'''
 
'''<tt>mod_helloworld.php</tt>'''
<source lang="php">
+
<source lang="php" highlight="13">
 
<?php  
 
<?php  
  
Line 48: Line 53:
 
</source>
 
</source>
 
</span>
 
</span>
 +
 +
The highlighted line takes care of loading the class. We could have used the '''require()''' function call to do this, but JLoader offers additional advantages such as lazy loading (The class is not actually loaded unless it is needed). The first argument is the name of the class, and the second argument is the path to the file. '''JPATH_BASE''' is a macro which always points to the root of the Joomla! application, in this case the frontend.
 +
 +
<span id="tmpl/default.php">
 +
'''<tt>tmpl/default.php</tt>'''
 +
<source lang="php" highlight="15">
 +
<?php
 +
 +
/**
 +
* @package Joomla.Tutorials
 +
* @subpackage Module
 +
* @copyright Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
 +
* @license License GNU General Public License version 2 or later; see LICENSE.txt
 +
*/
 +
 +
// No direct access to this file
 +
defined('_JEXEC') or die;
 +
?>
 +
 +
<p><?php echo $params->get('greeting', JText::_('MOD_HELLOWORLD_GREETING_DEFAULT')); ?></p>
 +
<p><?php echo modHelloWorldHelper::getGreeting(); ?></p>
 +
</source>
 +
</span>
 +
 +
Here we modify the module template to call the helper function '''getGreeting()''' in our helper class and render the string it returns in a new paragraph in addition to the original greeting.
  
 
With your favorite editor, edit the following file
 
With your favorite editor, edit the following file
Line 53: Line 83:
 
<span id="mod_helloworld.xml">
 
<span id="mod_helloworld.xml">
 
'''<tt>mod_helloworld.xml</tt>'''
 
'''<tt>mod_helloworld.xml</tt>'''
<source lang="xml">
+
<source lang="xml" highlight="22">
 
<?xml version="1.0" encoding="utf-8"?>
 
<?xml version="1.0" encoding="utf-8"?>
 
<extension type="module" version="2.5.0" method="upgrade">
 
<extension type="module" version="2.5.0" method="upgrade">
Line 97: Line 127:
 
</source>
 
</source>
 
</span>
 
</span>
 +
 +
Here we simply add '''helper.php''' to the list of files to be copied when the extension is installed.
  
 
== File listing ==  
 
== File listing ==  
Line 103: Line 135:
 
* <tt>[[#mod_helloworld.php|mod_helloworld.php]]</tt>
 
* <tt>[[#mod_helloworld.php|mod_helloworld.php]]</tt>
 
* <tt>[[#tmpl/default.php|tmpl/default.php]]</tt>
 
* <tt>[[#tmpl/default.php|tmpl/default.php]]</tt>
 +
* <tt>[[User:Rvsjoen/tutorial/Developing_a_Module/Part_04#language/en-GB.mod_helloworld.ini|language/en-GB.mod_helloworld.ini]]</tt>
 +
* <tt>[[User:Rvsjoen/tutorial/Developing_a_Module/Part_04#language/en-GB.mod_helloworld.sys.ini|language/en-GB.mod_helloworld.sys.ini]]</tt>
 +
* <tt>[[#helper.php|helper.php]]</tt>
 +
 +
== Testing your module ==
 +
 +
For details on how to install the module into your Joomla! site, refer to the information provided in
 +
[[User:Rvsjoen/tutorial/Developing_a_Module/Part_01#Installation_and_Testing|Part 01]].
  
 
== Download this part ==
 
== Download this part ==
  
To be continued...
+
[https://github.com/downloads/rvsjoen/joomla-tutorials/mod_helloworld-part05.zip Download example package]
  
 
{{:Chunks:Developing_a_Module_Contents}}
 
{{:Chunks:Developing_a_Module_Contents}}

Latest revision as of 17:47, 13 March 2012

Adding a helper[edit]

A helper in the context of a Joomla! extension is usually just a class providing various static helper functions to perform operations. A helper could be used to accomplish a lot of things but common examples are to get a list of items from a database, get the number of currently logged in users, etc... To illustrate how this works, we are going to implement a helper to the module which simple provides the module with yet another string.

With your favorite editor, create the following file

helper.php

<?php 

/**
 * @package	Joomla.Tutorials
 * @subpackage	Module
 * @copyright	Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
 * @license	License GNU General Public License version 2 or later; see LICENSE.txt
 */

// No direct access to this file
defined('_JEXEC') or die;

class modHelloWorldHelper
{
	public static function getGreeting() {
		return "Message from the helper";
	}
}

As you can see, this is nothing more than a class with a static function in it. The class will need to be loaded in the module entry point.

With your favorite editor, edit the following file

mod_helloworld.php

<?php 

/**
 * @package	Joomla.Tutorials
 * @subpackage	Module
 * @copyright	Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
 * @license	License GNU General Public License version 2 or later; see LICENSE.txt
 */

// No direct access to this file
defined('_JEXEC') or die;

JLoader::register('modHelloWorldHelper', JPATH_BASE.'/modules/mod_helloworld/helper.php');

require JModuleHelper::getLayoutPath('mod_helloworld', $params->get('layout', 'default'));

The highlighted line takes care of loading the class. We could have used the require() function call to do this, but JLoader offers additional advantages such as lazy loading (The class is not actually loaded unless it is needed). The first argument is the name of the class, and the second argument is the path to the file. JPATH_BASE is a macro which always points to the root of the Joomla! application, in this case the frontend.

tmpl/default.php

<?php 

/**
 * @package	Joomla.Tutorials
 * @subpackage	Module
 * @copyright	Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
 * @license	License GNU General Public License version 2 or later; see LICENSE.txt
 */

// No direct access to this file
defined('_JEXEC') or die;
?>

<p><?php echo $params->get('greeting', JText::_('MOD_HELLOWORLD_GREETING_DEFAULT')); ?></p>
<p><?php echo modHelloWorldHelper::getGreeting(); ?></p>

Here we modify the module template to call the helper function getGreeting() in our helper class and render the string it returns in a new paragraph in addition to the original greeting.

With your favorite editor, edit the following file

mod_helloworld.xml

<?xml version="1.0" encoding="utf-8"?>
<extension type="module" version="2.5.0" method="upgrade">

	<name>MOD_HELLOWORLD</name>
	<!-- The following elements are optional and free of formatting constraints -->
	<creationDate>Once upon a time</creationDate>
	<author>John Doe</author>
	<authorEmail>john.doe@example.org</authorEmail>
	<authorUrl>http://www.example.org</authorUrl>
	<copyright>Copyright Info</copyright>
	<license>License Info</license>
	<!--  The version string is stored in the extension table -->
	<version>0.0.5</version>
	<!-- The description is optional and defaults to the name -->
	<description>MOD_HELLOWORLD_XML_DESCRIPTION</description>

	<!-- Note the folder attribute: This attribute describes what to copy
		into the module folder -->
	<files>
		<filename module="mod_helloworld">mod_helloworld.php</filename>
		<filename>mod_helloworld.xml</filename>
		<filename>helper.php</filename>
		<folder>tmpl</folder>
		<folder>language</folder>
	</files>

	<config>
		<fields name="params">
			<fieldset name="basic">
				<field
					name="greeting"
					type="text"
					default="MOD_HELLOWORLD_GREETING_DEFAULT"
					label="MOD_HELLOWORLD_GREETING_LABEL"
					description="MOD_HELLOWORLD_GREETING_DESC"
				/>
			</fieldset>
		</fields>
	</config>

</extension>

Here we simply add helper.php to the list of files to be copied when the extension is installed.

File listing[edit]

Testing your module[edit]

For details on how to install the module into your Joomla! site, refer to the information provided in Part 01.

Download this part[edit]

Download example package

Articles in this series[edit]

This tutorial is supported by the following versions of Joomla!

Joomla 2.5