Create multiple custom fields for a topic

Hello,

I was wondering if anyone has created multiple custom fields for a topic? I was able to use this plugin successfully to create one custom field: GitHub - pavilionedu/discourse-topic-custom-fields: Discourse plugin showing how to add custom fields to Discourse topics

However, I am having trouble creating multiple custom fields. Does anyone have a working code example that would be willing to sell or share with me? I can modify it to meet my needs but just need a little help getting started. The 2 new fields I would need would be price (string) and url (string). I would love to also add an image upload field to the topic but have yet to see anything like that.

Thanks!

Hello,
I would be happy to help you. I recently created a plugin where I utilized topic custom field (Discourse Chain Topics Plugin).

In my plugin I created an extra two fields for each topic of type of integer. Similarly you could add as many fields as you want. In the end topic/user custom fields, is a JSON object where each field you add will be then be as a key/vale in the JSON obeject . where key is the field name and value is the value.

The example you brough above (which also helped me as well) is built such that you add extra field in admin panel, where you choose its type and name. However you don’t necessarly have to relate the extra fields to admin panel. Simliary you could add as many fields as you want (Not suer how this might affect the performace).

Lastly feel free to ping me/ message for any support you need. You can also find my conact details at https://ghassan.blog
Which you best of luck.

3 Likes

Hi, i was having the same problem… now i figured out how to accomplish with multiple custom fields!
beyond setup the plugin.rb you will also need to have individual files (js initializer and the .hbs and .hbr templates) for each field!

the discourse-topic-custom-fields plugin have a folder called

plugins/discourse-topic-custom-fields/assets/javascripts/discourse/initializers

in this folder you have a file called: topic-custom-field-initializer.js.es6

for a single field you can just edit this file, but for multiple fields you should have other files like this,

topic-custom-field-url-initializer.js.es6 and topic-custom-field-price-initializer.js.es6

inside each file you will have 3 lines with a variable api calling a method registerConnectorClass, you need to change the second parameter and add a suffix to have an exclusive name for each field class on each of the 3 lines

dont forget to change the exports name in each file (right after the “exports default {…”).

also, for the hbs and hbr files, you will need to create separated files for each field too, you just need to copy and paste the same files and just add a suffix for each file name, eg.:

image

1 Like