Discourse Plugin API Changes: Using api.registerValueTransformer for addPostMenuButton and component:topic-list

AI-generated summary

omppatil is developing a plugin for Discourse and encountered API changes affecting addPostMenuButton and component:topic-list in version 3.2.0.beta4-dev (plugin API 0.8.7). They asked how to migrate these to use api.registerValueTransformer and which plugin API version to target for compatibility. Their existing code for both functions is shared for context. In post 2, they updated to Discourse 3.5.0.beta2-dev (plugin API 2.1.1) and repeated the question. merefield directed them to a meta topic explaining upcoming post-menu changes. In post 4, omppatil confirmed they read the topic, implemented the new API changes, and resolved the component:topic-list issue — implying progress, though the addPostMenuButton migration via registerValueTransformer remains unaddressed in the thread.

Hi, I’m using version 3.2.0.beta4-dev for local development and plugin API version 0.8.7 for plugin development. However, due to recent changes in Discourse, the addPostMenuButton and component:topic-list functions have been modified.

How can I use api.registerValueTransformer for these API functions? Additionally, which plugin API version should I use to match with 3.2.0.beta4-dev to ensure compatibility with api.registerValueTransformer?

Here’s my code for addPostMenuButton:

api.addPostMenuButton("togglePostButton", (model) => {
  const siteSettings = api.container.lookup("site-settings:main");
  const posts = siteSettings.posts;
  const array_of_posts = posts.split("|");
  const postId = model.id;
  const postIdstr = postId.toString();
  const isPost = array_of_posts.includes(postIdstr);

  return {
    action: "togglePost",
    position: "first",
    className: isPost
      ? "button.topic_hidden custom-class-hidden"
      : "button.topic_visible custom-class-visible",
    icon: isPost ? "far-eye-slash" : "far-eye",
    title: isPost ? "Hide Post" : "Show Post",
  };
});

And here’s my code for component:topic-list:

api.modifyClass("component:topic-list", {
  pluginId: "post-toggle",
  didInsertElement: function () {
    this._super();
    const topics = this.topics;
    for (let key in topics) {
      if (topics.hasOwnProperty(key)) {
        let newTopicLatest = [];
        const topic_id = topics[key].id;
        const topic_posters = topics[key].posters;

        topic_posters.forEach((poster) => {
          if (poster && poster.extras !== null && poster.extras.includes("latest")) {
            newTopicLatest = poster["description"].split(",");
          }
        });

        let userListLatest = newTopicLatest;
        if (userListLatest.length > 1 && userListLatest[0] === "Original Poster") {
          userListLatest = userListLatest.slice(1);
          userListLatest[0] = userListLatest[0].trim();
        }

        let i = 0;
        const element = $('tr[data-topic-id="' + topic_id + '"]').find(
          "a[data-user-card]"
        );

        if (element.length > 0) {
          element.each(function () {
            const poster_inside = topic_posters[i];
            if (poster_inside !== undefined && Object.keys(poster_inside).length !== 0) {
              const userElement = $(this);
              if (!userListLatest.includes(poster_inside.user.id.toString())) {
                userElement.hide();
              }
            }
            i++;
          });
        }
      }
    }
  }
});

Can anyone help me with this?

I updated my local development Discourse to version 3.5.0.beta2-dev, and the plugin API version is 2.1.1. How can I use api.registerValueTransformer for the addPostMenuButton and component:topic-list functions?

Have you read the Topic?: Upcoming post menu changes - How to prepare themes and plugins

Yes, I have read about that topic, implemented changes based on the new plugin API updates, and figured out component:topic-list as well.