Tuesday, October 13, 2009

Placing module positions in the main content

I had a need to place other menus or modules in the main content for things like "see also" or "articles in this category" and I am sure that this method could be modified to place advertising in and amongst content too.

On a side note I found a mod which displays all articles in the current category called "mod_thiscategory", which works really well. I had to modify it slightly and as it was written as a single file with no template override I have re-worked it to be able to use a template (when I have worked out how to pack it up I will send it to the author).

  • Step 1: add a module position to my template xml - mine is called content
  • Step 2: Edit mytemplatepath/html/com_content/article/default.php
  • Step 3: Pick your spot, I chose line 104 just after <?php echo $this->article->event->beforeDisplayContent; ?> which would place the items after the heading and any other heading information
  • Step 5: Write the code to place the modules in that module position. I ended up doing 3 things
    • Used $content_modules = &JModuleHelper::getModules( 'content' ) to get the modules in that position
    • Looped through the result and test if the module is enabled using JModuleHelper::isEnabled($contentmod->name)
    • use the render function (JModuleHelper::renderModule( $contentmod, $_options );) to get the HTML into a variable and if the variable has content at the end of the loop then present the results in a <div> which can be styled appropriately. This step was the last as there was no point in adding a <div> if there was no content from any of the modules.
The code looks like this

<?php
$content_modules = &JModuleHelper::getModules( 'content' );
foreach ($content_modules as $contentmod) {
$_options = array( 'style' => 'xhtml' );
if(JModuleHelper::isEnabled($contentmod->name)) {
$moduleContent .= JModuleHelper::renderModule( $contentmod, $_options );
}
}
if($moduleContent) {
echo '<div class="additionalcontent">'.$moduleContent.'</div>';
}

?>

0 comments:

Post a Comment