ADVERTISEMENT


Breaking News

Recent Posts

Tuesday, June 24, 2014

How to add Variable Request in Joomla Module

In this Joomla Module Tutorial you will find how to add variable request in module. Adding a variable request to a module is simple which is done by installation file .xml. We will place some code in the xml file to add variable request in the module.

Creating Variable Request

In the .xml file we will add <config> tag to add variable request like shown below.
  1. <config>
  2. <fields name="params">
  3. <fieldset name="basic">
  4. <field
  5. name="width"
  6. type="text"
  7. default="185"
  8. label="Image Width"
  9. description="Image Width in px" />
  10. <field
  11. name="height"
  12. type="text"
  13. default="300"
  14. label="Image Height"
  15. description="Image Height in px" />
  16. </fieldset>
  17. </fields>
  18. </config>
<fields> tag contains the name attribute which will be used in the php file to access the variable.
<fieldset> tag groups the variable request under ‘name’ attribute.
<field> tag contains the variable request with various attribute such as name of the variable,type of the variable, default value of the variable, label shown in the module and description shown on mouse hover. Here we have created two variable request with name width and height.

Accessing Variable Request

A variable defined above have to be accessed in the php file.It is done by the get() function.
  1. $width = $params->get('width');
  2. $height = $params->get('height');
where,
$params is the <fields> tag name defined in .xml file.

Files Required

1) mod_varreq.php: This file is the entry point for the module. It will perform necessary initializations and call helper routine to collect necessary data and include the template which will display module output.
2) helper.php: This file contains the helper class which will collect necessary data to be used in the module from database or any other sources.
3) mod_varreq.xml: This file contains the information about the module. This is the installation file for the module.
4) tmpl/default.php: This is the file used for displaying the module output.

1) Creating file mod_varreq.php

The Complete code of mod_varreq.php is
  1. <?php
  2. // no direct access
  3. defined( '_JEXEC' ) or die( 'Restricted access' );
  4. // Include the syndicate functions only once
  5. require_once( dirname(__FILE__).DS.'helper.php' );
  6. $hello = modVarreqHelper::getHello( $params );
  7. $width = $params->get('width');
  8. $height = $params->get('height');
  9. require( JModuleHelper::getLayoutPath( 'mod_varreq' ) );
  10. ?>

2) Creating file helper.php

This file contains the class as defined in mod_ varreq.php ,here it is modVarreqHelper class and contains function getHello().
The complete code of helper.php is
  1. <?php
  2. // no direct access
  3. defined( '_JEXEC' ) or die( 'Restricted access' );
  4. class modVarreqHelper
  5. {
  6. static function getHello($params)
  7. {
  8. return 'Helper Hello World';
  9. }
  10. }
  11. ?>

3) Creating installation file mod_varreq.xml

This file contains the information about the module.
The complete code of mod_varreq.xml is
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <extension type="module" version="2.5" client="site" method="upgrade">
  3. <name>VAriable Request</name>
  4. <author>Larenge Kamal</author>
  5. <version>1.7</version>
  6. <description>A simple Variable request module.</description>
  7. <files>
  8. <filename>mod_varreq.xml</filename>
  9. <filename module="mod_varreq">mod_varreq.php</filename>
  10. <filename>index.html</filename>
  11. <filename>helper.php</filename>
  12. <filename>tmpl/default.php</filename>
  13. <filename>tmpl/index.html</filename>
  14. </files>
  15. <config>
  16. <fields name="params">
  17. <fieldset name="basic">
  18. <field
  19. name="width"
  20. type="text"
  21. default="185"
  22. label="Image Width"
  23. description="Image Width in px" />
  24. <field
  25. name="height"
  26. type="text"
  27. default="300"
  28. label="Image Height"
  29. description="Image Height in px" />
  30. </fieldset>
  31. </fields>
  32. </config>
  33. </extension>

4) Creating file tmpl\default.php

This file contains the output to be displayed by the module. This file has the same scope as that of the mod_varreq.php. So the variables defined in mod_varreq.php can be directly accessed in this file. ‘$hello’ variable defined in mod_varreq.php can be directly accessed here.
The complete code of tmpl\default.php is
  1. <?php
  2. // no direct access
  3. defined( '_JEXEC' ) or die( 'Restricted access' );
  4. ?>
  5. <html>
  6. <body>
  7. <?php echo $hello; ?>
  8. <br>
  9. width = <?php echo $width; ?>
  10. <br>
  11. height = <?php echo $height; ?>
  12. </body>
  13. </html>

5) Creating file index.html common to all

The complete code of index.html is
View source
  1. <html><body bgcolor="#FFFFFF"></body></html>
which will display an empty page.
Now create the zip file of the folder ‘mod_varreq’ which contains the following files.
1) mod_varreq.php
2) index.html
3) mod_varreq.xml
4) helper.php
5) tmpl\default.php
6) tmpl\index.html
The above zip file can now be installed using Joomla extension manager.
After installing the module,’ VAriable Request’ module will appear in the module manager.Variable request will also appear on the right side when this new module is opened in the joomla administrator.

Enable this new module and set the position for display of this module to see the output of the module.


No comments:

Post a Comment

Designed By Published.. Blogger Templates