developing a joomla 3.x module - joomladay south africa 2013

Download Developing a Joomla 3.x Module - Joomladay South Africa 2013

If you can't read please download the document

Upload: peter-martin

Post on 02-Jan-2016

345 views

Category:

Documents


2 download

DESCRIPTION

You can extent the functionality of a Joomla website with different types of extensions: Components, Modules, Plugins, Templates, Language files. And with a little PHP & MySQL knowledge, you can create them yourself. In this (English) session Peter will show you how to program a module for your Joomla 3.x website, including parameters, language file, alternative layout, installation package and one-click-update functionality.

TRANSCRIPT

PowerPoint Presentation

Developing
a Joomla 3.1 Module

Peter Martin, twitter: @pe7er
www.joomladay.org.za, Sat Oct 19th 2013

Overview Presentation

>>> Sheets via: www.db8.nl Template Manager > Preview Module Positions: enabled

URL + ?tp=1

Code for Module Position in Template: /templates/template_name/index.php



2. Module basics

2. Module basics

Location:Front-end: /modules/mod_name

Back-end: /administrator/modules/mod_name

Files:mod_name.php code

mod_name.xml installation & parameters

Database reference #__extensionsname=Module Name

type=module

element=mod_name

folder={emply}

client_id: 0 = front-end, 1 = back-end

2. Module basics: example

Joomla's Weblinks Component com_weblinksWeblinks in a Weblink category

Counts clicks

Database tabel: #__weblinksid, catid, title, url, description, created, hits, state, catid

Joomla's Weblinks Modulecreated date *not* used

2. Module Latest Weblinks

Develop Module:
Display most recent added weblinksFolder: /modules/mod_db8latestweblinks/

With files:mod_db8latestweblinks.php

Extension Manager > Discover

Extensions > Module Manager > [New]

2. Module Latest Weblinks

Manifest file: mod_db8latestweblinks.xml

db8 Latest Weblinks
Peter Martin
September 2013
(C) 2013 Peter Martin. All rights reserved.
GNU General Public License version 2 or later;
[email protected]
www.db8.nl
2.0
MOD_DB8LATESTWEBLINKS_XML_DESCRIPTION

mod_db8latestweblinks.php
mod_db8latestweblinks.xml

2. Module Latest Weblinks

3. Module Database

3. Module Database

Joomla Database Object

Call Joomla's Database Object

Instantiate an empty Query Object

Build Query Object

do NOT use hardcoded MySQL!
SELECT * FROM #__weblinks ORDER BY created DESC

Feed Query Object to Database Object

Retrieve Results from Database Object

3. Module Database

mod_db8latestweblinks.phpdefined('_JEXEC') or die;

$db = Jfactory::getDbo();
$query = $db->getQuery(true);

$query->select('*')
->from('#__weblinks')
->order('created DESC');

$db->setQuery($query, 0, 7);
$items = $db->loadObjectList();

?>





3. Module Database

Debug:echo "sql = $query";

print_r($items);

HTMLGoogle Chrome Inspect Element

FireFox + FireBug addon

4. Module OOP style

4. Module OOP style

Object Oriented Programming Separation between logic & screen output

MVC (Model/View/Controller) architecture:Model describes data


View determines screen output

Controller controls what happensCRUD Create Read Update Delete

4. Module OOP style

A Module's Model Controller View:Model
helper.php file
retrieve records from database

View
/tmpl/name.php HTML output
for template overrides!

Controller
the module itself (1 mode: display)

4. Module OOP style: Model

/modules/mod_db8latestweblinks/
helper.phpdefined('_JEXEC') or die;
class modDb8latestweblinksHelper
{
public static function getItems($params)
{
$db = Jfactory::getDbo();
$query = $db->getQuery(true);
$query->select('*')
->from('#__weblinks')
->order('created DESC');
$db->setQuery($query, 0, 7);
$items = $db->loadObjectList();
return $items;
}
}

4. Module OOP style: View

/modules/mod_db8latestweblinks/tmpl/
default.php

defined('_JEXEC') or die;
?>






4. Module OOP style: Controller

