Getting tags from post in plugin 'addPostTransformCallback(...)' function?


(Anton Struyk) #1

I’m working on a plugin that will transform the output of replies to a topic if there’s a particular tag on the topic itself. I’m doing my mangling in a callback I registered using something like:

api.addPostTransformCallback((transformedPost) => {
/* Do stuff to mangle transformedPost.cooked */
});

However, I’m having some trouble getting access to the tags that have been applied to the topic that the post is related to. Currently I’m using

api.includePostAttributes('topic');

in the same place I register my callback to include the topic object in the list of attributes from the original post that was passed into transform-post.js.es6 :: transformPost(...). That lets me do something like:

api.addPostTransformCallback((transformedPost) => {
    if (transformedPost.topic.tags.length > 0) {
        /* Do stuff */
    }
});

All I’m accessing from that is the topic.tags property. It feels really wrong.

I know the object passed into my callback has access to a topicId property, but I’m not sure how to take that and get the list of tags for the topic.

Is there a better way?


#2

I’m working on something similar that applies to any topic with a specific tag. I haven’t found a way to do it with the api at this point, but there is a CSS class for each tag that you can use to make sure you are in a topic with that particular tag:

$('.tag-tagname').length

In my scenario, tagname will be something an admin can set in a setting.


(Anton Struyk) #3

That sounds a lot like my scenario. Basically I’m making a plugin to hide things based on whether they have a tag or not:

I’ll take a look at searching for the CSS, though that also seems kind of hacky. It seems like it’d be pretty easy to add the tags to the transformed post object (in transform-post.js.es6 :: transformPost(...)) – it seems like there’s lots of topic data there already. That would make this a lot less hacky :slight_smile:


#4

I agree, it would be great to have the tags available directly through that api call.

Edit: Yeah, those look like the two options I can find for now. I’m not sure how big of an issue the “hackiness” would be in either solution, but you could always create a #feature request or possibly try submitting a PR that adds tags to the transformed post object.


(Anton Struyk) #5

I submitted a PR to include the tags under a topicTags attribute. We’ll see if they approve it.


(Anton Struyk) #6

FYI this doesn’t work correctly because sometimes the ’ Suggested Topics’ element that is displayed after the post+replies contains a tag that isn’t actually on the topic you’re looking at. That means it’ll incorrectly find the tag from the suggested topic instead of the one from the topic you’re actually looking at.


#7

Very good call! A more specific selector should solve that, but it’s still not as ideal as having the tags accessible from the transformed post object. Hopefully that PR goes through.


(Anton Struyk) #8

I may or may not have run into that bug because I tried implementing it that way :slight_smile:


#9

$('.topic-header-extra .tag-tagname').length should be specific enough if you wanted to mess around with this method more. Once again, great job picking up on the suggested topics coming into play :+1: