Getting Category's custom field value from Topic

AI-generated summary

Janno_Liivak is developing a plugin to access a custom category field (enable_bump_topic) from a Topic model. They report that the custom_fields property returns undefined when navigating directly to a Topic, only working correctly after visiting the Category’s edit form first, suggesting the data is not loaded by default. Falco advises that the custom field must be explicitly added to the serializer for the relevant page or fetched via an additional API request. When Janno_Liivak requests an example of adding a field to a serializer, Falco provides a reference to the discourse-signatures plugin code, explaining that they need to implement a similar modification for the topic serializer. Janno_Liivak confirms this guidance helps them progress with the implementation.

I have created custom category field enable_bump_topic (by example of @SamSaffron’s discourse-solved plugin). But I don’t seem to get how could I access this custom field when on Topic. I created canBump computed property for Topic (discourse-bump-topic/assets/javascripts/discourse/initializers/extend-topic-for-bump-button.js.es6 at master · jannolii/discourse-bump-topic · GitHub) but I only get value out from Category’s custom field when I first visit Category and open Edit form and then go to this Categroy’s topic. If I go directly to Topic I get undefined when try to access custom_fields. It’s like custom_fields doesn’t exist before I go and look at the Category settings.

What am I missing? What am I not getting?

My (still very raw) project: GitHub - jannolii/discourse-bump-topic · GitHub

You need to add it to the serializer of the page you want it to be, or make an additional request to an endpoint that will give you this value.

Thanks. Could there be an example where custom field is being added to a serializer? Quick scan didn’t reveal such thing being used in some popular plugins

Here code it’s adding a new field to the post serializer. You need something similar, but adding a new field to the topic serializer I think.

Thanks a lot! Now I’m starting to get somewhere :wink: