Modifying Hooks Behavior (filter:category.topics.prepare)

Moved Solved Technical Support
  • I am using the hook filter:category.topics.prepare to get all tids under certain category. I need to filter this tids 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.

  • nullpointerN nullpointer marked this topic as a question
  • 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-L57

    You would have to copy some code from core to preserve the default behaviour though.

  • julianJ julian moved this topic from NodeBB Development
  • @baris where exactly should I implement my custom logic? Is it after firing the hook and filtering the hook result, or somewhere else?

  • 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!

  • nullpointerN nullpointer has marked this topic as solved


Suggested Topics