Building a Joomla Module

  • Published on
    21-Jan-2018

  • View
    610

  • Download
    0

Transcript

  1. 1. Building a Joomla Module Cory WebbJoomla Day Chicago 2017CoryWebbMedia.comCWExtensions.com@corywebb
  2. 2. 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)
  3. 3. Agenda Build a module to display a Joomla article Hello World Database query Display the article Add a helper le Add a template layout
  4. 4. Get the Module https://cwextensions.com/jday-chicago
  5. 5. Build the Module Create folder named mod_article_display Add 2 les: mod_article_display.php mod_article_display.xml Add language les: en-GB.mod_article_display.ini en-GB.mod_article_display.sys.ini Discover install
  6. 6. Hello World Display Hello World in the module output Edit mod_article_display.php dened(_JEXEC) or die; echo Hello world!; Publish the module
  7. 7. 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();
  8. 8. 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 article echo link . > . $article->title . ; echo
    . $article->introtext .
    ; echo

    link . class=read-more>Read More

    ;
  9. 9. Add a Helper File helper.php class ModArticleDisplayHelper {} Load the helper class JLoader::register('ModArticleDisplayHelper', __DIR__ . /helper.php'); Move logic to helper $article = ModArticleDisplayHelper::getArticle($params);
  10. 10. Add a Template Layout tmpl/default.php Load the template require JModuleHelper::getLayoutPath('mod_article_display', $params->get('layout', default'));
  11. 11. Questions/Comments