Kapitel 28 – Module entwickeln

Alle Modul-Quelltexte aus dem Buch zum einfachen Copy&Paste.

 

28.1.1 XML-Manifest – »mod_backendmodul.xml«

„mod_backendmodul.xml“: Das Attribut „client“ des „“-Tags kennzeichnet das Modul fürs Backend

<?xml version="1.0" encoding="utf-8"?>
<extension version="3.0" type="module" client="administrator" method="upgrade">
    <name>Backendmodul</name>
    <author>Vorname Nachname</author>
    <creationDate>June 2015</creationDate>
    <copyright>Copyright (C) 2015 Vorname Nachnname. All rights reserved.</copyright>
    <license>http://www.gnu.org/licenses/gpl-3.0.html</license>
    <authorEmail>Diese E-Mail-Adresse ist vor Spambots geschützt! Zur Anzeige muss JavaScript eingeschaltet sein!</authorEmail>
    <authorUrl>https://joomla-handbuch.com</authorUrl>
    <version>0.1.0</version>
    <description>Beschreibung</description>
    <files>
        <filename module="mod_backendmodul">mod_backendmodul.php</filename>
        <filename>helper.php</filename>
        <filename>index.html</filename>
        <folder>tmpl</folder>
    </files>
</extension>

28.1.2 Steuerdatei – »mod_backendmodul.php«

„mod_backendmodul.php“: Modul-Controller, der die Daten aus dem Model („helper.php“) holt und den View („default.php“) initialisiert

<?php
defined('_JEXEC') or die;
abstract class mod_backendmodulHelper
{
    public static function getList(&$params)
    {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);

        $query->select($db->quoteName(array('id', 'title', 'introtext', 'fulltext', 'state', 'modified', 'modified_by', 'featured')));
        $query->from($db->quoteName('#__content', 'content'));
        $query->order('modified DESC');
        
        $db->setQuery($query, 0, 10);
        try
        {
            $results = $db->loadObjectList();
        }
        catch (RuntimeException $e)
        {
            JFactory::getApplication()->enqueueMessage($e->getMessage(), 'error');
            return false;
        }

        foreach ($results as $row => $fields)
        {
            $results[$row] = new stdClass;
            $results[$row]->id = $fields->id;
            $results[$row]->title = $fields->title;

            $results[$row]->introtext = strip_tags($fields->introtext, '<br><br/><strong><em><b><i>');
            $results[$row]->fulltext = strip_tags($fields->fulltext, '<br><br/><strong><em><b><i>');

            if (!empty($results[$row]->introtext))
            {
                $results[$row]->summary = strlen($results[$row]->introtext) > 300 ? substr($results[$row]->introtext, 0, 300) . " […]" : $results[$row]->introtext;
            }
            else if  (!empty($results[$row]->fulltext))
            {
                $results[$row]->summary = strlen($results[$row]->fulltext) > 300 ? substr($results[$row]->fulltext, 0, 300) . " […]" : $results[$row]->fulltext;
            }
            else
            {
                $results[$row]->summary = '[No Content]';
            }

            $results[$row]->modified_by = $fields->modified_by;
            $results[$row]->state = $fields->state;
            $results[$row]->modified = $fields->modified;
            $results[$row]->featured = $fields->featured;
        }
        return $results;
    }
}

28.1.3 Ausgelagerte Helferklasse – »helper.php«

„helper.php“: Datenbankabfrage und Datenaufbereitung wie im Model des MVC-Architekturmusters

<?php
defined('_JEXEC') or die;
abstract class mod_backendmodulHelper
{
    public static function getList(&$params)
    {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);

        $query->select($db->quoteName(array('id', 'title', 'introtext', 'fulltext', 'state', 'modified', 'modified_by', 'featured')));
        $query->from($db->quoteName('#__content', 'content'));
        $query->order('modified DESC');
        
        $db->setQuery($query, 0, 10);
        try
        {
            $results = $db->loadObjectList();
        }
        catch (RuntimeException $e)
        {
            JFactory::getApplication()->enqueueMessage($e->getMessage(), 'error');
            return false;
        }

