Update Topic slug


  • Plugin & Theme Dev

    @Developers

    What's the right way to update a topic slug?
    I am trying to "parse and convert" all topic titles, but I would like the slugs to match the changes.

    basically something like this

       var parse = function() { /* some custom parser function ...*/  };
       var slugify = utils.slugify; // the core NodeBB utils.slugify
    
       var newTitle = parse(oldTitle);
    
        db.setObjectField('topic:' + topic.tid, 'title', newTitle, function(err) {
            if (err) return cb(err);
            db.setObjectField('topic:' + topic.tid, 'slug', slugify(newTitle), cb);
        });
    

    This results in some weird behavior, the topic generated URLs no longer include the tid so

    /topic/1/my-topic-title changed to /topic/my-topic-title

    Screen Shot 2014-10-15 at 6.57.07 PM.png

    I am assuming there is another field that needs to be updated, but can't find it 😕

    relevant issue: https://github.com/akhoury/nodebb-plugin-import/issues/45, last few comments are the most relevant.

    Miaourt created this issue in akhoury/nodebb-plugin-import

    closed accent problem #45


  • Admin Staff

    The topic slugs include the topic id so the value stored in the database for this topic is 2647/update-topic-slug for example.

    Your code should be

    db.setObjectField('topic:' + topic.tid, 'title', newTitle, function(err) {
        if (err) return cb(err);
        db.setObjectField('topic:' + topic.tid, 'slug', topic.tid + '/' + slugify(newTitle), cb);
    });
    

  • Plugin & Theme Dev

    ah crap, i totally missed reading this line!

    thanks you @baris


Log in to reply
 

Suggested Topics

| |