Alphabetize Categories

More
13 years 3 months ago #17511 by Brat
Alphabetize Categories was created by Brat
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.

8) 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. ;)
Attachments:

Please Log in or Create an account to join the conversation.

More
13 years 3 months ago #17514 by micker
Replied by micker on topic Alphabetize Categories
Great sharing !
See ggppk to see its good for svn
Regards

FLEXIcontent is Free but involves a very big effort on our part.
Like the our support? (for a bug-free FC, despite being huge extension) Like the features? Like the ongoing development and future commitment to FLEXIcontent?
-- Add your voice to the FLEXIcontent JED listing reviews. Thanks![/size]

Please Log in or Create an account to join the conversation.

More
13 years 3 months ago #17546 by Brat
Replied by Brat on topic Alphabetize Categories

micker wrote: Great sharing !
See ggppk to see its good for svn
Regards


Did you need me to do this? I just didn't understand your post. Thanks! 8-)

Please Log in or Create an account to join the conversation.

More
13 years 3 months ago #17573 by ggppdk
Replied by ggppdk on topic Alphabetize Categories
I have seen your post, adding an option on how to sort displayed categories could be useful. I put it on bug tracker in the TODO enhancements, not to forget

Best Regards


-- Flexicontent is Free but involves a big effort on our part.
Like the our support? (for a bug-free FC, despite having a long list of functions) Like the features? Like the ongoing development and future commitment to FLEXIcontent?
-- Add your voice to the FLEXIcontent JED listing with a 5-star...

Please Log in or Create an account to join the conversation.

Moderators: vistamediajoomlacornerggppdk
Time to create page: 0.435 seconds
Save
Cookies user preferences
We use cookies to ensure you to get the best experience on our website. If you decline the use of cookies, this website may not function as expected.
Accept all
Decline all
Essential
These cookies are needed to make the website work correctly. You can not disable them.
Display
Accept
Analytics
Tools used to analyze the data to measure the effectiveness of a website and to understand how it works.
Google Analytics
Accept
Decline