        foreach ($results as $row => $fields)
        {
            $results[$row] = new stdClass;
            $results[$row]->id = $fields->id;
            $results[$row]->title = $fields->title;

            $results[$row]->introtext = strip_tags($fields->introtext, '<br><br/><strong><em><b><i>');
            $results[$row]->fulltext = strip_tags($fields->fulltext, '<br><br/><strong><em><b><i>');

            if (!empty($results[$row]->introtext))
            {
                $results[$row]->summary = strlen($results[$row]->introtext) > 300 ? substr($results[$row]->introtext, 0, 300) . " […]" : $results[$row]->introtext;
            }
            else if  (!empty($results[$row]->fulltext))
            {
                $results[$row]->summary = strlen($results[$row]->fulltext) > 300 ? substr($results[$row]->fulltext, 0, 300) . " […]" : $results[$row]->fulltext;
            }
            else
            {
                $results[$row]->summary = '[No Content]';
            }

            $results[$row]->modified_by = $fields->modified_by;
            $results[$row]->state = $fields->state;
            $results[$row]->modified = $fields->modified;
            $results[$row]->featured = $fields->featured;
        }
        return $results;
    }
}

28.1.4 HTML-Template – »/tmpl/default.php«

„/tmpl/default.php“: iIn Bootstrap-Elemente eingewickelte Ausgabe der Beitragsdetails

state == '1') ? ' icon-publish' : ' icon-unpublish'; ?>
 
featured == '1') ? ' icon-featured' : ' icon-unfeatured'; ?>
   summary, '', '', $item->title); ?>
 
 

28.2.1 XML-Manifest – »mod_backendmodul.xml«

„mod_backendmodul.xml“: Ergänzung des XML-Manifests um eine Modulkonfiguration

<?xml version="1.0" encoding="utf-8"?>
<extension version="3.0" type="module" client="administrator" method="upgrade">
    <name>Backendmodul</name>
    <author>Vorname Nachname</author>
    <creationDate>June 2015</creationDate>
    <copyright>Copyright (C) 2015 Vorname Nachnname. All rights reserved.</copyright>
    <license>http://www.gnu.org/licenses/gpl-3.0.html</license>
    <authorEmail>Diese E-Mail-Adresse ist vor Spambots geschützt! Zur Anzeige muss JavaScript eingeschaltet sein!</authorEmail>
    <authorUrl>https://joomla-handbuch.com</authorUrl>
    <version>0.1.0</version>
    <description>Beschreibung</description>
    </files>
    <config>
        <fields name="params">
            <fieldset name="basic">
                <field name="numberOfArticles"
                    type="text"
                    default="5"
                    label="Number of Articles"
                    description="How many articles are supposed to be listed?" />
                <field name="onlyOwnArticles" type="radio"
                    default="0"
                    class="btn-group"
                    label="Show only own Content"
                    description="If No, content from all authors will be shown">
                <option value="1">JYES</option>
                <option value="0">JNO</option>
                </field>          </fieldset>
        </fields>
    </config>
</extension>

28.2.2 Model-Aktualisierung – »helper.php«

„helper.php“: Erweiterung der Datenbankabfrage, Ergänzung des Beitrags-Bearbeitungslinks und Kürzung des Beitragstexts

<?php
/**
 * @version   0.2.0
 * @author    Vorname Nachname
 * @copyright Copyright (C) 2015 Vorname Nachname
 * @license   http://www.gnu.org/licenses/gpl-3.0.html
 */

defined('_JEXEC') or die;

abstract class mod_backendmodulHelper
{
    public static function getList(&$params)
    {
        // Get Parameters from module configuration
        $numberOfArticles = $params->get('numberOfArticles', 10);
        $onlyOwnArticles = $params->get('onlyOwnArticles', 0);

        // Prepare access to the current user and the database
        $user = JFactory::getuser();
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);

        // Collect all database table fields that we would like to display in the module
        $query->select($db->quoteName(array('content.id', 'title', 'introtext', 'fulltext', 'state', 'modified', 'modified_by', 'featured', 'username')));
        // The basic data is coming from the content table
        $query->from($db->quoteName('#__content', 'content'));
        // Join the user table so we can get the username of the user who recently edited the article
        $query->join('INNER', $db->quoteName('#__users', 'users') . ' ON (' . $db->quoteName('content.modified_by') . ' = ' . $db->quoteName('users.id') . ')');
        if ($onlyOwnArticles == 1)
        {
            $query->where($db->quoteName('modified_by') . ' = ' . $user->id);
        }
        // Display the most recent edits at the top of the list
        $query->order('modified DESC');
        