/modules/mod_db8latestweblinks/
mod_db8latestweblinks.php
defined('_JEXEC') or die;

require_once dirname(__FILE__).'/helper.php';
$items = modDb8latestweblinksHelper::getItems($params);

require JmoduleHelper::getLayoutPath(
'mod_db8latestweblinks', $params->get('layout', 'default'));

4. Module OOP style

Exactly the same output!Template override possible

5. Parameters

5. Parameters

FlexibilityCategories

Limit results

Target Window

Follow/No Follow

Display: Date, Description (hover), Hits

Count clicks / or not

Display: Published / unpublished

.xml define choices

Helper.php use parameters in SQL query

5. Parameters Choices

/modules/mod_db8latestweblinks/
mod_db8latestweblinks.xml









5. Parameters Choices

/modules/mod_db8latestweblinks/
mod_db8latestweblinks.xml



5. Parameters

/modules/mod_db8latestweblinks/
helper.php
public static function getItems($params){
$db = Jfactory::getDbo();
$query = $db->getQuery(true);
$query->select('*')
->from('#__weblinks')
->order('created DESC');
$catid = $params->get('catid');
if(is_array($catid)){
if($catid[0] != null){
$catid = implode(',', $catid);
$query->where('catid IN ('.$catid.')');
}
}
$db->setQuery($query, 0, $params->get('count',5));
$rows = $db->loadObjectList();
return $rows;
}

5. Parameters Choices


6. Language File

6. Language File

FlexibilityRegarding distribution & Language Override !

Language labels in .xml, and JText in .php

Location Front-end:In language files folder: /language/en-GB/en-GB.mod_name.ini

/language/en-GB/en-GB.mod_name.sys.ini

Or in Module, e.g.: /modules/mod_db8latestweblinks/language/en-GB/
en-GB.mod_db8latestweblinks.ini

/modules/mod_db8latestweblinks/language/en-GB/
en-GB.mod_db8latestweblinks.sys.ini

6. Language File

/modules/mod_db8latestweblinks/
mod_db8latestweblinks.xml


6. Language File

6. Language File

/language/en-GB/en-GB.mod_db8latestweblinks.iniMOD_DB8LATESTWEBLINKS_FIELD_CATEGORY_DESC="Choose the weblinks category to display"
MOD_DB8LATESTWEBLINKS_FIELD_COUNT_DESC="Number of weblinks to display"
MOD_DB8LATESTWEBLINKS_FIELD_COUNT_LABEL="Count"

/language/en-GB/en-GB.mod_db8latestweblinks.sys.iniMOD_DB8LATESTWEBLINKS="Weblinks Latest"
MOD_DB8LATESTWEBLINKS_XML_DESCRIPTION="This modules displays the latest weblinks from a category defined in the Weblinks component."

/language/de-DE/de-DE.mod_db8latestweblinks.iniMOD_DB8LATESTWEBLINKS="Weblinks Neueste"
MOD_DB8LATESTWEBLINKS_XML_DESCRIPTION="Dieses Modul zeigt die allerneuesten Weblinks aus einer Kategorie in der Weblinks-Komponente definiert."
MOD_DB8LATESTWEBLINKS_FIELD_CATEGORY_DESC="Whlen Sie die Kategorie Web-Links angezeigt werden"
MOD_DB8LATESTWEBLINKS_FIELD_COUNT_LABEL="Anzahl"
MOD_DB8LATESTWEBLINKS_FIELD_COUNT_DESC="Anzahl an angezeigten Weblinks"

/language/de-DE/de-DE.mod_db8latestweblinks.sys.iniMOD_DB8LATESTWEBLINKS="Weblinks Neueste"
MOD_DB8LATESTWEBLINKS_XML_DESCRIPTION="Dieses Modul zeigt die allerneuesten Weblinks aus einer Kategorie in der Weblinks-Komponente definiert."

6. Language File

7. Views

7. Views

View = HTML Output/modules/mod_db8latestweblinks/tmpl/default.php

Change HTML output? Template override!/templates/your_template/html/
mod_db8latestweblinks/default.php

