2017 © Pedro Peláez
 

contao-module contao-modal

A solid contao modal layer framework.

image

heimrichhannot/contao-modal

A solid contao modal layer framework.

  • Thursday, June 21, 2018
  • by digitales@heimrich-hannot.de
  • Repository
  • 5 Watchers
  • 3 Stars
  • 2,211 Installations
  • PHP
  • 6 Dependents
  • 1 Suggesters
  • 0 Forks
  • 1 Open issues
  • 37 Versions
  • 44 % Grown

The README.md

Modal - A solid contao modal window framework

Modal provides modal elements within modal archives. It works best together with 'heimrichhannot/contao-teaser' and its teaser content elements., (*1)

Features

  • Url alias support with backlinks (browser history)
  • extendable for other modal frameworks/libraries
  • custom header/footer
  • content elements within modal body
  • inserttags
  • works together with contao-disclaimer
  • Bootstrap 3 and 4 support
  • Contao Components support

Setup

Install: composer require heimrichhannot/contao-modal, (*2)

We recommend to disable the not used modal framework component in page layout., (*3)

Insertags

Insert-Tag Description Arguments
{{modal_url::*::*::*}} Returns the modal url (usage: <a href="{{modal_url::1::home}}">Hier klicken</a>) 1: modal id/alias, 2: jump to page id/alias, 3: auto_item id or alias
{{modal_link::*::*::*::*}} Returns the modal link 1: modal id/alias, 2: jump to page id/alias, 3: link text & title, 4: auto_item id or alias
{{modal_link_open::*::*::*}} Returns the modal link's opening tag (<a href="{{modal_url::1::home}}">). 1: modal id/alias, 2: jump to page id/alias, 3: auto_item id or alias
{{modal_link_close::*}} Returns the modal link's closing tag (</a>) -> Requires associated {{modal_link_open}} tag. 1: modal id/alias

Hooks

Name Arguments Expected return value Description
generateModalUrl $arrRow, $strParams, $strUrl $strUrl Modify the modal url.
generateModal $objTemplate, $objModel, $objConfig, $objModal void Modify the modal output.
getModalContentElement $strContent, $objElement, $objTemplate, $objModel, $objConfig, $objModal $strContent Modify the modal within content element.

Register custom modules to make usage of modal

If you want to register you list modules for example to make usage of the modal framework, you should enable by adding your module to the $GLOBALS['MODAL_MODULES'], (*4)

config.php
/**
 * Modal module configuration
 */
$GLOBALS['MODAL_MODULES']['mymodule_list'] = array
(
    'invokePalette' => 'customTpl;', // The modal palette will be invoked after the field customTpl; as example
);

Than you have to implement the links within you list module. The following example is taken from the newslist implementation within the modal framework:, (*5)

 public function parseArticlesHook(&$objTemplate, $arrArticle, $objModule)
    {
        if (!$objModule->useModal || $arrArticle['source'] != 'default') {
            return false;
        }

        $objJumpTo = \PageModel::findPublishedById($objTemplate->archive->jumpTo);

        if ($objJumpTo === null || !$objJumpTo->linkModal) {
            return false;
        }

        $objModal = ModalModel::findPublishedByIdOrAlias($objJumpTo->modal);

        if ($objModal === null) {
            return false;
        }

        $objJumpTo = \PageModel::findWithDetails($objJumpTo->id);

        $arrConfig = ModalController::getModalConfig($objModal->current(), $objJumpTo->layout);

        $blnAjax = true;
        $blnRedirect = true;

        $objTemplate->link         = ModalController::generateModalUrl($arrArticle, $objTemplate->archive->jumpTo, $blnAjax, $blnRedirect);
        $objTemplate->linkHeadline = ModalController::convertLinkToModalLink($objTemplate->linkHeadline, $objTemplate->link, $arrConfig, $blnRedirect);
        $objTemplate->more         = ModalController::convertLinkToModalLink($objTemplate->more, $objTemplate->link, $arrConfig, $blnRedirect);
    }

As you can see, we attach to the parseArticles HOOK and adjust all links with ModalController::generateModalUrl() or ModalController::convertLinkToModalLink() functions., (*6)

To access the Reader entities it is necessary to link the redirect page with a modal. Add a new modal first, attach the reader modules as content element and select "link Modal" within the redirect page in the site structure and assign the previously created modal to the page., (*7)

Add custom modal framework

To extend modal with your own framework, you have add the following:, (*8)

Add your own modal config

You have to register your custom modal within '$GLOBALS['TL_MODALS']'., (*9)

// my_module/config/config.php

/**
 * Modal types
 */
$GLOBALS['TL_MODALS']['my_custom_modal'] = array
(
    'header'   => true,
    'footer'   => true,
    'template' => 'modal_my_custom_modal',
    'link'     => array(
        'attributes' => array(
            'data-toggle' => 'modal',
        ),
    ),
    'js'       => array
    (
        'system/modules/my_module/assets/js/jquery.my_custom_modal.js',
    ),
);

Add your modal template

The modal template contains the complete markup of your modal framework. Add as much as possible., (*10)

// my_module/templates/modals/modal_my_custom_modal.html5






The modal link template is required by inserttags (e.g {{modal_link::*}} to provide the correct trigger markup., (*11)

// my_module/templates/modals/modallink_my_custom_modal.html5
<a href="<?= $this->href; ?>" title="<?= $this->linkTitle; ?>"<?= $this->target; ?><?= $this->linkAttributes; ?>><?= $this->link; ?></a>

Add your modal javascript logic

The modal windows are delivered asynchronous by the ModalController. You have to implement the toggle, close and ajax loading by your own., (*12)

// my_module/assets/js/jquery.my_custom_modal.js
(function ($) {

    var ModalBs3 = {
        init: function () {
            this.bindToggle();
            this.bindClose();
            $(document).ajaxComplete($.proxy(this.ajaxComplete, this));
        },
        ajaxComplete: function () {
            this.bindClose();
        },
        bindToggle: function () {
            $('[data-toggle=modal]').on('click', function () {
                var $el = $(this),
                    url = $el.attr('href');

                if (!url) {
                    return false;
                }

                $.ajax({
                    url: url,
                    dataType: 'json',
                    error: function(jqXHR, textStatus, errorThrown) {
                        if (jqXHR.status == 301) {
                            location.href = jqXHR.responseJSON.result.data.url;
                            closeModal(jqXHR.responseJSON, $form);
                            return;
                        }
                    },
                    success: function (response, textStatus, jqXHR) {

                        if (typeof response == 'undefined') {
                            return;
                        }

                        if (response.result.html && response.result.data.id) {
                            var $modal = $(response.result.html);
                            $('body').find('.modal').remove();
                            $modal.appendTo('body').modal('show');

                            if(typeof response.result.data.url !== 'undefined')
                            {
                                history.pushState(null, null, response.result.data.url);
                            }
                        }
                    }
                });

                return false;

            });
        },
        bindClose: function () {
            $('.modal').on('hide.bs.modal', function (e) {
                var $this = $(this);

                // stop embedded videos like youtube
                $this.find('iframe').each(function () {
                    var $this = $(this);

                    // reset the src will stop the video
                    $this.attr('src', $this.attr('src').replace('autoplay=1', 'autoplay=0'));
                });

                // stop embedded audio/video
                $this.find('audio, video').each(function () {
                    this.pause();
                });

                history.pushState(null, null, $this.data('back'));
                // window.location.replace($this.data('back'));
            });
        }
    }

    $(function () {
        ModalBs3.init()
    });

})(jQuery);

The Versions