2 minutes reading time (312 words)

Supporting custom module positions in your Joomla extension

Supporting custom module positions in your Joomla extension

Note: This Blog is targeted to developers!

Sometimes the module positions that come with your template are just not enough, or if you’re a developer you might want to allow your users to load modules inside various parts of your component.

A good example is if you want to publish Ads coming from SocialAds on your EasySocial stream without changing any default functionalities of the stream. You'd do this by loading the Social Ads module in one of the various positions that Easysocial has already set up on their stream. 

Sounds familiar ? Read on for a simple yet powerful trick to do exactly that!

 

This is a 2 step process -

1. Create custom position

  • Go to module admin

  • In position: write the custom position name. Say custom-body

  • Choose publish, of course!

  • Save

You can create as many positions you want or can have same position for multiple modules.

2. Render in HTML

Go to your php code, to render these modules. Assuming multiple modules are published at position named custom-body, the following code will fetch all the modules from that position.

 

jimport( 'joomla.application.module.helper' ); // Internal Joomla stuff, dont change
$renderer = $document->loadRenderer('module'); // Internal Joomla stuff, dont change
$modules = JModuleHelper::getModules( 'custom-body' ); // This loads all modules from the custom-body position

$display_module = ''; 
ob_start(); 
foreach ($modules as $module) { 
$attribs['style'] = 'xhtml'; 
$display_module .= $renderer->render($module, $attribs); 
} 
ob_get_clean();

echo $display_module; // Print the HTML output

The last line actually prints the HTML output, so place it wherever you wish to print the HTML. This is how you can easily supplement the already present positions in your template. Create your own positions and show them wherever you wish to display. This can be used to support custom positions in your own extensions, or you can put this in layout overrides to add positions to existing extensions.

1
The Joomla Speak! In Conversation with Peter Bui
SocialAds 3.1. First Look!

Related Posts