On Joomla's Zt News 4 template, I'm trying to show a module position on all k2 pages (like this section page) Except K2 item pages pages.
It's Ok But there's a problem which is driving me crazy; K2 Item pages inherit the class of the "content container" from top level pages such as category pages So while on item view I have 1 module position lesser than category view, the content container would not fill enough width. I use this snippet to show module position on every k2 page except item views:
<?php
$com = JRequest::getCmd('option');
$view = JRequest::getCmd('view');
if(!($com =='com_k2' && $view!=='item')) : ?>
<jdoc:include type="modules" name="position-5" style="xhtml" />
<?php endif ?>
As expected pistion-5
codes don't exist on item views' HTML source but still the content container uses "span6" class which belongs to 3-columns pages. (Actually we need "span9" class for the content container on item pages). ُThese classes are chosen through below function:
public function isContent ()
{
$modLeft = $this->theme->countModules('position-7');
$modItem = $this->theme->countModules('position-custom');
$modRight = $this->theme->countModules('position-5');
switch(true) {
case (!$modLeft && !$modItem && !$modRight):
return "span12";
case (!$modLeft && !$modRight && $modItem):
return "span9";
case (!$modRight):
return "span9";
case (!$modLeft):
return "span9";
case (!$modItem):
return "span9";
default:
return "span6";
}
}
And is assigned to the container this way:
<div class="<?php echo $function->isContent();?>">
<jdoc:include type="component" />
</div>
Please save me from this predicament
There are several possible solutions for this, although I don't think any of my suggestions is optimal.
Option 1
You can create a function that determines the target views so that you do not repeat yourself.
I am not sure exactly what are the conditions for your grid width, or when does
position-custom
come into play, but if I understand you correctly, the main concern is to set$modRight
to0
when you disable that module position.You can adapt it to use your own conditions, if necessary. The part that removes the module position from the template becomes:
This piece of code is based on the
$function
variable in your last excerpt. I do not know if or how it is accessible by the part that determines the inclusion of position-5'sjdoc
tag.Option 2
Another option is to programmatically empty some modules from the position by changing their destination position. I am not sure that this is the cleanest way to achieve this.
To remove only certain modules, you can have an array with their titles or another property and do the following:
In order to completely clear the module position, you can use the following code:
JModuleHelper::getModules
returns an array with objects whichposition
property matches the provided criteria (in this case,'position-5'
). You must empty the position before any code that relies on the count of the contents of that position.