Need to add tag when any of 4 other tags exist on topic

Hey all,

I’ve done some restructuring on my forum recently and started making use of tags.

One thing i’m struggling with (i know it’s not native, but perhaps a simple plugin exists?) is when a new topic is created with the tag “sync1” “sync2” “sync3” or “sync4” i need to also append the tag “apim” does any one know how i might do this?

My ruby skill is lacking unfortunately.
I’ve seen both

and

but i’m unsure how i can adapt this to simply check if any of those 4 tags are on the topic instead of the checks those examples currently have

Basically, if any of the tags in Pink exist in the topic, the Green tag should also be added automatically

DiscourseEvent.on(:topic_created) do |topic|
  guardian = Guardian.new(Discourse.system_user)
  required_tag_names = [”sync1”, “sync2”, “sync3”, “sync4”]
  DiscourseTagging.tag_topic_by_names(topic, guardian, ["apim"], append: true) if topic.tags.exists?(name: required_tag_names)
end

You need this :arrow_up: code.

Edit: You can also enforce it by adding a tag group and setting “apim” as the parent tag. In that case, users can choose “sync” tags only after selecting the parent tag “apim”.


I think it will be more useful if we allow users to select child tags first then we add the parent tag automatically :person_shrugging: Most end users won’t know about the parent tag requirements.

2 Likes

Excellent, usually i try to figure these things out my self but ruby isn’t my speciality and i had a feeling someone would have a example in a matter of minutes :slight_smile:

Now i just need to figure out how to add that to a plugin, but i’m sure it’s quite simple.

I agree that sometimes selecting a child tag that adds a parent could be a preferred outcome.

Thanks for your help.

EDIT: plugin created and working as intended, thanks so much for your help!

1 Like