Kapitel 27 – Plugins entwickeln

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

 

27.1.2 XML-Manifest – »imagepopup.xml«

„imagepopup.xml“: einfachste Ausführung eines Plugin-XML-Manifests mit Referenzen zur Programm- und Verzeichnisschutzdatei

<?xml version="1.0" encoding="utf-8"?>
<extension version="3.0" type="plugin" group="content" method="upgrade">
    <name>Content – Image Popup</name>
    <author>Vorname Nachname</author>
    <creationDate>May 2015</creationDate>
    <copyright>Copyright (C) 2015 Vorname Nachname. 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>http://IhrDomainName.de</authorUrl>
    <version>0.1.0</version>
    <description>Beschreibung</description>
    <files>
        <filename plugin="imagepopup"> imagepopup.php</filename>
        <filename>index.html</filename>
    </files>
</extension>

27.1.3 Plugin-Code – »imagepopup.php«

„imagepopup.php“: Businesslogik-Teil des Plugins, eine einfache Debug-Ausgabe für die erste Version

<?php
/**
 * @version   0.1.0
 * @author    Vorname Nachname
 * @copyright Copyright (C) 2015 Vorname Nachname
 * @license   http://www.gnu.org/licenses/gpl-3.0.html
 */
defined('_JEXEC') or die;

class plgContentImagepopup extends JPlugin
{
    public function onContentPrepare($context, &$article, &$params, $page = 0)
    {
        JFactory::getApplication()->enqueueMessage('Wir sind im Ereignis onContentPrepare', 'notice');
        return true;
    }
}

27.2.1 XML-Manifest – »imagepopup.xml«

„imagepopup.xml“: Erweiterung des XML-Manifests um eine Plugin-Konfiguration und Referenzen zu Sprachdateien

<?xml version="1.0" encoding="utf-8"?>
<extension version="3.0" type="plugin" group="content" method="upgrade">
    <name>Content – Image Popup</name>
    <author>Vorname Nachname</author>
    <creationDate>May 2015</creationDate>
    <copyright>Copyright (C) 2015 Vorname Nachname. 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>http://IhrDomainName.de</authorUrl>
    <version>0.2.0</version>
    <description>PLG_CONTENT_IMAGEPOPUP_DESCRIPTION</description>
    <files>
        <filename plugin="imagepopup">imagepopup.php</filename>
        <filename>index.html</filename>
    </files>
    <languages>
        <language tag="en-GB">language/en-GB/en-GB.plg_content_imagepopup.ini</language>
    </languages>
    <config>
        <fields name="params">
            <fieldset name="basic">
                <field name="popupTechnology" type="radio"
                    default="0"
                    class="btn-group"
                    label="PLG_CONTENT_IMAGEPOPUP _FIELD_POPUPTECHNOLOGY_LABEL"
                    description="PLG_CONTENT_IMAGEPOPUP _FIELD_POPUPTECHNOLOGY_DESC">
                    <option value="0">PLG_CONTENT_IMAGEPOPUP _FIELD_POPUPTECHNOLOGY_OPTION_0_OFF</option>
                    <option value="1">PLG_CONTENT_IMAGEPOPUP _FIELD_POPUPTECHNOLOGY_OPTION_1_MAGNIFICPOPUP</option>
                </field>
            </fieldset>
        </fields>
    </config>
</extension>

27.2.2 Applikationscode – »imagepopup.php«

„imagepopup.php“: Ergänzung der Sprachschlüssel-Platzhalter und Businesslogik für die Darstellung der Popup-Fenster

<?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;

class plgContentImagepopup extends JPlugin
{
    public function onContentPrepare($context, &$article, &$params, $page = 0)
    {
        $this->loadLanguage();
        JFactory::getApplication()->enqueueMessage(JText::_('PLG_CONTENT_IMAGEPOPUP_DEBUGMESSAGE'), 'notice');

        if (($this->params->get('popupTechnology', 0) == 0) || ($context == 'com_finder.indexer'))
        {
            return true;
        }

        JHtml::_('jquery.framework');

        switch ($this->params->get('popupTechnology', 0))
        {
            case '1':
                /* Magnific Popup */
                
                $doc =& JFactory::getDocument();
                $doc->addScript('//cdnjs.cloudflare.com/ajax/libs/magnific-popup.js/1.0.0/jquery.magnific-popup.min.js');
                $doc->addStyleSheet('//cdnjs.cloudflare.com/ajax/libs/magnific-popup.js/1.0.0/magnific-popup.min.css');

                $regex      = '/<img.*?>/';

                $createImageLink = function($imageTag)
                { 
                    preg_match('/src="([^"]*)"/i', $imageTag[0], $results);
                    $srcAttribute = $results[0];
                    $srcUrl = explode("=", $srcAttribute);
                    return '<a href=' . $srcUrl[1] . ' class="magnific_popup">'.$imageTag[0].'</a>';
                };                                                     

                if (isset($article->text))
                {
                    $article->text = preg_replace_callback($regex, $createImageLink, $article->text);
                    $article->text .= '<script> jQuery( document ).ready(function() { jQuery(".magnific_popup").magnificPopup({ type: "image", enableEscapeKey:true }); });</script>';
                }
                if (isset($article->introtext))
                {
                    $article->introtext = preg_replace_callback($regex, $createImageLink, $article->introtext);
                    $article->introtext .= '<script> jQuery( document ).ready(function() { jQuery(".magnific_popup").magnificPopup({ type: "image", enableEscapeKey:true }); });</script>';
                }
                break;
        }

        return true;
    }
}

27.2.3 Sprachdatei – »en-GB.plg_content_imagepopup.ini«

„en-GB.plg_content_imagepopup.ini“: Zuweisung von Ausgabetexten zu Sprachschlüsseln

PLG_CONTENT_IMAGEPOPUP_DESCRIPTION="Beschreibung"
PLG_CONTENT_IMAGEPOPUP_FIELD_POPUPTECHNOLOGY_LABEL="Popup Technology"
PLG_CONTENT_IMAGEPOPUP_FIELD_POPUPTECHNOLOGY_DESC="Pick any popup technology. If one doesn't work or doesn't look good to you, just try a different one."
PLG_CONTENT_IMAGEPOPUP_FIELD_POPUPTECHNOLOGY_OPTION_0_OFF="Off"
PLG_CONTENT_IMAGEPOPUP_FIELD_POPUPTECHNOLOGY_OPTION_1_MAGNIFICPOPUP="Magnific Popup" 
PLG_CONTENT_IMAGEPOPUP_DEBUGMESSAGE="This debugging messages originates from the language file <i>en-GB.plg_content_imagepopup.ini</i>."

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