Getting a list of all categories on ACP plugin settings

Plugin Development
  • How do I display in ACP plugin settings all categories so the user can select them manually, in order to then automatically post a topic in all of them (for example, using the hook filter:post.create)?

    For example, like nodebb-plugin-question-and-answer does:

    8d41a651-9f25-439a-8d0f-79eaf93119ee-image.png

  • This post is deleted!
  • Have you tried looking at what Q&A does? The source is here

  • I already have the list with all categories, but now I want the function I have in the /static/lib/main.js file to run only on the categories I selected on the plugin settings page (those settings are stored in the /library.js file).

    Can I do this or do I have to move that function to the /library.js file too?


    static/lib/main.js:

    "use strict";
    
    $(window).on('action:ajaxify.end', function(ev) {
    
        var CategoriesIds = [28,29];
    
    // I want to change this to fetch the categories selected from the plugin's settings page
    // instead of using the array above, to prevent the user having to modify the plugin files 
    // to change the ID of the categories
        if( $.inArray( parseInt(ajaxify.data.cid, 10) , CategoriesIds) != -1){
                runFunction();
         }
    
    });
    

    /library.js:

    plugin.init = function (params, callback) {
    	var app = params.router;
    	var middleware = params.middleware;
    
    	app.get('/admin/plugins/myPlugin', middleware.admin.buildHeader, renderAdmin);
    	app.get('/api/admin/plugins/myPlugin', renderAdmin);
    
    	handleSocketIO();
    
    	meta.settings.get('myPlugin', function (err, settings) {
    		if (err) {
    			return callback(err);
    		}
    
    		plugin._settings = settings;
    		callback();
    	});
    };
    
    plugin.appendConfig = function (config, callback) {
    	config['myPlugin'] = plugin._settings;
    	setImmediate(callback, null, config);
    };
    
    function renderAdmin(req, res, next) {
    	async.waterfall([
    		async.apply(db.getSortedSetRange, 'categories:cid', 0, -1),
    		function (cids, next) {
    			categories.getCategoriesFields(cids, ['cid', 'name'], next);
    		},
    	], function (err, data) {
    		if (err) {
    			return next(err);
    		}
    
    		res.render('admin/plugins/myPlugin', {
    			categories: data,
    		});
    	});
    }
    
    module.exports = plugin;
    

    Plugin config page on ACP:

    3e6c1961-df53-4578-a5c6-af078d678241-image.png

  • @jtsimoes

    It depends on what you hope to accomplish. Some things can only be done on the server, some things can only be done on the client, some things can be done on both but are easier on one or the other.

    Without knowing what runFunction() does I can't really advise.

  • I can tell u what runFunction() is, I just simplified it for a better understanding.
    Because what I just to replace is this condition if( $.inArray( parseInt(ajaxify.data.cid, 10) , CategoriesIds) != -1) instead of going search if the current category is on the array CategoriesIds above (hardcoded), I need to check if the current category is on the selection made by the user on the plugin config page.

    And I'm not realizing how I make that connection between the plugin settings and the function.

    I don't know if I'm making sense to you, but here is the repository with all the code: https://github.com/jtsimoes/nodebb-plugin-nsfw


Suggested Topics