Topic List Sort By Votes in voting plugin

We have a feature request category using the voting plugin, and the Votes sort order is available in our top menu, but is not available in the “Topic List Sort By:” dropdown in the category settings. I would love to set the default sort on this to show the highest votes at the top of the list. While I understand that sometimes a chronological view makes sense, in my use case, generating valuable feedback on the most requested features helps me to get a better picture of the benefit story for my customer base. Is there any hack I could do to make it default sort to Votes and/or is this something that is planned already for a future update?

20 Likes

Great suggestion, #pr-welcome

7 Likes

Hi,

Nothing new, since then?

Not that it’s 100% guaranteed, but if nobody has posted a “I’ll give it a try” it can be relatively safe to assume that the “pr_welcome” is still open. Considering that it’s been 10 months, I say go for it.

1 Like

I’ve just had a quick look at this but it appears that the Default Topic List and Topic List Sort By dropdown options are hardcoded and not available to be extended by plugins.

What are the valid/desired approaches here?

  1. Add a plugin-specific option to the hardcoded list in core
  2. Add extension points that allow plugins to add options to default topic and topic list sort dropdowns

Option 1 would obviously be the quickest to implement but I have no idea if that approach would be accepted in core (/cc @sam).

Has option 2 been proposed before? If so are any specs available?

2 Likes

Another potential option is allowing redirects for top-level category URLs. Eg:

https://forum.ghost.org/c/Ideas
->
https://forum.ghost.org/c/Ideas/l/votes

That way the majority of links to the topic will end up on the “most-voted first” list whilst still allowing the “Latest” and “Top” menu items to work as before.

2 Likes

In general I much prefer (2) a more extensible core opens the door to other plugins here which is desirable.

4 Likes

Is there a solution yet to sort by number of votes?

If you have no problems in getting your hands a little dirty, you can edit ./app/assets/javascripts/discourse/components/edit-category-settings.js.es6 and add “votes” to the array described in function availableSorts, like so:

  @computed
  availableSorts() {
    return [
      "likes",
      "op_likes",
      "views",
      "posts",
      "activity",
      "posters",
      "category",
      "created",
      "views",
    ]
      .map(s => ({ name: I18n.t("category.sort_options." + s), value: s }))
      .sort((a, b) => {
        return a.name > b.name;
      });
  },

Now, you should see [[category.sort_options.voting]] in settings.

8 Likes

can be this included in the plugin?

Currently, no. The plugin API does not offer this functionality yet. It will soon. :wink:

8 Likes

Is this sort by votes available yet? It’s a must have for me to use this plugin.

+1, looking for solutions similar to this one

1 Like

Here’s my +1 for this as well!

1 Like

Could this be done in a theme?

Edit: (I infer that’s a “no.”)

How hard will it be to get this added to the theme? I have a client who really, really, really, wants this, but also has a really limited budget.

You can override computed properties in a theme by using api.modifyClass. Something like this should work. You’d need to test that the changes don’t break anything:

<script type="text/discourse-plugin" version="0.8.25">
  api.modifyClass('component:edit-category-settings', {
    availableSorts: Ember.computed(function() {
    let sorts = this._super();
    console.log('sorts', sorts);
    // Add a votes object to the sorts array. Then...
    return sorts;
    })
  });
</script>
7 Likes

Thanks, Simon. I always appreciate your patience.

I add

<script type="text/discourse-plugin" version="0.8.25">
  console.log('something is sorting, batman!')
  api.modifyClass('component:edit-category-settings', {
    availableSorts: Ember.computed(function() {
    let sorts = this._super();
 
    console.log('sorts', sorts);
    // Add a votes object to the sorts array. Then...

    return sorts;
    })
  });
</script>

to </head>. I see something is sorting, batman!’ in the console log, but not sorts.

I think what’s supposed to happen is that I’d see what’s in the sorts array so that I could add stuff to it.

Try this, but make sure you test it:

 api.modifyClass('component:edit-category-settings', {
    availableSorts: Ember.computed(function() {
    let sorts = this._super();
    let voteSort = {};
    voteSort["name"] = "Votes";
    voteSort["value"] = "votes";
    sorts.push(voteSort);
    return sorts;
    })
  });

If you put a console.log statement in that function, you will only see its output when you open the category edit modal. You’ll also probably have to select ‘Preserve Log’ on your develper tools console to see anything.

6 Likes

Ah! Of course! As I said before, I really appreciate your patience. I can see that I’m getting closer, but don’t have time to finish it now.

Thanks again! I’ll update in my morning.

1 Like

Yup! That did it! Thanks so very much. And slowly, after fighting for over 20 years, I start to learn Javascript.

Thanks again, @simon. I really appreciate your help.

1 Like