Adding a module position (loadposition) inside another module in Joomla
1st June, 2011 in Technical
Here is a good way to use {loadposition} inside another module without hacking core files or using extensions in Joomla…
1) First copy templates/system/html/modules.php into your_template/html/ override folder so we dont change the core file.
2) Then 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.
<?php $module->content = plgModuleLoadModule($module, &$params); ?>
Code to add into modules.php:-
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; }
Recent
22nd November, 2017 in
Amity is pleased to announce it's sister web design company has launched in Newport. http://nettlofnewport.co.uk will design & build, host & support and digitally market websites for customers in and around Newport, South Wales, using Amity as the technical backend. Nettl will also offer design & print material for marketing & exhibitions.
Did you know that approximately 96% of people leave a website without taking the action the website owner would like them to complete? Whether it’s buying a product, signing up to a mailing list or requesting a callback, the vast majority of people visiting your site do not do it!
28th November, 2016 in Advice
When did you last check the speed of your website? According to Google’s guide for webmasters, a good eCommerce website should load fully in under 2 seconds...
If your business isn’t currently using social media, now is the time to consider why that is.
25th October, 2016 in Advice, Amity News
December is the perfect time to kick-start campaigns and refresh your marketing. Even if the nature of your business isn’t remotely festive, your customers will appreciate the goodwill of a seasonal offer to two, or simply a friendly ‘Merry Christmas’ message.