How to add custom field to topic?

I tried to add custom field to Topic but doing it the same way as for category gives me error:

Property set failed: object in path "custom_fields" could not be found or was destroyed.

Example of how it works successfully for category:

https://github.com/jannolii/discourse-bump-topic/blob/master/assets/javascripts/discourse/pre-initializers/extend-category-for-bump-topics.js.es6#L9-L23

Suggestions, anyone?

إعجاب واحد (1)

I’m really stuck here :frowning:
I just don’t get it … what do I have to add to plugin.rb in order my plugin to have access to custom fields? As I understood, topics should already have custom field functionality by default.

I have code in pre-initializer…

    Topic.reopen({

      @property('custom_fields.bumped_at_with_button')
      bumped_at_with_button: {
        get(date_value) {
          return new Date(date_value);
        },
        set(value) {
          this.set('custom_fields.bumped_at_by_button', (value ? value.toISOString() : ''));
          return value;
        }
      }

    });

I have following section in plugin.rb…

add_to_serializer(:topic_view, :bumped_at_with_button, false) {
  object.topic.custom_fields['bumped_at_with_button']
}

But when setting value with…

topic.set('custom_fields.bumped_at_with_button')

… I get the message I pointed out in topic :frowning:

Help… anybody?

إعجاب واحد (1)

Hello @Janno_Liivak,

As far as I know, TopicViewSerializer does not include a custom_fields field by default. With your call to add_to_serializer you could access directly to a field called bumped_at_with_button. However, If you want to access to that field through a custom_fields field, then I think you should change your code to something like:

add_to_serializer(:topic_view, :custom_fields, false) {
  object.topic.custom_fields.slice(:bumped_at_with_button)
}
إعجابَين (2)

Thanks. It worked. But still … the value in setter function is always null. Could you point me in a right direction with

Topic.update(topic, props);

How should custom_fields.bumped_at_with_button be passed to props in order to be saved?

I’ve tried something, but it doesn’t work :frowning: In following code only bumped_at get’s saved to database… and sadly no new record in topic_custom_fields table.

import Topic from 'discourse/models/topic';

export default {
  actions: {
    clickButton(topic) {
      const current_date = new Date();
      topic.set('bumped_at', current_date);
      topic.set('custom_fields.bumped_at_with_button', current_date);
      const props = topic.getProperties('bumped_at', 'custom_fields');
      Topic.update(topic, props);
    }
  }
};
إعجاب واحد (1)

Hey @Janno_Liivak

Did you find how to do it? I’m looking to the same at the moment

Thanks!

Yep. But as it was a long time ago don’t remember exactly what I did. I managed to build two plugins eventually. Maybe you can find your answers there?

Second one isn’t here but you can find it on github.

إعجاب واحد (1)

Please note that sensitive info may be stored in topic custom fields - so don’t just dump them all out. Plugins should be selecting out just the fields they’re using.