Modifying Hooks Behavior (filter:category.topics.prepare)
-
I am using the hook
filter:category.topics.prepare
to get alltids
under certain category. I need to filter thistids
based on certain condition, because I don't want to load the whole topics under the category. What is the best approach to do this?This is piece of code that I currently use (copied from
src/categories/topics.js
) :let results = await plugins.hooks.fire('filter:category.topics.prepare', data); const tids = await Categories.getTopicIds(results); let topicsData = await topics.getTopicsByTids(tids, data.uid);
Thanks in advance.
-
-
Maybe you can use
'filter:categories.getTopicIds'
and implement your custom logic to retrieve tids. https://github.com/NodeBB/NodeBB/blob/master/src/categories/topics.js#L47-L57You would have to copy some code from core to preserve the default behaviour though.
-
-
You would implement it in your plugin in the hook
filter:categories.getTopicIds
. That hook is passed alot of data that you can use.if (plugins.hooks.hasListeners('filter:categories.getTopicIds')) { const result = await plugins.hooks.fire('filter:categories.getTopicIds', { tids: [], data: data, pinnedTids: pinnedTidsOnPage, allPinnedTids: pinnedTids, totalPinnedCount: totalPinnedCount, normalTidsToGet: normalTidsToGet, }); return result && result.tids; }
In that hook you can get the tids you need and set them too
hookData.tids = ...
or use the core code to provide the default behaviour. -
@baris Alright. Clear. Thanks a lot!
-