How To Add HTML or Plain Text Above And Below Any Module

For Joomla! 3.x Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, dam-man

Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
User avatar
websitedons
I've been banned!
Posts: 393
Joined: Sat May 27, 2017 9:42 am

How To Add HTML or Plain Text Above And Below Any Module

Postby websitedons » Mon Jun 19, 2017 3:59 am

One would be surprised at the added SEO value derived from even more relative text! That is the forever quest of every website owner, and as such, many constantly ask how they can add text below and / or after any module type, specifically that lil' one called mod_footer.

While module templates can be overridden, that only takes care of the display, but you need to have the form fields available in the edit view in order to get the text to the database, and the .xml file which creates the fields, doesn't ride the override train. So you need a plugin for that.

A System plugin serves the purpose.

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

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

Code: Select all

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

class plgSystemAdd_Module_Text extends JPlugin {

   public function onContentPrepareForm($form, $data) {
      // module edit form. it checks for the popular advanced modules extension
      if( $form->getName() == 'com_modules.module' || $form->getName() == 'com_advancedmodules.module' ) {
         JForm::addFormPath(JPATH_PLUGINS.'/system/add_module_text/');
         $form->loadFile('modulefields', false);
      }
   }

}


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

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<extension version="3.7.0" type="plugin" group="system" method="upgrade">
   <name>System - Add Module Text</name>
   <author>AnyWebSiteHere.com</author>
   <creationDate>06.18.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 HTML text area to any module</description>
   
   <files>
      <filename plugin="add_module_text">add_module_text.php</filename>
      <filename>index.html</filename>
     <filename>modulefields.xml</filename>
   </files>
   
</extension>



4. Create within the folder, the form control xml file which will be shown in the module edit view.
modulefields.xml

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<form>
<fields name="params">
   <fieldset name="basic">
      <field type="spacer" hr="true" />
      <field type="spacer" label="&lt;b&gt; Add Text Before And After The Module &lt;/b&gt;" />
      <field name="pretext" type="textarea" class="span8" rows="2" filter="safehtml" label="Pre Text" />
      <field name="posttext" type="textarea" class="span8" rows="2" filter="safehtml" label="Post Text" />
   </fieldset>
</fields>
</form>

If you would like to use the WYSIWYG editor instead of plain text fields, swap the above textarea field lines with

Code: Select all

<field name="posttext" type="editor" width="500" height="400" filter="safehtml" label="Post Text" />

See the list of attributes which can be used: https://docs.joomla.org/Editor_form_field_type

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

5. Compress the folder as a .zip, install and enable. Go to any module edit view. The fields will be at the bottom of the page.
output.jpg


Get the data output on the front end
You can now make overrides of all the modules you wish to add custom text

1. Go to Extensions > Template Manager
2. Under the column titled Templates, click the name of your default front end template (the one with the highlighted star)
3. Click the tab labeled Create Overrides
4. Under the column titled Modules, click any module name to immediately create the override. The page will refresh and confirm the copy.
5. Click the tab titled Editor
6. Click html, mod_footer (or whichever you created), default.php. The code editor will display.
7. Add the following based on your desire, above and below the logical HTML element you see. This example is based on mod_footer.

Code: Select all

<?php if( !is_null($params->get('pretext')) ) { ?>
<div class="pretext"><?php echo $params->get('pretext'); ?></div>
<?php } ?>

<?php // these 2 lines are the default core ?>
<div class="footer1<?php echo $moduleclass_sfx; ?>"><?php echo $lineone; ?></div>
<div class="footer2<?php echo $moduleclass_sfx; ?>"><?php echo JText::_('MOD_FOOTER_LINE2'); ?></div>

<?php if( !is_null($params->get('posttext')) ) { ?>
<div class="posttext"><?php echo $params->get('posttext'); ?></div>
<?php } ?>

8. Click Save.

Repeat the override action for each module you want to add the custom fields. Note The fields will appear for EVERY module even if they do not have the frontend override. Nothing will be harmed, even if text is added.

output-front.jpg


das it!

Some advanced ish
You can take the wilder side of content output with added module text fields by rendering content plugins within those newly added fields. One may wish to use their video plugin {yuutube url="the-url"} or a {gallery}agallery{/gallery}, or even {loadmodule login} etc to post above or below any module. Well it can be done with some additional code lines PER module.

Modify the previous coding

Code: Select all

<?php // allow content plugin rendering with user permission
$user = JFactory::getUser();

$pretext = $params->get('pretext');
$posttext = $params->get('posttext');

if( $user->authorise('core.create', 'com_modules') && $user->authorise('core.edit', 'com_modules') ) {
JPluginHelper::importPlugin('content');
$pretext = JHtml::_('content.prepare', $params->get('pretext'));
$posttext = JHtml::_('content.prepare', $params->get('postext'));
}
?>

<?php if( !is_null($params->get('pretext')) ) { ?>
<div class="pretext"><?php echo $pretext; ?></div>
<?php } ?>

<?php // these 2 lines are the default core ?>
<div class="footer1<?php echo $moduleclass_sfx; ?>"><?php echo $lineone; ?></div>
<div class="footer2<?php echo $moduleclass_sfx; ?>"><?php echo JText::_('MOD_FOOTER_LINE2'); ?></div>

<?php if( !is_null($params->get('posttext')) ) { ?>
<div class="posttext"><?php echo $posttext; ?></div>
<?php } ?>
You do not have the required permissions to view the files attached to this post.

Return to “Joomla! 3.x Coding”

Who is online

Users browsing this forum: No registered users and 6 guests