Showing a module position only on k2 item views dealing with responsive layout

849 views Asked by At

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

1

There are 1 answers

4
MasterAM On

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 to 0 when you disable that module position.

public function isK2NonItem() {
    $com = JRequest::getCmd('option');
    $view  = JRequest::getCmd('view');
    return $com =='com_k2' && $view !== 'item';
}

public function isContent()
{
    $modLeft  = $this->theme->countModules('position-7');
    $modRight = $this->theme->countModules('position-5');

    if($this->isK2NonItem()) {
        $modRight = 0;
    }

    switch(true) {
        case (!$modLeft && !$modRight):
            return "span12";
        case (!$modRight):
            return "span9";
        case (!$modLeft):
            return "span9";
        default:
            return "span6";
    }
}

You can adapt it to use your own conditions, if necessary. The part that removes the module position from the template becomes:

<?php
    if(!$function->isK2NonItem() : ?>
        <jdoc:include type="modules" name="position-5" style="xhtml" />
<?php endif; ?>

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's jdoc 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:

$mods = JModuleHelper::getModules('position-5'); //gets all of the modules from the specified position

$remove_titles = array('Mod1', 'Mod2');

for($i = 0; $i < count($mods); ++$i) {
    //you can alo target other module properties, such as id, module or name.
    //skip the condition to clear the position completely
    if(array_search($mods[$i]->title, $remove_titles) {
        $mods[$i]->position = 'nonExistentPosition';
    }
}

In order to completely clear the module position, you can use the following code:

$mods = JModuleHelper::getModules('position-5');

for($i = 0; $i < count($mods); ++$i) {
    $mods[$i]->position = 'nonExistentPosition';
}

JModuleHelper::getModules returns an array with objects which position 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.