Okay, I wanted to save this here for posterity, as I am notorious about coding stuff, then forgetting where I hid it. :oops:
Like a lot of people, I needed to have Flexicontent alphabetize categories "on the fly" without having to sort them in the back end. I couldn't find anything readily available, but after hours of searching found code that works. Credit goes to the gods of PHP from Stack Overflow, who pointed me to code found on PHP.net.
Basically, there is this awesome PHP function called
usort. You can read about it here:
php.net/manual/en/function.usort.php
Basically, this powerful function allows you to sort objects by specific fields. The thing is, the categories object used in Flexi is indeed nested, so you have to write a special function to loop through and handle every field so you get the sort order you want.
My solution is provided below. Please note that this is specifically for sorting categories in the "directory" view, though hopefully the code will help you elsewhere. Also, of course, please test this in a non-production environment just to be safe.
Be forewarned that you need to use Joomla's HTML override feature for this implementation. Maybe something can be done on the plugin level so that it's not template-dependent?
Anyway, here it is:
1) Create an
html folder within your current Joomla template directory if one isn't already there
2) In the
html directory, create a
com_flexicontent subdirectory. Then, in there, create a directory called
flexicontent
3) Now, go to the following directory: JOOMLA/components/com_flexicontent/views/flexicontent/tmpl (where JOOMLA is your Joomla root)
4) Get a copy the file
default_categories.php and plunk it in the directory you made above. That is: JOOMLA/templates/yourtemplate/html/com_flexicontent/flexicontent. The file needs to be the EXACT name it was when you copied it. DO NOT RENAME IT!
5) Now edit the default_categories.php file (meaning, the copy you just put in your template directory)
6) Somewhere near the top of the file (I inserted this after the first <div> tag:
Code:
<?php
/**
* Sort array of objects by field.
*
* @param array $objects Array of objects to sort.
* @param string $on Name of field.
* @param string $order (ASC|DESC)
*/
function sort_on_field(&$objects, $on, $order = 'ASC') {
$comparer = ($order === 'DESC')
? "return -strcmp(\$a->{$on},\$b->{$on});"
: "return strcmp(\$a->{$on},\$b->{$on});";
usort($objects, create_function('$a,$b', $comparer));
};
$on = "title";
sort_on_field($this->categories, $on, $order = 'ASC')
?>
Basically, this code will sort an object array by the Title field (of course, edit this as you want - the point is that the $on variable must be the sort key, so to speak. In my case, I needed to sort by category title).
7) The above code sorts the top-most category levels. But what about all the subs? In order to sort your sub-categories, you need to go to the line:
Code:
foreach ($sub->subcats as $subcat) :?>
Immediately above that line (but after the <?php statement), add the following code:
Code:
sort_on_field($sub->subcats, $on, $order = 'ASC');
You're now passing the subcategories object array to the sort function.
Save your file and test.
If all goes well, you should now have your categories and subcategories all nice and sorted on the fly. Just in case, I attached the real file I'm using so you can see the code in context.
Again, please tweak and improve this code at will. Hope this helps someone.