How to extend a custom Joomla Module


How to extend an own or custom Joomla module in order that it contains a Joomla Article.
Motivation: Joomla Modules (e.g. the sidebar) cannot be modified by authors who have the rights of "Editor" or "Publisher". Nevertheless the manager of the CMS wants to allow an "Editor" or "Publisher" to modify the content displayed at a module's position.
Therefore the contents of a regular article should be displayed at a module's position. If you're interested, follow this instruction ...
How to extend an own or custom Joomla module in order that it contains PHP-generated content.
Read more ...

Please note: the descriptions are currently based on Joomla 3.10.x using the template "t3_bs3_blank"

How to extend a custom Joomla Module in order that it contains a Joomla Article

As an example of the description, I refer to the sidebar of the website joomla.cvjm-wuertingen.de.

  • Create a custom ModuleStyle

    Create or extend the following file: WWWROOT/joomla/htdocs/templates/t3_bs3_blank/html/modules.php:
    <?php
    /**
     * @package     Joomla.Site
     * @subpackage  Template.system
     *
     * @copyright   (C) 2006 Open Source Matters, Inc. 
     * @license     GNU General Public License version 2 or later; see LICENSE.txt
     */
    
    defined('_JEXEC') or die;
    
    
    function modChrome_includePHP($module, &$params, &$attribs)
    {
        $includeFile = JPATH_SITE . '/media/php-includes/' . $module->position . '.php';
        if (!is_file($includeFile)) {
            echo 'File not found: ' . $includeFile ;
            return;
            }
        if ((bool) $module->showtitle){
            $headerTag = htmlspecialchars($params->get('header_tag', 'h3'));
            $headerClass = $params->get('header_class');
            $headerClass = !empty($headerClass) ? ' class="' . htmlspecialchars($headerClass) . '"' : '';
            echo '<' . $headerTag . $headerClass .'>' . $module->title .'';
            }
        require_once($includeFile);
    }
    
    function modChrome_includeAktuell($module, &$params, &$attribs)
    {
        $includeFile = JPATH_SITE . '/media/php-includes/' . 'aktuell.php';
        if (!is_file($includeFile)) {
            echo 'File not found: ' . $includeFile ;
            return;
            }
        if ((bool) $module->showtitle){
            $headerTag = htmlspecialchars($params->get('header_tag', 'h3'));
            $headerClass = $params->get('header_class');
            $headerClass = !empty($headerClass) ? ' class="' . htmlspecialchars($headerClass) . '"' : '';
            echo '<' . $headerTag . $headerClass .'>' . $module->title .'';
            }
        require_once($includeFile);
    }
    
    ?>
        			
  • Create a custom Module

    Refer to Joomla documentation How do you create a custom module? Tab "Module": Assign position "sidebar-1" (or any valid other position) to the module.
    Tab "Extensions": Assign the module style with the previously defined name of the module style, here "includeAktuell" Screenshot of module style editor
  • Create a Joomla Article

    ... in the normal way and remember the article-id (here 17). Assign the article to the category "sidebar" (here category-id 17) or even "unassigned" (often category-id 1), but in every case in a way it will NOT be displayed.
  • Create the PHP content

    Create and edit the following file: WWWROOT/joomla/htdocs/media/php-includes/aktuell.php, which is referred in WWWROOT/joomla/htdocs/templates/t3_bs3_blank/html/modules.php:
    <?php
    $page  = 'https://joomla.cvjm-wuertingen.de/index.php?option=com_content&view=article&id=17&catid=17&tmpl=component';
    $input = file_get_contents($page);
    $pos   = strpos($input, '<section class="article-content clearfix" itemprop="articleBody">');
    $pos2  = strpos($input, '</section>') - $pos;
    echo substr($input, $pos, $pos2);
    ?>
        			
    The script extracts the pure content of the article and removes all overhead.
Now, the pure contents of the article will be displayed at the position of the module


How to extend a custom Joomla Module in order that it contains PHP-generated content

As an example of the description, I refer to the "Belegungsplan" (bookings plan) of the website https://joomla.cvjm-wuertingen.de/index.php/freizeitheim, which is the 2nd article

  • Create a custom ModuleStyle

    Create or extend the following file: WWWROOT/joomla/htdocs/templates/t3_bs3_blank/html/modules.php:
    <?php
    /**
     * @package     Joomla.Site
     * @subpackage  Template.system
     *
     * @copyright   (C) 2006 Open Source Matters, Inc. 
     * @license     GNU General Public License version 2 or later; see LICENSE.txt
     */
    
    defined('_JEXEC') or die;
    
    
    function modChrome_includePHP($module, &$params, &$attribs)
    {
        $includeFile = JPATH_SITE . '/media/php-includes/' . $module->position . '.php';
        if (!is_file($includeFile)) {
            echo 'File not found: ' . $includeFile ;
            return;
            }
        if ((bool) $module->showtitle){
            $headerTag = htmlspecialchars($params->get('header_tag', 'h3'));
            $headerClass = $params->get('header_class');
            $headerClass = !empty($headerClass) ? ' class="' . htmlspecialchars($headerClass) . '"' : '';
            echo '<' . $headerTag . $headerClass .'>' . $module->title .'';
            }
        require_once($includeFile);
    }
    
    function modChrome_includeBelegungsplan($module, &$params, &$attribs)
    {
        $includeFile = JPATH_SITE . '/media/php-includes/' . 'belegungsplan.php';
        if (!is_file($includeFile)) {
            echo 'File not found: ' . $includeFile ;
            return;
            }
        if ((bool) $module->showtitle){
            $headerTag = htmlspecialchars($params->get('header_tag', 'h3'));
            $headerClass = $params->get('header_class');
            $headerClass = !empty($headerClass) ? ' class="' . htmlspecialchars($headerClass) . '"' : '';
            echo '<' . $headerTag . $headerClass .'>' . $module->title .'';
            }
        require_once($includeFile);
    }
    ?>
        			
  • Create a custom Module

    Refer to Joomla documentation How do you create a custom module? Tab "Module": Leave the position of the module empty.
    Tab "Extensions": Assign the module style with the previously defined name of the module style, here "includeBelegungsplan" Screenshot of module style editor
    Remember the module id, here 102: Screenshort of modules
  • Create the PHP content

    Create and edit the following file: WWWROOT/joomla/htdocs/media/php-includes/belegungsplan.php, which is referred in WWWROOT/joomla/htdocs/templates/t3_bs3_blank/html/modules.php:
    <?php
    defined('_JEXEC') or die;
    
    session_start();
    extract ($_REQUEST);
    
    $content = '';
    
    // Create your PHP content here ...
    
    echo $content;
    
    ?>
        			
  • Add PHP-generated content to a Joomla Article

    Create a Joomla Article with the following content by adding a module with the module id of your custom module, created in step 2.
    {loadedmoduleid 102}
    				

    Screenshot of adding module
Now, the PHP-generated content will be visible as content of the Joomla Article!