Adding a module position (loadposition) inside another module in Joomla

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 don’t 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 don’t 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 = ‘/{loadpositions*.*?}/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;
}