Sie sind auf Seite 1von 28

Programming Joomla 1.

5 Modules Hour 2: Create a simple Module


by Peter Martin Nickname: pe7er Twitter: @pe7er Website: www.db8.nl

13 August 2011

Hour 2 Write a Module part 1


H2.1 Development Tools H2.2 Simple non-OOP Modules H2.3 Module Positions H2.4 Parameters Assignments: a. Create a Simple non-OOP Module b. Module Positions c. Use of Parameters

2.1 Development Tools 1/2


Local

web environment

OS: Windows / MAC OSX / Linux LAMP stack (or XAMPP) Xdebug (PHP module) php.ini: error_reporting = E_ALL & E_NOTICE

Joomla
3

(Latest Stable Version)

Example data + default Rhuk Milkyway template 2nd language installed (nb-NO or nl-NL) J!Dump

2.1 Development Tools 2/2


PHP

Editor with code highlighting

Netbeans Eclipse PDT

phpMyAdmin FireFox
4

Firebug Webdeveloper toolbar MeasureIT ColorZilla

2.2 Simple non-OOP Modules 1/2


File

Locations

/administrator/modules/mod_example Front-end: /modules/mod_example Example: db8 Latest Weblinks => mod_db8latestweblinks


Back-end:

File
5

names

.PHP (logic) .XML (installation & parameters) .INI (language files, in /languages/ )

2.2 Simple non-OOP Modules 2/2


Reference

in jos_modules

Manual reference INSERT INTO `jos_modules` VALUES (0, 'db8 Latest Weblinks', '', 0, 'left', 0, '0000-00-00 00:00:00', 1, 'mod_db8latestweblinks', 0, 0, 1, '', 0, 0, ''); Automatic reference
Install

in Joomla back-end with XML installation file (Sandbox installer) mod_db8latestweblinks.zip


mod_db8latestweblinks.xml mod_db8latestweblinks.php

Assignment 2a
Joomla's weblinks component shows weblinks from category + register clicks. My client wants a module that shows the latest added weblinks on their site. Create Module for Joomla 1.5

Show the title + link + mouse-over description for the latest 3 weblinks
TIP Database table: jos_weblinks title, URL, description, date (not used by component :-), hits, catid.

Assignment 2a possible solution


mod_db8latestweblinks.php <?php defined('_JEXEC') or die('Restricted access'); $db =& JFactory::getDBO(); $query = 'SELECT title, url, description' . ' FROM #__weblinks' . ' ORDER BY date DESC'; $db->setQuery($query, 0, 3); $rows = $db->loadObjectList(); foreach ($rows as $item) : ?> <a href="<?php echo $item->url; ?>" target="_blank" title="<?php echo $item->description; ?>"> <?php echo $item->title; ?></a><br/> <?php endforeach; ?>

Assignment 2a possible solution


mod_db8latestweblinks.xml
<?xml version="1.0" encoding="utf-8"?> <install type="module" version="2.1" client="site"> <name>db8 Latest Weblinks</name> <author>Peter Martin (pe7er)</author> <authorEmail>joomla@db8.nl</authorEmail> <authorUrl>www.db8.nl</authorUrl> <creationDate>June 2009</creationDate> <copyright>Copyright 2009 by Peter Martin / db8.nl.</copyright> <license>http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL</license> <version>2.3</version> <description>This module shows the latest weblinks.</description> <files> <filename module="mod_db8latestweblinks">mod_db8latestweblinks.php</filename> </files>

</install>

Assignment 2a possible solution


Module will work without XML file, But you'll need a reference in jos_modules. 1. Manually:

INSERT INTO `jos_modules` VALUES (0, 'db8 Latest Weblinks', '', 0, 'left', 0, '0000-00-00 00:00:00', 1, 'mod_db8latestweblinks', 0, 0, 1, '', 0, 0, '');

2. Sandbox installer: Put .XML & .PHP both in zip file, and install via Joomla's Installer in back-end.

10

2.3 Module Positions


Defined

