Thanks Simon!
I’ve been testing it this morning, and added back in a few of the features we need (like an exclude list). I also moved the query to a variable so we didn’t need to keep typing it. My code is below.
It doesn’t it solves the underlying problem for us.
One thing I noticed while testing is that the query doesn’t work on the base url (for us) because we have our site setup to only display categories on the home page.
Which means that when we try to append the tag filter to the base url it doesn’t return any results.
https://domainname/?max_posts=5&tags=prompt-1
vs. https://meta.discourse.org/?max_posts=5&tag=pr-welcome
Is there a way around this without either a) changing our homepage display or b) hardcoding the category?
- (hardcoding the category actually does work for us because we only care about tags in one category, I’m just trying to avoid it as a best practice)
Is there a reason the tags route doesn’t respect the max_posts filter?
Thanks again, I really appreciate the help.
<!-- POSTS THAT NEED LOVE -->
<script type="text/discourse-plugin" version="0.8.18">
if (I18n.translations.en) {
I18n.translations.en.js.filters.needsreply = {title: "Posts That Need Love", help: "Topics that have not be answered"};
}
// CUSTOMIZE THESE VALUES
// Set the search query
let search_query = '?ascending=true&order=posts&max_posts=5';
// Exclude certain categories from showing needs_reply
let excludeList = ['process', 'resources', 'prompts', 'staff', 'unpublished-prompts'];
// Add active class to Needs Reply button
api.modifyClass('component:navigation-item', {
active: Ember.computed("contentFilterMode", "filterMode", function () {
let contentFilterMode = this.get('content').get('filterMode');
if (window.location.search && window.location.search.split('&')[0] === search_query.split('&')[0]) {
return contentFilterMode === "needsreply";
} else {
return this._super(contentFilterMode, this.get('filterMode'));
}
}),
});
// Remove max_posts filter and tags filter
api.modifyClass('controller:discovery/topics', {
resetParams: function () {
this.setProperties({max_posts: null});
this.setProperties({tags: null});
this._super();
}
});
Discourse.ExternalNavItem = Discourse.NavItem.extend({
href: function () {
return this.get('href');
}.property("href")
});
Discourse.NavItem.reopenClass({
buildList: function (category, args) {
let list = this._super(category, args),
tag = args.tagId,
needsreplyHref;
if (!category) { // does not have a category
if (args.tagId) { // has a tag
needsreplyHref = '/c/student-work' + search_query + '&tags=' + tag; // build the href with the tag.
} else {
return list; // return the list without creating the custom nav item because the query doesn't work on the base url of a Discourse install
}
}
else if (excludeList.indexOf(category.slug) != -1) { // the category is in the exclude list, do nothing
return list;
}
else if (!category.parentCategory) { // is not a sub-category
needsreplyHref = tag ? '/c/' + category.slug + search_query + '&tags=' + tag : '/c/' + category.slug + search_query; // if there's a tag build the href with tags and the category, else fallback to the search query et al
} else { // is a sub-category
needsreplyHref = tag ? '/c/' + category.parentCategory.slug + '/' + category.slug + search_query + '&tags=' + tag :
'/c/' + category.parentCategory.slug + '/' + category.slug + search_query; // if there's a tag build the href with tags, sub-cateogry, and category, else fallback to search query et al
}
list.push(Discourse.ExternalNavItem.create({href: needsreplyHref, name: 'needsreply'}));
return list;
}
});
</script>
edit: fixed the code to clear and add the active class based on the search query variable