        // Try to query the database
        $db->setQuery($query, 0, $numberOfArticles);
        try
        {
            $results = $db->loadObjectList();
        }
        catch (RuntimeException $e)
        {
            JFactory::getApplication()->enqueueMessage($e->getMessage(), 'error');
            return false;
        }

        foreach ($results as $row => $fields)
        {
            // Create new class to overwrite the existing database results with added parsing of values
            $results[$row] = new stdClass;
            $results[$row]->id = $fields->id;

            // If the user is authorized to edit this article, link to it
            if ($user->authorise('core.edit', 'com_content.article.' . $fields->id))
            {
                $link = JRoute::_('index.php?option=com_content&task=article.edit&id=' . $fields->id);
            }
            else
            {
                $link = '';
            }
            $results[$row]->title = ($link == '') ? $fields->title : '<a href="' . $link . '" target="_blank">' . $fields->title . '</a>';

            // Remove all HTML tags from introtext and fulltext
            $results[$row]->introtext = strip_tags($fields->introtext, '<br><br/><strong><em><b><i>');
            $results[$row]->fulltext = strip_tags($fields->fulltext, '<br><br/><strong><em><b><i>');

            // Shorten the introtext and fulltext to 300 characters
            if (!empty($results[$row]->introtext))
            {
                $results[$row]->summary = strlen($results[$row]->introtext) > 300 ? substr($results[$row]->introtext, 0, 300) . " […]" : $results[$row]->introtext;
            }
            else if  (!empty($results[$row]->fulltext))
            {
                $results[$row]->summary = strlen($results[$row]->fulltext) > 300 ? substr($results[$row]->fulltext, 0, 300) . " […]" : $results[$row]->fulltext;
            }
            else
            {
                $results[$row]->summary = '[No Content]';
            }

            // Replace the user id with the username
            $results[$row]->modified_by = $fields->username;

            // The rest of the returned fields are just copied back into the return class
            $results[$row]->state = $fields->state;
            $results[$row]->modified = $fields->modified;
            $results[$row]->featured = $fields->featured;
        }

        return $results;
    }
}

28.2.3 HTML-Ausgabe – »/tmpl/default.php«

„/tmpl/default.php“: kleine Ergänzung der Spaltenüberschriften

<?php
/**
 * @version   0.2.0
 * @author    Vorname Nachname
 * @copyright Copyright (C) 2015 Vorname Nachname
 * @license   http://www.gnu.org/licenses/gpl-3.0.html
 */

defined('_JEXEC') or die;

// Import the tooltip funcionality to show the article content on mouse hover over the title
JHtml::_('behavior.tooltip');

?>

<div class="recentlyedited">
    <div class="row-striped">
        <div class="row-fluid">
            <div class="span1 small">Online</div>
            <div class="span6 small">Article Title</div>
            <div class="span3 small">Last Modified</div>
            <div class="span2 small">By</div>
        </div>
        <?php foreach ($list as $item) : ?>
        <div class="row-fluid">
            <?php
                // Add a simple Joomla standard class to show if this is a published or unpublished article
                $stateClass = ($item->state == '1') ? ' icon-publish' : ' icon-unpublish';
            ?>
            <div class="span1 <?php echo $stateClass; ?>">
                &nbsp;
            </div>
            <?php
                // Add a simple Joomla standard class to show if this is a featured article
                $featuredClass = ($item->featured == '1') ? ' icon-featured' : ' icon-unfeatured';
            ?>
            <div class="span6 <?php echo $featuredClass; ?>">
                <strong class="row-title">&nbsp;&nbsp;
                    <?php echo JHtml::tooltip($item->summary, '', '', $item->title); ?>
                </strong>
            </div>
            <div class="span3">
                <?php echo JHtml::_('date', $item->modified, JText::_('DATE_FORMAT_LC2')); ?>
            </div>
            <div class="span2">
                <?php echo $item->modified_by; ?>
            </div>
        </div>
        <?php endforeach; ?>
    </div>
</div>

Handbuch lesen

Infos zum Joomla-Handbuch, Inhaltsübersichten und Leseproben finden Sie auf dieser Webseite des Rheinwerk Verlags.

Oder natürlich bei Amazon:

Handbuch empfehlen

Sponsoren