Create choices for admins?Layout choice parameter
/modules/mod_db8latestweblinks/
mod_db8latestweblinks.xml

Alternatieve view
/modules/mod_db8latestweblinks/tmpl/
alternative_view.php

7. Views Layout choice parameter

/modules/mod_db8latestweblinks/
mod_db8latestweblinks.xml

7. Views Alternative layout

/modules/mod_db8latestweblinks/tmpl/
withdate.php
defined('_JEXEC') or die;
?>





8. Parameters Advanced

8. Parameters Advanced

Advanced OptionsAlternative Layout
(see 7. Views)

Module Class Suffix

Caching

Cache Time

8. Parameters Advanced




JGLOBAL_USE_GLOBAL
COM_MODULES_FIELD_VALUE_NOCACHING





9. Packaging (for distribution)

9. Packaging (for distribution)

Installable package: mod_name_j3x_v2.zip

Contents:mod_db8latestweblinks.xml

mod_db8latestweblinks.php

helper.php

Language file folder + files:/language/en-GB/en-GB.mod_db8latestweblinks.ini

/language/en-GB/en-GB.mod_db8latestweblinks.sys.ini

/language/de-DE/de-DE.mod_db8latestweblinks.ini

/language/de-DE/de-DE.mod_db8latestweblinks.sys.ini

HTML output folder + files:/tmpl/default.php

/tmpl/with-date.php

9. Packaging (for distribution)

Installation xml manifest file: mod_db8latestweblinks.xml



mod_db8latestweblinks.php
mod_db8latestweblinks.xml
helper.php
tmpl
language

TEST!At other webserver / website

10. Update

10. Update

In your Module:Manifest .xml file with

On your distribution server:.xml file with Module version information

.zip archive to download

10. Update .xml manifest file

/modules/mod_db8latestweblinks/
mod_db8latestweblinks.xml


http://www.db8.nl/updates/ mod_db8latestweblinks.xml

Do not use spaces or line breaks between the tags!

10. Update .xml server file

Location as specified in .xml manifest file:
http://www.db8.nl/update/mod_db8latestweblinks.xml




db8 Latest Weblinks
mod_db8latestweblinks
module
0
2.1

http://www.db8.nl/some-url/


[see next page]

10. Update .xml server file



http://www.db8.nl/updates/
mod_db8latestweblinks_j3x_v21.zip



Peter Martin
http://www.db8.nl
db8



10. Update Database Reference

Update mechanism:
Extensions > Extension Manager > [Update][Find Updates] creates reference in #__updates

Nothing found?Check URL .xml file in browser

Reinstall Module

The

Questions?

Questions?

Presentation available at www.db8.nl

Peter Martine-mail: info at db8.nlwebsite: www.db8.nl

Used Photos

Speed Typing - Matthew Bowden http://www.sxc.hu/photo/275499

Motherboard 4 - Lisa Zanchi http://www.sxc.hu/photo/103914

ram Mohamed Riffath http://www.sxc.hu/photo/487296

lots of files 2 - Michael & Christa Richert http://www.sxc.hu/photo/1370555

Blank E-Box for Software etc 6 - Przemyslaw Szczepanski, http://www.sxc.hu/photo/950048

switches - "trhaynes", http://www.sxc.hu/photo/56670

Bengali Keyborad - Mohammad Jobaed Adnan http://www.sxc.hu/photo/676844

san sebastian views 1 - ibon san martin http://www.sxc.hu/photo/94018

Communications Receiver - John Munsch http://www.sxc.hu/photo/260775

Fragile Parcel - Richard Dudley http://www.sxc.hu/photo/1279274

Sparks - Hector Landaeta http://www.sxc.hu/photo/1184243

signs signs - Jason Antony, http://www.sxc.hu/photo/751034

Face - Questions - Bob Smith, http://www.sxc.hu/photo/418215

Click to edit the title text format

Click to edit the outline text formatSecond Outline LevelThird Outline LevelFourth Outline LevelFifth Outline LevelSixth Outline LevelSeventh Outline LevelEighth Outline LevelNinth Outline Level

Peter Martin joomladagen.nl 20+21 april 2013

Joomladay 2013 South Africa