Difference between revisions of "Counting modules in multiple module positions"

From Joomla! Documentation

m (→‎Counting Modules in multiple Module positions: corrected <div class="greyline">)
(Some markup changes.)
 
(6 intermediate revisions by 4 users not shown)
Line 1: Line 1:
===== Counting Modules in multiple Module positions =====
+
Basic usage of the ''countModules'' method is explained [[Counting_modules_in_a_given_module_position|here]].
  
The countModules function can be used to determine the number of Modules in more than one Module position.  More advanced calculations can also be performed.
+
The ''countModules'' method can be used to determine the number of Modules in one given Module position.
  
The argument to the countModules function is normally just the name of a single Module position. The function will return the number of Modules currently enabled for that Module position.  But you can also do simple logical and arithmetic operations on two or more Module positions.
+
The argument to the ''countModules'' function is normally just the name of a single Module position. The function will return the number of Modules currently enabled for that Module position.
  
For example, to determine the total number of Modules enabled in the 'user1' and 'user2' positions together, you can use the function call
+
Since Joomla! 3.0 all official templates use plain PHP code to calculate the number of multiple modules positions. This code calculates the content width, and is found in the ''index.php'' file of Protostar:
  
<source lang="php">
+
<syntaxhighlight lang="php">
$this->countModules( 'user1 + user2' );
+
// Adjusting content width
</source>
+
if ($this->countModules('position-7') && $this->countModules('position-8'))
 +
{
 +
$span = "span6";
 +
}
 +
elseif ($this->countModules('position-7') && !$this->countModules('position-8'))
 +
{
 +
$span = "span9";
 +
}
 +
elseif (!$this->countModules('position-7') && $this->countModules('position-8'))
 +
{
 +
$span = "span9";
 +
}
 +
else
 +
{
 +
$span = "span12";
 +
}
 +
</syntaxhighlight>
  
Although the usual arithmetic operators, +. -. *, / will work as expected, these are not as useful as the logical operators 'and' and 'or'.
+
This code is used in the main area of the template and automatically arranges the width of it. So if the user enabled some modules in the left side, the widths will become ''span9'' and ''span3'' respectively. (''span12'' is full width.) If the user enables both left and right, the widths will become ''span3'' ''span6'' ''span3''. And if no modules are enabled, either left or right, the width will become ''span12''. (A full row.)
  
For example, to determine if the 'user1' position and the 'user2' position both have at least one Module enabled, you can use the function call
+
[[Category:Tutorials]]
 
 
<source lang="php">
 
$this->countModules( 'user1 and user2' );
 
</source>
 
 
 
Careful: A common mistake is to try something like this
 
<source lang="php">
 
$this->countModules( 'user1' and 'user2' );
 
</source>
 
<noinclude>
 
[[Category:Intermediate]]
 
[[Category:Templates]]
 
[[Category:Topics]]
 
</noinclude>
 
 
 
This is pretty much guaranteed to always return false regardless of the number of Modules enabled in either position, so check what you are passing to countModules carefully.
 
 
 
You must have exactly one space character separating each item in the string.  For example, 'user1+user2' will not produce the desired result as there must be a space character either side of the '+' sign.  Also, 'user1  +  user2' will produce a PHP error message as there is more than one space separating each element.
 
 
 
Example: The user1 and user2 Module positions are to be displayed in the region, but you want the region to not appear at all if no Modules are enabled in either position.
 
 
 
<source lang="php">
 
<?php if ($this->countModules( 'user1 or user2' )) : ?>
 
<div class="rightcolumn">
 
<jdoc:include type="modules" name="user1" style="xhtml" />
 
<jdoc:include type="modules" name="user2" style="xhtml" />
 
</div>
 
<?php endif; ?>
 
</source>
 
 
 
Example: The user1 and user2 Module positions are to be displayed side-by-side with a separator between them.  However, if only one of the Module positions has any Modules enabled then the separator is not needed.  Furthermore, if neither user1 or user2 has any Modules enabled then nothing is output.
 
 
 
<source lang="php">
 
<?php if ($this->countModules( 'user1 or user2' )) : ?>
 
<div class="user1user2">
 
 
 
<?php if ($this->countModules( 'user1' )) : ?>
 
<jdoc:include type="modules" name="user1" style="xhtml" />
 
<?php endif; ?>
 
 
 
<?php if ($this->countModules( 'user1 and user2' )) : ?>
 
<div class="greyline"></div>
 
<?php endif; ?>
 
 
 
<?php if ($this->countModules( 'user2' )) : ?>
 
<jdoc:include type="modules" name="user2" style="xhtml" />
 
<?php endif; ?>
 
 
 
</div>
 
<?php endif; ?>
 
</source>
 
 
 
Notice how the first countModules call determines if there any Modules to display at all.  The second determines if there are any in the 'user1' position and if there are it displays them.  The third call determines if both user1 and user2 positions have any Modules enabled and if they do then if provides a separator between them.  Finally, the fourth call determines if there are any enabled Modules in the 'user2' position and displays them if there are any.
 

Latest revision as of 11:35, 10 October 2022

Basic usage of the countModules method is explained here.

The countModules method can be used to determine the number of Modules in one given Module position.

The argument to the countModules function is normally just the name of a single Module position. The function will return the number of Modules currently enabled for that Module position.

Since Joomla! 3.0 all official templates use plain PHP code to calculate the number of multiple modules positions. This code calculates the content width, and is found in the index.php file of Protostar:

// Adjusting content width
if ($this->countModules('position-7') && $this->countModules('position-8'))
{
	$span = "span6";
}
elseif ($this->countModules('position-7') && !$this->countModules('position-8'))
{
	$span = "span9";
}
elseif (!$this->countModules('position-7') && $this->countModules('position-8'))
{
	$span = "span9";
}
else
{
	$span = "span12";
}

This code is used in the main area of the template and automatically arranges the width of it. So if the user enabled some modules in the left side, the widths will become span9 and span3 respectively. (span12 is full width.) If the user enables both left and right, the widths will become span3 span6 span3. And if no modules are enabled, either left or right, the width will become span12. (A full row.)