11

Click here to load reader

Building a Joomla Module

Embed Size (px)

Citation preview

Page 1: Building a Joomla Module

Building a Joomla Module

Cory WebbJoomla Day Chicago 2017

CoryWebbMedia.comCWExtensions.com

@corywebb

Page 2: Building a Joomla Module

Who is this guy?• Joomla developer since 2003

(Mambo)

• Owner Cory Webb Media & CWExtensions.com

• Course creator for OSTraining.com

• Author of Beginning Joomla Web Site Development (Wrox, 2009)

Page 3: Building a Joomla Module

Agenda• Build a module to display a Joomla article

• Hello World

• Database query

• Display the article

• Add a helper file

• Add a template layout

Page 4: Building a Joomla Module

Get the Modulehttps://cwextensions.com/jday-chicago

Page 5: Building a Joomla Module

Build the Module• Create folder named mod_article_display

• Add 2 files: mod_article_display.phpmod_article_display.xml

• Add language files: en-GB.mod_article_display.inien-GB.mod_article_display.sys.ini

• Discover install

Page 6: Building a Joomla Module

Hello World

• Display “Hello World” in the module output

• Edit mod_article_display.phpdefined(‘_JEXEC’) or die;echo ‘<h3>Hello world!</h3>’;

• Publish the module

Page 7: Building a Joomla Module

Database Query• Get the article ID

$article_id = $params->get(‘article_id’);

• Create the query$db = JFactory::getDbo();$query = $db->getQuery(true);$query->select(‘*’)->from(‘#__content’)->where(‘id = ‘ . $article_id);$db->setQuery($query);

• Load the article$article = $db->loadObject();

Page 8: Building a Joomla Module

Display the article• Get a link to the article with ContentHelperRoute

JLoader::register(‘ContentHelperRoute', JPATH_SITE . '/components/com_content/helpers/route.php');$article->slug = $article->id . ‘:’ . $article->alias;$article->link = JRoute::_( ContentHelperRoute::getArticleRoute( $article->slug, $article->catid, $article->language ));

• Display the articleecho ‘<h3><a href=“’ . $article->link . ‘“>’ . $article->title . ‘</a></h3>’;echo ‘<div class=“article-display-article”>’ . $article->introtext . ‘</div>’;echo ‘<p><a href=“‘ . $article->link . ‘“ class=“read-more”>Read More</a></p>’;

Page 9: Building a Joomla Module

Add a Helper File

• helper.phpclass ModArticleDisplayHelper {…}

• Load the helper classJLoader::register('ModArticleDisplayHelper', __DIR__ . ‘/helper.php');

• Move logic to helper$article = ModArticleDisplayHelper::getArticle($params);

Page 10: Building a Joomla Module

Add a Template Layout

• tmpl/default.php

• Load the templaterequire JModuleHelper::getLayoutPath('mod_article_display', $params->get('layout', ‘default'));

Page 11: Building a Joomla Module

Questions/Comments