I am documenting the process to add a module position (loadposition) tag inside a Joomla 3.x component. With the Joomla help and documentation a bit low, quite a search was needed to get the code up and running. I had to use help pages from older versions of Joomla and get them to work on Joomla 3.7.x which was the current version when project was implemented.

The purpose was in the component the user could add inside there content the following tag.

{loadposition user1}

This is standard functionality for Joomla and can be used lots of places including articles.

When the page is generated in the front part of Joomla, the loadposition will be replaced with a module created and marked with position "user1".

Firstly we have to find all the occurrences of load position in the user generated html content.

See below the functions I used in the end.

function plgModuleLoadModule( $html )
{
   $db = JFactory::getDBO();
   // simple performance check to determine whether bot should process further
   if ( JString::strpos( $html, 'loadposition' ) == false ) {
      return $html;
   }

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

   // find all instances of plugin and put in $matches
   preg_match_all( $regex, $html, $matches );

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

   // plugin only processes if there are any instances of the plugin in the text
   if ( $count ) {
      $html = plgModuleProcessPositions( $html, $matches, $count, $regex );
   }
  
   return $html;
}

We use an regular expression to find all occurrences of loadposition. ( To be honest I still have to look further into the world of Regular expressions, I still search for the correct regex patterns. )

Then we need to loop thru all the occurrences and extract the "user1" position to get the module code from Joomla. And replace the loadposition string part with the actual module code.

function plgModuleProcessPositions ( $html, $matches, $count, $regex )
{
   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 );
      $html    = str_replace($matches[0][$i], $modules, $html );
   }

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

Next we need to use JModuleHelper::getModules("user1") to extract all the module codes with an "user1" position. There can be more than one module in the "user1" position. (To be honest there is different ways of extracting the module from Joomla, I am not sure if this is the correct way for Joomla 3.x but for me it works.)

function plgModuleLoadPosition( $position )
{
   $document   = JFactory::getDocument();
   $renderer   = $document->loadRenderer('module');
   $options   = array('style' => 'xhtml');

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

I used the following code to execute the function.

$htmlbody = plgModuleLoadModule($htmlbody);

 Here is the code all together if you want to copy and Paste.

function plgModuleLoadModule( $html )
{
   $db = JFactory::getDBO();
   // simple performance check to determine whether bot should process further
   if ( JString::strpos( $html, 'loadposition' ) == false ) {
      return $html;
   }

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

   // find all instances of plugin and put in $matches
   preg_match_all( $regex, $html, $matches );

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

   // plugin only processes if there are any instances of the plugin in the text
   if ( $count ) {
      $html = plgModuleProcessPositions( $html, $matches, $count, $regex );
   }
  
   return $html;
}

function plgModuleProcessPositions ( $html, $matches, $count, $regex )
{
   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 );
      $html    = str_replace($matches[0][$i], $modules, $html );
   }

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

function plgModuleLoadPosition( $position )
{
   $document   = JFactory::getDocument();
   $renderer   = $document->loadRenderer('module');
   $options   = array('style' => 'xhtml');

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

}

$htmlbody = plgModuleLoadModule($htmlbody);

References:

1. https://www.amityweb.co.uk/blog/adding-a-module-position-loadposition-inside-another-module-in-joomla

2. https://joomla.stackexchange.com/questions/1054/displaying-a-joomla-module-using-php