Back to Top

I recently asked the following K2 question via Twitter:

Twitter Status

I guess I was hoping that this functionality was something that I'd missed in the module configuration somewhere; seems like a pretty obvious requirement, especially if you have parent categories setup for very different purposes, each with their own set of sub-categories (like, a news section or a blog with several sub-category subjects).

It stands to reason that such a requirement would be quite necessary, but when I didn't get an answer to my Tweet, it dawned on me that this functionality was overlooked (or perhaps not thought of) by the K2 developers.

So, I tucked into the source myself to see what I could do about it.

Cracking open mod_k2_tools

So, upon opening the helper.php file from the module that powers the Archive functionality (amongst other things), I set about adding in some extra database queries to the mix to get the exact archive list that I want. I did manage to do this by hard-coding the query for certain CATIDs that were already set-up, but of course, if the list was ever changed or new categories were added, the query would need to be modified manually. Yuck.

Help(er) from mod_latestnews

It was then that I remembered that the Latest News core module had similar functionality to what was needed: the ability for the user to specify a comma-separated list of categories to include, which was pulled from the module's XML parameters. Here's the code:

$ids = explode( ',', $catid );
JArrayHelper::toInteger( $ids );
$catCondition = ' AND (cc.id=' . implode( ' OR cc.id=', $ids ) . ')';

Pretty simple huh? Next step was to add the extra parameter into the K2 Tools module's XML (mod_k2_tools.xml), and remove/disable the original select list:

<param name="inccatid" type="text" default="" label="Category IDs"
description="Choose the category IDs, separated with a comma." />

I modified some of the param/variable names above and in the next bit, just to be sure I wasn't overwriting any other variables. So, next was to implement the code into the helper file (mod_k2_tools/helper.php). The first part grabs the values from the XML parameters:

$inccatid = trim( $params->get('inccatid') );

Then we test to see if any values have been entered, and set-up our extra query if true:

if ($inccatid)
{
$cids = explode( ',', $inccatid );
JArrayHelper::toInteger( $cids );
$query .= ' AND (catid=' . implode( ' OR catid=', $cids ) . ') ';
}

...and that's it. All you need to do now is specify the catids to include in your archive list (via the module administrator) and you're good to go.

Update

After further testing it seems that the item count was not being processed correctly, and was counting all items regardless of category. I've updated the files to compensate for this - just some additional changes to pass the category array to the countArchiveItems function in helper.php - however, there is still a further issue remaining that I cannot solve unfortunately. The archive module list is correctly displayed, but when you click on a month in the list, the view shows all items from that month, regardless of category. I cannot find a way to pass the array of categories to the component to restrict the view to only the specified categores. If anyone knows of a solution, please do get in touch.

You can download my modified files below - if you want to use them, simply unzip and upload to your mod_k2_tools module directory, overwriting the two files that exist (I recommend backing them up first just in case you want to revert the module). These files are intended for use on K2 V2.3 only:

About K2

K2 is a GNU/GPL CCK extension for Joomla, created by premier extension developers JoomlaWorks. You can find out more and get involved with the K2 community at http://getk2.org.

Disclaimer

I take no responsibility for any damage done to your website, it's files, nor it's database assets in using these files. If you choose to use them on your own site, you do so at your own risk. No support is provided for this modification, and I do not guarantee that they will work as described. And, like I always say - BACKUP YOUR SITE FIRST!