HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.
Please upgrade here. These earlier versions are no longer being updated and have security issues.

Sort Some Categories Aphabetically

Does anyone know how to sort a specific category by Title alphabetically instead of by date?

I've found other discussions where you can sort all discussion categories alphabetically, but I'd like to only apply this sorting to one or two categories. I'd like to use these categories more as a table of contents and not as a discussion category. Thanks ya'll!

Answers

  • hgtonighthgtonight ∞ · New Moderator

    I don't think I have seen you around before, hi!

    What version number of Vanilla are you running?

    You would have to do the filtering on a per category basis then. This sounds like you would need to make a plugin that hooks into the discussion model with something like this:

    public function DiscussionModel_BeforeGet_Handler($Sender) {
      if(in_array('d.CategoryID', $Sender->EventArguments['Wheres'])) {
        $CategoryID = $Sender->EventArguments['Wheres']['d.CategoryID'];
        if($CategoryID == C('Vanilla.TableOfContents.CategoryID')) {
          $Sender->EventArguments['SortField'] = 'd.Name';
          $Sender->EventArguments['SortDirection'] = 'asc';
        }
      }
    }
    

    Then put $Configuration['Vanilla']['TableOfContents']['CategoryID'] = 3; in your /conf/config.php file. Be sure to change 3 to the category ID you want to be alphabetized.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • Hi! Thanks for the welcome hgtonight!

    I'm running the latest version 2.0.18.10 currently.

    Making a plugin to do this sounds daunting for my skills, but I'll give it a shot. Thanks for throwing that code together and linking to the plugin creation page. I'll check back with my results.

    hgtonight
  • Well I can't seem to get this to work. This plugin doesn't seem to make a change. Here is what I'm trying to use now:

    1. class CategorySorter extends Gdn_Plugin {
    2. public function DiscussionModel_BeforeGet_Handler($Sender) {
    3. if(in_array('d.CategoryID', $Sender->EventArguments['Wheres'])) {
    4. $CategoryID = $Sender->EventArguments['Wheres']['d.CategoryID'];
    5. if($CategoryID == C('Vanilla.Test.CategoryID')) {
    6. $Sender->EventArguments['SortField'] = 'd.Name';
    7. $Sender->EventArguments['SortDirection'] = 'asc';
    8. }
    9. }
    10. }
    11. }

    And here is the line from my config.php when using the discussion name of 'Test'.

    $Configuration['Vanilla']['Test']['CategoryID'] = 22;

    Making a plugin is easier that I could have expected. Thanks for the direction @hgtonight

  • hgtonighthgtonight ∞ · New Moderator

    Did you comment out the lines noted in that linked discussion?

    From: http://vanillaforums.org/discussion/comment/133654/#Comment_133654

    Oh right, forgot that you also need to edit \applications\vanilla\models\class.discussionmodel.php.

    You have to comment out line 148 & line 149 like this:

    //    if (!in_array($SortField, array('d.DateLastComment', 'd.DateInserted')))
    //        $SortField = 'd.DateLastComment';
    

    It should work then.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • I didn't comment that out originally, but I just commented that out and tested.

    It is line 177 and 178 of the \applications\vanilla\models\class.discussionmodel.php that I commented out in the newer version that I'm running. Doing so didn't seem to help with the sorting of a specific category.

    Thanks again for the direction!

Sign In or Register to comment.