2

I was just wondering if there was some PHP that I could add to my template index.php that would ensure only one module is loaded in a particular position.

I know there are other options available such as Advanced Module Manager but for this particular site it would be great if I could add some php directly to the page. Basically if there are 2 modules being loaded into one position then I just want the first module to load.

Thanks.

  • 1
    Is this a template you're developing to make available for people to download If so I would strongly suggest you don't do this. Else if this if just your own website, then why would you need to do this? Can you simply just ensure that you only assign 1 mdoule to 1 position? – Lodder Jul 26 '14 at 9:21
  • This template is just for me, no one else. This is a specific issue related to one website only. – DesignSubway Jul 27 '14 at 8:37
  • Have to agree with others here, the best way is to just not publish more than one module there. Simple, effective, works out-of-the-box. – Seth Warburton Jul 28 '14 at 13:12
2

I have to agree with Lodder's comment... This is not making a real sense and it probably will create confusion.

But for the sake of answering to your question and to help you find a way to achieve something like this, you could try the approach below:

<?php
  jimport( 'joomla.application.module.helper' );
  $modules = JModuleHelper::getModules( 'yourposition' );
  $attribs = array('style' => 'xhtml');    
  echo JModuleHelper::renderModule($modules[0], $attribs);
?>

The getModules($position) function will load an array with all the modules in your position, ordered by module id.

Then, with the renderModule($module,$attributes) function, you output the module. But since you have an array, you need to tell which module you will render, by specifying the index of the array's element.

But because there will still be a dynamic way of creating/editing/changing modules settings, I am not that sure how you could ensure what module will be rendered at the end, in the case that other users will be managing the website.

So, if you would explain a little bit more what are you trying to achieve and why, we could propose you the right solution.

1

Most templates render modules like thus

<?php if ($this['modules']->count('search')) : ?>
<div id="search"><?php echo $this['modules']->render('search'); ?></div>
<?php endif; ?>

$this['modules']->count('search') is basically the number of modules so if you had two modules in the search position being rendered on a particular page, the value would be 2

You could build in a simple if $this['modules']->render('search'); > 1 then echo 'Oly one module allowed in this position' type code in your index file for the template, but this is not really standard and this is not really how Joomla is designed to work, and if its your own website, just make sure you only set one module.

As a retail template, I can see that particular function causing issues and maybe you should look at coding that particular module position as a slideshow so that it can render multiple modules, but just slides through them in the same space as opposed to rendering them all at once.

Hope this helps

  • The $this within the template refers to an JDocumentHTML Object for the template. There is no modules index in there. Furthermore the JDocumentHTML function to count the modules of a position is countModules and not count. How the code snippet of your answer can be used inside a casual joomla template, because as it is, it can't...? – FFrewin Jul 29 '14 at 7:47
  • For reference: The way of counting and rendering modules within the template is described here: docs.joomla.org/JDocumentHTML/countModules – FFrewin Jul 29 '14 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.