Topic ID as a variable?

Is there a way to get the topic ID as a variable within a topic?

Use case, we have categories for support and bug tracking tickets. I’d like to set them up with templates like so.
This:

> Do **not** remove this line: Support request #$(topicid)

Looks like this:

Do not remove this line: Support request #246

Open to other ways to get the info. Just not sure how to do this.

1 Like

You’re trying to put the topic ID in the raw text of the topic?

What problem is that going to solve?

1 Like

It will solve the problem of me telling my support staff that they need to first create a new topic, then save it, then look at the URL and get the ID #, then go back and edit the topic in order to put the # in the body manually.

What are you using the topic ID# in the body of the OP for in your use case here?

1 Like

A unique id # for a ticket/bug.

Where is that ticket/bug going? The ID is in the URL; there it is persistent and never wrong. How does it help to have the topic ID in the text of the topic? And, as you point out, if it’s in the body of the message, anyone can change, delete, or type it wrong.

Is something else using the topic ID? If so, what and how?

1 Like

It will be easier to create a custom component that appends this to the correct posts rather than filling a template.

If you edit the code of your theme under admin > themes & components… and add this to the JS tab, it should get you what you’re after with some adjustment:

import { apiInitializer } from "discourse/lib/api";
import Component from "@glimmer/component";

class TopicIdentifier extends Component {
  get topicId() {
    return this.args.post?.topic?.id;
  }

  get shouldShow() {
    const firstPost = this.args.post?.post_number === 1; // first post in topic
    const desiredCategories = [45, 3]; // a comma separated list of category IDs you want this to appear in 
    const isInCategory = desiredCategories.includes(
      this.args.post?.topic.category.id
    );

    return firstPost && isInCategory;
  }

  <template>
    {{#if this.shouldShow}}
      <!-- you can edit the content below, {{this.topicId}} is where the topic ID will be filled -->
      This topic's ID is
      {{this.topicId}} 
      <!-- you can edit the content above -->
    {{/if}}
  </template>
}

export default apiInitializer((api) => {
  api.decorateCookedElement((element, helper) => {
    const wrapper = document.createElement("div");
    wrapper.className = "custom-topic-id"
 
    helper?.renderGlimmer(
      wrapper,
      <template><TopicIdentifier @post={{helper.model}} /></template>
    );

    element.appendChild(wrapper);
  });
});

You can find the category ID from its URL to configure desiredCategories, for example, the support category here on Meta has the ID of 6

You can add CSS to style this how you’d like with the CSS tab… for example

.custom-topic-id {
  display: inline-block;
  background: yellow;
}

3 Likes

Yes, it can be typed wrong. Hence why if you use a variable you don’t have to worry about typing it wrong.

It helps because I’m the user and that’s how I want my workflow set up.

You cannot currently search Discourse by topic id. If the # is in the body of the topic, then it’s now part of the search results.

So I can tell a coworker, 'Hey search for bug #138 and let me know what you think."

Then it’s really easy for Nancy to just go to the site and do a search for 138 and get a result.

2 Likes

You don’t need to search:

/t/-/id – but really /t/id works.

They can just type /t/138 and not have to search. But if you are talking to a person, wouldn’t saying "search for bug about “some words they know” make more sense to use in conversation than a number?

And that was going to be my other suggestion.

I think I’d have used renderInOutlet rather than decorateCookedElement, but that’s probably because I don’t fully understand decorateCookedElement.

But this will don’t let you search for a bug/topic by topic_id.

1 Like

I will test this out. Thanks for the info. Is that part of the body that becomes searchable?

But I want to search. It’s easier.
Go to site, then append /t/id to the URL at the top of the window VS go to site and type ID into the search field that’s right in the center of the page.

If I’m talking to a person, or typing a message, yes a number is more concise and less error prone than “Search for exact title that might be 5 words long”

I will look into that, but if it kills being able to use in a search field, then it won’t work for me.

I really love Discourse, and I have gotten a TON of useful feedback and help from everyone here.
What I don’t understand is that occasionally folks tell me that “You shouldn’t do it that way because this way is better.” Well, no, this way is better for you, but that way is better for me. And neither is wrong.

3 Likes

Yeah don’t bother then, unfortunately it only appends content to the topic when it’s rendered… it doesn’t add it to the database to make it searchable. I think what you’d ultimately need is a custom plugin or a custom script added to Discourse Automation for it to be fully automatic.

Searching by topic ID or including it in a template isn’t a common request, so it’s not something we support out of the box.

I think it’s that regulars here know it’s not a feature we currently have, so we’re trying to support you by figuring out some potential work-arounds.

2 Likes

And often if you go back you can find a better solution.

If you really want to embed them topic id in the text so you can find it in search then you’d need a plugin and it could add ticket123 to the raw text (I think search looks at raw?). The topic id doesn’t exist before you create the topc, so you can’t insert it when you create it.

Oh! Maybe what you want is a plugin that would add a tag bug123 when a topic in a bug category gets created. Then you could refer to it as #bug123 and have it link anywhere you mention it and be able to search. If you’re self hosted or on enterprise that wouldn’t be too hard. I imagine someone in Marketplace would do it for $250 to $500.

Or, if you want to be able to “search” for a bug by number, you could add a theme component that let you type it in a ‘bug box’ and go straight there. Maybe that’s a solution that would work and it wouldn’t require a plugin.

1 Like

What I ended up with is a more discreet showing of the topic ID. It’s still not searchable, but I’ll live with it until what I really need becomes a reality.

<script type="text/discourse-plugin" version="0.5">
api.decorateWidget('post-meta-data:after', dec => {
  if (dec.attrs.post_number === 1) {
    const topic = dec.getModel().get('topic');
    return dec.rawHtml(`<div class="post-info topic-id"><b>&nbsp#${topic.get('id')}</b></div>`);
  }
});
</script>

Problem is that this code is now outdated and I don’t know how to fix it.
What should it be updated to in order to be compliant?

I created GitHub - literatecomputing/discourse-custom-components as an example of how to do that.

Is a trivial example of inserting a component. So you would add some code before <template> to get the topic id and logic for whether to display.

I think you might want topic-list-before-link as the plugin outlet.

I don’t understand any of that. I’m not a coder.
The code I posted does this:


How would I do that with your code?

If I’m not wrong, inputting topic:id into the searchbar will show the topic in the results.

E.g. searching topic:374168 this brings up posts from this topic only.

I wonder… would a TC that sends an ajax request to update the topic’s raw suffice?

1 Like

That only works for this topic, try a different random one. Try searching for “one two four two four zero” that’s the tag banners topic ID.

You mean topic:124240? The filler word (‘hello’) was just an example. In the case of Tag Banners, ‘hi’ works. Maybe if you have a common word across support tickets, or just a common word in general, you can try that? Just suggesting a workaround :slight_smile: .

Did you try the code in Topic ID as a variable? - #7 by awesomerobot

Also, if what you want to do is “search” for a topic id, then you could probably make a component that accepted a topic ID and then just jumped to that topic.

1 Like