Tip: apply content plugins to mod_newsflash and mod_custom

Everything to do with Joomla! 1.5 templates and templating.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Locked
fedefil
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Mon Feb 11, 2008 7:32 am

Tip: apply content plugins to mod_newsflash and mod_custom

Post by fedefil » Wed Feb 13, 2008 9:52 am

Hi,
as (almost) everybody already knows, in Joomla! 1.5 content plugins are not applied to content shown in modules.
Thanks to overrides, you can easily get around this "bug" (bug? No, no. It's a feature! ;) ).
Here is my proposed solution.

For the Newsflash module, as a first step, I overloaded the modNewsFlashHelper::renderItem() method in a custom class, adding the relevant code for triggering content plugins on the item text (near the end of the method).

Code: Select all

class myNewsFlashHelper extends modNewsFlashHelper
{
	function renderItem(&$item, &$params, &$access)
	{
		global $mainframe;

		$user 	=& JFactory::getUser();

		$item->text 	= $item->introtext;
		$item->groups 	= '';
		$item->readmore = (trim($item->fulltext) != '');
		$item->metadesc = '';
		$item->metakey 	= '';
		$item->access 	= '';
		$item->created 	= '';
		$item->modified = '';

		if ($params->get('readmore') || $params->get('link_titles'))
		{
			if ($params->get('intro_only'))
			{
				// Check to see if the user has access to view the full article
				if ($item->access <= $user->get('aid', 0)) {
					$linkOn = JRoute::_(ContentHelperRoute::getArticleRoute($item->slug, $item->catslug, $item->sectionid));
				} else {
					$linkOn = JRoute::_('index.php?option=com_user&task=register');
				}
			}

			$item->linkOn = $linkOn;
		}
		
		if (!$params->get('image')) {
			$item->text = preg_replace( '/<img[^>]*>/', '', $item->text );
		}
		
		$results = $mainframe->triggerEvent('onAfterDisplayTitle', array (&$item, &$params, 1));
		$item->afterDisplayTitle = trim(implode("\n", $results));

		$results = $mainframe->triggerEvent('onBeforeDisplayContent', array (&$item, &$params, 1));
		$item->beforeDisplayContent = trim(implode("\n", $results));

		// Apply content plugins to newsflash items
		$results = $mainframe->triggerEvent('onPrepareContent', array (&$item, &$params, 1));
		
		require(JModuleHelper::getLayoutPath('mod_newsflash', '_item'));
	}
}
I then saved my class in a file called "helper.php" inside the "html/mod_newsflash" subdirectory of my template.
In order to apply the custom method, I just call it in the actual overload, instead of the original one. For ex., in the vertical layout rendering script "vert.php" (inside the "html/mod_newsflash" subdirectory), I first include my custom class file and then call the myNewsFlashHelper::renderItem() method.

Code: Select all

<?php defined('_JEXEC') or die('Restricted access'); ?>

require_once (JPATH_SITE.DS.'templates'.DS.$mainframe->getTemplate().DS.'html'.DS.'mod_newsflash'.DS.'helper.php');

<?php foreach ($list as $item) :
	myNewsFlashHelper::renderItem($item, $params, $access);
	if (count($list) > 1) : ?>
		<span class="article_separator">&nbsp;</span>
 	<?php endif; ?>
<?php endforeach; ?>
For the custom modules, my approach has been to create a custom module chrome.
In a file called "modules.php", inside the "html" directory of my template, I put a custom module chrome function for a "customxhtml" module style (you can call it whatever you like).

Code: Select all

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

/**
 * Custom module chrome.
 * Content plugins are applied to module content before rendering it
 */
function modChrome_customxhtml($module, &$params, &$attribs)
{
  	global $mainframe;

	if ($module->module == "mod_custom") {
		// Create temporary article
		$item =& JTable::getInstance('content');
		$item->parameters = new JParameter('');
		$item->text	= $module->content;
		// Apply content plugins to custom module content
		$results = $mainframe->triggerEvent('onPrepareContent', array (&$item, &$params, 1));
		$module->content = $item->text;
	}	
	modChrome_xhtml($module, $params, $attribs);
}
?>
As you can see, I create a temporary article, set the article text to the module content, apply the content plugins to it and then copy the resulting text back into the module content. Finally, I call the original chrome function for the "xhtml" module style. A bit of a hack, but it works!

This solution is very versatile, because lets you choose which custom modules will have the content applied, based on their position in the template main layout. For example, you can apply the custom chrome only to modules in the "top" position, with the following line of code in your template's "index.php" script.

Code: Select all

<jdoc:include type="modules" name="top" style="customxhtml" />
I hope the above tips will help someone overcome this J! 1.5 little nuisance.
Obviously, don't hesitate to propose improvements and/or optimizations to my code.
As I already say, I'm here to help but also to learn.

Best regards,
Federico

 
seoalan
Joomla! Fledgling
Joomla! Fledgling
Posts: 4
Joined: Thu May 22, 2008 2:50 pm
Contact:

Re: Tip: apply content plugins to mod_newsflash and mod_custom

Post by seoalan » Thu May 22, 2008 3:33 pm

helllo...I have this question. I want to customze mod_Flash so that if user selects all sections then module should show content items of ALL sections.
i tried a few things but didnt work..
can u tell where i need to change..or some link wher i can get this info...
Thanks in advance...

Mello
Joomla! Intern
Joomla! Intern
Posts: 98
Joined: Fri Jan 25, 2008 8:12 am
Location: Kuwait
Contact:

Re: Tip: apply content plugins to mod_newsflash and mod_custom

Post by Mello » Sat Jul 19, 2008 6:43 pm

Thanks for the code
you was so helpful with me
Mestslm Rady Be Tab3ek, 3sfor Fe El-7elm Masaknk :: Me7tar 3la Shat Shafayfk, Dh 3tor El Koon Men Ward
Rosheha Nasyeny Meen ANA!
My Hosting Company : http://www.sz4h.com

Varsys
Joomla! Apprentice
Joomla! Apprentice
Posts: 27
Joined: Thu Sep 18, 2008 3:31 pm
Location: Chicago, IL
Contact:

Re: Tip: apply content plugins to mod_newsflash and mod_custom

Post by Varsys » Tue Dec 16, 2008 4:54 pm

That worked! Thanks for helpful info.

User avatar
BBC2009
Joomla! Intern
Joomla! Intern
Posts: 50
Joined: Sun Feb 08, 2009 4:41 pm

Re: Tip: apply content plugins to mod_newsflash and mod_custom

Post by BBC2009 » Sat Apr 25, 2009 10:03 pm

I tried this and it´s working. I can get my yoogallery plugin to show in my Newsflash module.
But I can´t get it working if I click on a menu link that is linking to a component!!??

Tex, "Contact", "Search" or any other extension (component).

If I click on link that is linking to a component I get again just visible text code in Newsflash module. With all other links it´s working OK. (articles)

I tried with external links and it´s showing just tex code in module, it wont parse plugin.

Will someone give me some directions why is it so? Thanks.

User avatar
BBC2009
Joomla! Intern
Joomla! Intern
Posts: 50
Joined: Sun Feb 08, 2009 4:41 pm

Re: Tip: apply content plugins to mod_newsflash and mod_custom

Post by BBC2009 » Sat Apr 25, 2009 10:27 pm

It´s not because of template. (tested)
It´s not because of module position. (tested)

Tested with last SVN Joomla files too.

This hack is more or less useless if it not trigger plugin content when you click on a link that is linking to a extension (component). What a pitty.

User avatar
BBC2009
Joomla! Intern
Joomla! Intern
Posts: 50
Joined: Sun Feb 08, 2009 4:41 pm

Re: Tip: apply content plugins to mod_newsflash and mod_custom

Post by BBC2009 » Mon Apr 27, 2009 12:07 pm

For those interessted in parsing plugin contents in modules, use "Ultimate Content Display Module" from Joomlaworks. It´s free, it does parse plugin contents and it has nice "fade" and "slide" Ajax ynd Jquery effects, if you want to show nice slides of your articles, images, plugins, etc....

Anyway want to say that I don´t doubt that this hack is OK, as topic author write it. Just couldn´t get it to work in my case.

vaniklot
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Wed Apr 01, 2009 4:23 am

Re: Tip: apply content plugins to mod_newsflash and mod_custom

Post by vaniklot » Tue Apr 28, 2009 7:39 pm

thank you very much for the info

User avatar
bobysolo
Joomla! Apprentice
Joomla! Apprentice
Posts: 23
Joined: Fri Sep 15, 2006 2:16 pm
Contact:

Re: Tip: apply content plugins to mod_newsflash and mod_custom

Post by bobysolo » Fri Jul 17, 2009 9:26 am

That was just what I was looking for. Thank you very much.

I wonder why this function isn't implemented in Joomla nativelly? Couldn't there be a switch / option by each module where you could select whether that module would parse plugins or not?

While I was searching for a solution, found a similar solution, if you have fixed modules, already defined in template files, than you could use this code - found on FyreWolfe's Web Log:

Code: Select all

<?php
$email = 'some.email@some.domain';
$emcloaked = JHTML::_('Email.cloak',$email);
echo $emcloaked;
?>
or just simply

Code: Select all

echo JHTML::_("email.cloak", "name.surname@example.com");
Anyway, thanks again.

User avatar
bobysolo
Joomla! Apprentice
Joomla! Apprentice
Posts: 23
Joined: Fri Sep 15, 2006 2:16 pm
Contact:

Re: Tip: apply content plugins to mod_newsflash and mod_custom

Post by bobysolo » Sat Aug 22, 2009 3:33 pm

After some testing I found out that this method works on all pages, except on the Frontpage and on the contact page. Any ideas why?

EDIT:
I figured out why the module doesn't parse the plugins - because I have no content loaded on the frontpage, just modules. I have no

Code: Select all

<jdoc:include type="component" />
showing on the frontpage. Even if I add it, it changes nothing, since I have no articles showing on the frontpage. Only when I select an article, to be shown on the frontpage, the module parses the plugins. And it doesn't matter if I have the <jdoc:include type="component" /> declared in my template for the frontpage or not - it works both ways.

So, my solution was ultimately only to select an article to be shown on the Frontpage and disable the <jdoc:include type="component" /> calling for the Frontpage, since I don't need it there.

Now I only have to figure out, how to parse the plugins in modules on the contact page (com_contact).

djb_excal
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Fri Sep 18, 2009 2:35 pm

Re: Tip: apply content plugins to mod_newsflash and mod_custom

Post by djb_excal » Fri Sep 18, 2009 2:43 pm

I edited helper.php, adding

Code: Select all

// Apply content plugins to newsflash items

      $results = $mainframe->triggerEvent('onPrepareContent', array (&$item, &$params, 1));
inside the renderItem function for the modNewsFlashHelper class, and the plugin that didn't show before did without me having to change other code.

MechtaDesign
Joomla! Apprentice
Joomla! Apprentice
Posts: 7
Joined: Sat Aug 01, 2009 7:15 pm

Re: Tip: apply content plugins to mod_newsflash and mod_custom

Post by MechtaDesign » Sat Oct 24, 2009 7:33 pm

I've modifyed all the required fields, and files, now it's giving me an error on all pages...

Fatal error: Class 'modNewsFlashHelper' not found in /public_html/online/modules/mod_newsflash/helper.php on line 21

it's the helper.php file, and on line 21 is only this:

class myNewsFlashHelper extends modNewsFlashHelper

what did I do wrong?... I have created a template from Artisteer 2... any help would be usefull... thank you in advance...

ademc
Joomla! Apprentice
Joomla! Apprentice
Posts: 12
Joined: Sat Dec 06, 2008 5:53 am

Re: Tip: apply content plugins to mod_newsflash and mod_custom

Post by ademc » Mon Mar 08, 2010 7:03 am

fedefil wrote:

Code: Select all

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

/**
 * Custom module chrome.
 * Content plugins are applied to module content before rendering it
 */
function modChrome_customxhtml($module, &$params, &$attribs)
{
  	global $mainframe;

	if ($module->module == "mod_custom") {
		// Create temporary article
		$item =& JTable::getInstance('content');
		$item->parameters = new JParameter('');
		$item->text	= $module->content;
		// Apply content plugins to custom module content
		$results = $mainframe->triggerEvent('onPrepareContent', array (&$item, &$params, 1));
		$module->content = $item->text;
	}	
	modChrome_xhtml($module, $params, $attribs);
}
?>
I tried to put the code above in modules.php, and it didn't seem to work but it put me on the right track.

I used the following code in modules.php, and it allows me to use plugins in mod_custom

Code: Select all

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

/**
* Custom module chrome.
* Content plugins are applied to module content before rendering it
*/
function modChrome_customPlugin($module, &$params, &$attribs)
{
     global $mainframe;

   if ($module->module == "mod_custom") {
      // Create temporary article
      $item = JTable::getInstance('content');
      $dispatcher =JDispatcher::getInstance();
      $params =new JParameter('');
      JPluginHelper::importPlugin('content');
      $item->parameters = new JParameter('');
      $item->text   = $module->content;
      // Apply content plugins to custom module content
      $results = $dispatcher->trigger('onPrepareContent', array ($item, $params, 0));
      $module->content = $item->text;
   }   
   modChrome_none($module, $params, $attribs);
}
?>
Thought I may help some others that couldn't get the original code working.

flerd
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Mon Feb 11, 2008 4:32 pm

Re: Tip: apply content plugins to mod_newsflash and mod_custom

Post by flerd » Sat Apr 03, 2010 9:03 am

bobysolo wrote:After some testing I found out that this method works on all pages, except on the Frontpage and on the contact page. Any ideas why?

EDIT:
I figured out why the module doesn't parse the plugins - because I have no content loaded on the frontpage, just modules. I have no
...
So, my solution was ultimately only to select an article to be shown on the Frontpage and disable the <jdoc:include type="component" /> calling for the Frontpage, since I don't need it there.
...
Now I only have to figure out, how to parse the plugins in modules on the contact page (com_contact).
hi all,
(to solve the problem of BBC2009 and bobysolo)

to get it work with any link item which has no content (component, wrapper, contact, ect.)
just use this code:

Code: Select all

// Apply content plugins to newsflash items
   JPluginHelper::importPlugin('content'); // need this to work without content
   $results = $mainframe->triggerEvent('onPrepareContent', array (&$item, &$params, 1));
there is a good module to do this without any hack, just check:
name: CustomContent
http://extensions.joomla.org/extensions ... dules/4175
thanks to Ian MacLennan

i found this code in his module ;)

