Add Window Target Option Per Banner In Joomla Banner Manager Component

Sometimes a user wants to set a specific banner to open in a different target window than what the module setting dictates, without having to create a whole other module just to open a single banner from the same category in a new window, when the module target is set to parent. This is resolved with a selection field per banner creation. Of course the excellent JForm system allows that with simplicity. It will let a user override the module setting for any individual banner.

1. Create the folder which will contain all the files and give it a relative name
banner_window_target

2. Create the primary function file within the folder and keep the name similar to the folder for simplicity
banner_window_target.php

<?php defined('_JEXEC') or die();
/**
* @package      plg_system_banner_window_target
* @copyright   Copyright (C) AnySiteYouWantHere.com All rights reserved.
* @license      GNU General Public License version 2 or later; see http://www.gnu.org/licenses/
**/

class plgSystemBanner_Window_Target extends JPlugin {

   public function onContentPrepareForm($form, $data) {
      // get the form field xml
      if( $form->getName() == 'com_banners.banner' ) {
         JForm::addFormPath(JPATH_PLUGINS.'/system/banner_window_target/form/');
         $form->loadFile('banner', false);
      }
   }

}

3. Create the installer XML file within the folder and give it the same name as the function file
banner_window_target.xml

<?xml version="1.0" encoding="utf-8"?>
<extension version="3.7.0" type="plugin" group="system" method="upgrade">
   <name>System - Banner Window Target</name>
   <author>AnyWebSiteHere.com</author>
   <creationDate>06.23.2017</creationDate>
   <copyright>All Rights Reserved.</copyright>
   <license>license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html</license>
   <authorEmail>yourmail@yoursite.com</authorEmail>
   <authorUrl>http://www.yoursitehere.com</authorUrl>
   <version>1.0</version>
   <description>Add individual ad banner window target to override the module setting as needed</description>
   
   <files>
      <filename plugin="banner_window_target">banner_window_target.php</filename>
      <filename>index.html</filename>
     <folder>form</folder>
   </files>
</extension>

4. Create a folder named form within the plugin folder and add the form control file which will be shown in the banner item edit under the image setting.
banner.xml

<?xml version="1.0" encoding="UTF-8"?>
<form>
<fields name="params">
   <fieldset name="image">
      <field name="wintarget" type="list" default="" label="Window Target">
         <option value="">Module Setting</option>
         <option value="0">Parent</option>
         <option value="1">New Window</option>
         <option value="2">Popup Window</option>
      </field>
   </fieldset>
</fields>
</form>

Be sure to add an empty index.html file to the folder or remove <filename>index.html</filename> from the installer .xml file.

5. Compress the parent folder with all the files as a .zip and install, enable, then go to Components > Banners > New. The select field will be above the Click URL field.

Create The Banner Module Template File

This is not an override. This will be selectable when publishing the banner module, under the Advanced tab, field labeled Layout. It outputs the same data as the core default, though the coding appears different, but it will include the window target condition to override the module setting per banner.

1. Create a php file and name it as desired eg: primarybanner.php. Add the following code and save.
2. Upload the file to modules/mod_banners/tmpl/
(you can optionally create an install.php file within the plugin and use JFile::copy() function to place the file)

<?php defined('_JEXEC') or die;

JLoader::register('BannerHelper', JPATH_ROOT . '/components/com_banners/helpers/banner.php');
$baseurl = JUri::base();
?>

<div class="bannergroup<?php echo $moduleclass_sfx; ?>">

<?php
if( $headerText ) echo $headerText;

foreach( $list as $item ) :

//--------------[ Define Variables ]----------------------//
$link = JRoute::_('index.php?option=com_banners&task=click&id=' . $item->id);
$imageurl = $item->params->get('imageurl');

//set conditional variable values to default null
$imgwidth= $imgheight= $linkopen= $linkclose= $attrib='';

if( !is_null($item->params->get('width')) )
   $imgwidth = ' width="' . $item->params->get('width') . '"';
if( !is_null($item->params->get('height')) )
   $imgheight = ' height="' . $item->params->get('height') . '"';

$alt = $item->params->get('alt');
$alt = $alt ?: $item->name;
$alt = $alt ?: JText::_('MOD_BANNERS_BANNER');

/* get the module window target setting and override per banner setting
if value is not null
*/
$target = $params->get('target', 1);
if( $item->params->get('wintarget') != '' )
   $target = $item->params->get('wintarget', '');

?>

   <div class="banneritem">
      
      <?php
      if( $item->type == 1 ) {
      // Text based banners
         echo str_replace(array('{CLICKURL}', '{NAME}'), array($link, $item->name), $item->custombannercode);
         
      }else{
         // Image based banner
         if( BannerHelper::isImage($imageurl) ) {

            // setup link variations
            if( !empty($item->clickurl) ) {
               switch( $target ) {
                  // Open in new window
                  case 1: $attrib = 'target="_blank"';
                  break;
                  // Open in a popup window
                  case 2: $attrib = '//the popup onclick function goes her. it is not allowed here so you will need to copy from original file';
                  break;
               }
               $linkopen = '<a href="'.$link.'" '.$attrib.' rel="noopener noreferrer" title="'.htmlspecialchars($item->name, ENT_QUOTES, 'UTF-8').'">';
               $linkclose = '</a>';
            }
      ?>
            <?php //output the image and link if set
         echo $linkopen; ?>
         <img src="/<?php echo $baseurl . $imageurl;?>" alt="<?php echo $alt;?>" <?php echo $imgwidth . $imgheight; ?> />
      <?php echo $linkclose; ?>
            
      <?php
      }else
         // output flash animated
         if( BannerHelper::isFlash($imageurl) ) { ?>
   // flash object coding goes here. not allowed here so you will need to copy from the original file
      <?php }
         
      } ?>
      
      <div class="clr"></div>
   </div>
   
<?php endforeach; ?>

<?php if( $footerText ) { ?>
   <div class="bannerfooter">
      <?php echo $footerText; ?>
   </div>
<?php } ?>

</div>

 


Saturday, 10 June 2017 11:29  |   258 views

 

Copyright © 2017 CMSEnergizer.com. All Rights Reserved.
This website is powered by the Joomla!©™ 3 Website Framework | File download and article manager component by K2 | Advanced Module Manager and Modules Anywhere by NoNumber Elements | Website hosting and development by WebsiteDons

CMSEnergizer.com is owned and operated by Emuzement Net Inc, 4532 West Kennedy Blvd. Ste 180 33609 Tampa, Florida USA