positions positions

show: www.example.com/index.php?tp=1 <?php if($this->countModules('left')) : ?> <jdoc:include type="modules" name="left" style="xhtml" /> <?php endif; ?>

Defining

11

Assignment 2b
For some incomprehensible reason, my customer wants to replace their logo with the 3 latest added weblinks. Apparently they have gone mad. The safest thing now is to comply with their request until the companies CEO has returned from holiday. Create a new Module Position in Joomla's default template Rhuk Milkyway at the postion where the logo is located. Display your Module in that Module Position

12

Assignment 2b possible solution

In template Rhuk Milkyway /templates/rhuk_milkyway/index.php Add: <jdoc:include type="modules" name="weblinks" style="xhtml" /> somewhere after <div id="header"> or <div id="header_l"> or <div id="header_r">

13

2.4 Module Parameters 1/3


How does Joomla... determine parameters?

.XML file define all parameters

keep

parameters?

jos_modules.params stores parameters

use

parameters?

.php file retrieve & use

14

Assignment 2c
My customer phoned me the 4th time this week to ask me to change the number of latest added weblinks. This cannot continue and we need to find a way they can determine the ammount themselves. Edit your module, add a parameter to display n weblinks To prevent any other disturbances, add a weblink category too
15

Assignment 2c possible solution


mod_db8latestweblinks.xml Number of weblinks & category ID
<params> <param name="count" type="text" default="5" label="Number of weblinks" description="The number of weblinks to display (default is 5)" /> <param name="catid" type="text" default="" label="Category ID" description="The category ID of the weblinks to display (default is 0 = all categories)" /> </params>

16

Assignment 2c possible solution


mod_db8latestweblinks.php
defined('_JEXEC') or die('Restricted access'); $count = intval( $params->get( 'count' ) ); $catid = trim( $params->get( 'catid' ) );

17

Assignment 2c possible solution


mod_db8latestweblinks.php $db =& JFactory::getDBO(); $query = 'SELECT title, url, description' . ' FROM #__weblinks' . ' WHERE catid = ' .$catid. ' ORDER BY date DESC'; $db->setQuery($query, 0, $count); $rows = $db->loadObjectList();
18

Assignment 2c possible solution


mod_db8latestweblinks.php foreach ($rows as $item) : ?> <a href="<?php echo $item->url; ?>" target="_blank" title="<?php echo $item->description; ?>"> <?php echo $item->title; ?></a><br/> <?php endforeach; ?>

19

Assignment 2c possible solution

20

Assignment 2c possible solution

21

2.4 Module Parameters 2/3


It is working!... but now your client wants to have the same layout as Main Menu...

22

2.4 Module Parameters 3/3


Default

layout

In template.css (CSS tag module)

Individual

layout

Specific layout in template.css (CSS tag: module_menu) Module Class Suffix in Module parameters (tag: _menu)

23

Assignment 2d
Now you are getting a real headache from this customer. This time they want to restyle the latest added weblinks module. Before our headache increases and before they call for the 7th time, we have to find a more permanent solution for their layout demands. Edit your module, add a parameter for layout (moduleclass_sfx)
24

Assignment 2d possible solution


mod_db8latestweblinks.xml
<param name="moduleclass_sfx" type="text" default="" label="Module Class Suffix" description="PARAMMODULECLASSSUFFIX" /> <param name="@spacer" type="spacer" default="" label="" description="" />

25

Assignment 2d possible solution


mod_db8latestweblinks.php <?php // no direct access defined('_JEXEC') or die('Restricted access'); ?> <ul class="db8latestweblinks<?php echo $params->get('moduleclass_sfx'); ?>">

26

Assignment 2d possible solution


In back-end > Module Manager > Weblinks > parameters: db8 Latest

Module Class Suffix: _menu (= same suffix as the main menu module).

27

Thanks for your attention!

Peter Martin

nick: pe7er E-mail: info at db8.nl website: www.db8.nl Twitter: @pe7er

28

Das könnte Ihnen auch gefallen