i know it is an old topic but maybe it is usefull for someone :)

:eek:

turkmaxi
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Sun Apr 11, 2010 12:43 pm

Re: Tip: apply content plugins to mod_newsflash and mod_custom

Post by turkmaxi » Sun Apr 11, 2010 1:20 pm

thanks for help

infocom
Joomla! Intern
Joomla! Intern
Posts: 96
Joined: Sun Oct 22, 2006 2:26 pm
Contact:

Re: Tip: apply content plugins to mod_newsflash and mod_cust

Post by infocom » Wed Dec 15, 2010 2:14 pm

I found a simple way that requires no core file hack... its just adding the loadposition functions into0 the modules.php file which you override in your template.

1) First copy templates/system/html/modules.php into your_template/html/ override folder so we dont change the core file.

2) Then I copy the loadposition plugin code (see below) into this file as new functions.

3) I change reference to plgContentLoadModule, plgContentProcessPositions and plgContentLoadPosition to plgModuleLoadModule, plgModuleProcessPositions and plgModuleLoadPosition so we dont get conflict with the actual loadposition functions.

4) I also change reference to $row->text to $module->content

5) I then call the function plgModuleLoadModule($module, &$params); to replace the module content with the new content with the loadposition content in it. e.g.

Code: Select all

<?php $module->content = plgModuleLoadModule($module, &$params); ?>
Seems to work OK for me.

