Widget attribute clarifications

AI-generated summary

In a discussion titled ‘Widget attribute clarifications’ under the Dev category, tshenry seeks clarification on how widget attributes are established in Discourse, how to add missing attributes like trust_level, and whether such changes can be made via theme components. eviltrout explains that widget attributes vary per widget and are typically defined in serializers — for example, post attributes are set in transform-post.js.es6. To add trust_level, one must modify the post_serializer.rb via a plugin, as theme components cannot alter serializers. tshenry thanks eviltrout for the clear guidance, confirming the information resolved their query. codinghorror had earlier suggested eviltrout might assist.

As I have been continuing down the rabbit-hole that is Discourse dev, I came to something that I could use some clarification on if possible. I could be missing something extremely obvious, but here’s where I need some help:

If I print out Object.keys(attrs) in a createWidget(), I get a list like:

id,username,avatar_template,post_count,primary_group_name,primary_group_flair_url,primary_group_flair_color,primary_group_flair_bg_color,topic

I have a few questions:

  1. How are widget attributes established? It seems like each createWidget() has its own set of attributes and I’m having trouble tracking down where they come from and how they are determined.

  2. How do I get an attribute that isn’t a part of a widget’s attrs by default? For example, in the above list, how would I add trust_level to that attribute object?

  3. Can the addition of an attribute be done in the context of a theme component or would it require a proper plugin or modifying core Discourse?

I can provide more details on the exact scenario that prompted these questions if that helps. I just wanted to keep it brief if I can.

Also worth mentioning, I’ve read the “Rendering Attributes” section of A tour of how the Widget (Virtual DOM) code in Discourse works and still haven’t quite found the answers for my specific scenario.

Sure @eviltrout can assist on this perhaps tomorrow?

There is no standard set of attributes because each widget is different. You’re most likely looking at the topic rendering, where each post receives a series of attributes. The basic post attributes are created here.

In some sub-widgets, you’ll find fewer or different attributes are passed in. You have to review the widget code to see what attributes are passed.

Your plugin would have to add that attribute to one of the serializers used to generate attributes. It’s probably going to go into the post serializer. You should have access to it via attrs.post.get('trust_level') at that point.

You need to a proper plugin to add an attribute to a serializer.

Thanks @eviltrout! That was exactly the info I was looking for. I really appreciate you taking the time to explain.