I am working on a website where I need a category drop-down. And I need it sorted in reverse compared to the standard sorting.
The CMS that I am using for the website is Joomla, and i am using the K2 extension. With K2 comes a module called K2 Tools (mod_k2_tools) and you can use it to make a list of categories or a drop-down menu for choosing the category among other things.
The category listing has a setting in the control panel to decide how to sort it. But if you choose category drop-down, you do not get any options for sorting at all.
After searching a lot, it appears that nobody knows how to do this, but at least I got a suggestion from Simon aka. k2joom to see if it would be possible to use the code from the listing option.
As I am not a programmer, this would not be an easy thing to do, but I started to look at the code. In mod_k2_tools.php there was nothing that looked like sorting, but it called another file, helper.php, so I opened this one and found this piece of code in a section that looked like it could belong to the category listing:
switch ($params->get('categoriesListOrdering')) {
case 'alpha':
$orderby = 'name';
break;
case 'ralpha':
$orderby = 'name DESC';
break;
case 'order':
$orderby = 'ordering';
break;
case 'reversedefault':
$orderby = 'id DESC';
break;
default:
$orderby = 'id ASC';
break;
}
As the category drop-down menu is the next in the settings I looked a bit further and found this code:
function treeselectbox(&$params, $id = 0, $level = 0) {
$root_id = (int) $params->get('root_id2');
$option = JRequest::getCmd('option');
$view = JRequest::getCmd('view');
$category = JRequest::getInt('id');
$id = (int) $id;
$user = &JFactory::getUser();
$aid = (int) $user->get('aid');
$db = &JFactory::getDBO();
if (($root_id != 0) && ($level == 0)) {
$query = "SELECT * FROM #__k2_categories WHERE parent={$root_id} AND published=1 AND trash=0 AND access< ={$aid} ORDER BY ordering ";
} else {
$query = "SELECT * FROM #__k2_categories WHERE parent={$id} AND published=1 AND trash=0 AND access<={$aid} ORDER BY ordering ";
}
Comparing the last couple of lines to the first block seemed to indicate that there indeed was some ordering going on in there. So I tried changing the order in the categories, and the drop-down menu immediately reflected the change!
As I needed reverse sort by ID, I would rather not sit and redo the order by hand every time something got added. So I looked at the first block of code and decided to try to replace “ordering” with “id DESC” in the last couple of lines. They would then look like this:
if (($root_id != 0) && ($level == 0)) {
$query = "SELECT * FROM #__k2_categories WHERE parent={$root_id} AND published=1 AND trash=0 AND access< ={$aid} ORDER BY id DESC ";
} else {
$query = "SELECT * FROM #__k2_categories WHERE parent={$id} AND published=1 AND trash=0 AND access<={$aid} ORDER BY id DESC ";
}
And the miracle happened! The drop-down menu is now prefectly sorted by ID in descending order!
Of course, this change will be overwritten in the next upgrade, which is part of the reason why I document it here. But I also hope that someone that knows a little bit about programming could add sort order as a setting in this module. It would certainly be welcome by me!
Joomla: http://www.joomla.org/
K2: http://getk2.org/














Nice work. I will give it a try. This is a good instruction in unraveling the mysteries of K2
Thank you. I will try to write some more about K2 here in the future.