Code to add into modules.php:-

Code: Select all

function plgModuleLoadModule( &$module, &$params, $page=0 )
{
	$db =& JFactory::getDBO();
	// simple performance check to determine whether bot should process further
	if ( JString::strpos( $module->content, 'loadposition' ) == false ) {
		return $module->content;
	}

 	// expression to search for
 	$regex = '/{loadposition\s*.*?}/i';

 	$pluginParams = new JParameter( $plugin->params );

	// check whether plugin has been unpublished
	if ( !$pluginParams->get( 'enabled', 1 ) ) {
		$module->content = preg_replace( $regex, '', $module->content );
		return true;
	}

 	// find all instances of plugin and put in $matches
	preg_match_all( $regex, $module->content, $matches );

	// Number of plugins
 	$count = count( $matches[0] );

 	// plugin only processes if there are any instances of the plugin in the text
 	if ( $count ) {
		// Get plugin parameters
	 	$style	= $pluginParams->def( 'style', -2 );

 		plgModuleProcessPositions( $module, $matches, $count, $regex, $style );
	}
	
	return $module->content;
}

function plgModuleProcessPositions ( &$module, &$matches, $count, $regex, $style )
{
 	for ( $i=0; $i < $count; $i++ )
	{
 		$load = str_replace( 'loadposition', '', $matches[0][$i] );
 		$load = str_replace( '{', '', $load );
 		$load = str_replace( '}', '', $load );
 		$load = trim( $load );

		$modules	= plgModuleLoadPosition( $load, $style );
		$module->content 	= str_replace($matches[0][$i], $modules, $module->content );
 	}

  	// removes tags without matching module positions
	$module->content = preg_replace( $regex, '', $module->content );
	
	return $module->content;
}

function plgModuleLoadPosition( $position, $style=-2 )
{
	$document	= &JFactory::getDocument();
	$renderer	= $document->loadRenderer('module');
	$params		= array('style'=>$style);

	$contents = '';
	foreach (JModuleHelper::getModules($position) as $mod)  {
		$contents .= $renderer->render($mod, $params);
	}
	return $contents;
}


 

Locked

Return to “Templates for Joomla! 1.5”