Category custom field value strage behaviour in category edit form

AI-generated summary

The discussion revolves around a plugin development issue in Discourse where a custom field checkbox in the category settings behaves incorrectly when unchecked. Janno_Liivak explains that although the database value is set to “false”, the checkbox remains checked upon reopening the settings. The issue appears to stem from the computed property handling the custom field value. Alavi1412 asks about the correct method to add a custom field to a category, referencing two approaches: using plugin.rb or modifying the Category model directly. Stranik shares their working implementation, confirming the approach works but expressing difficulty in understanding how to use the field further. Janno_Liivak shares a link to their completed plugin, which may offer additional insights.

I’m about to create a plugin adding a checkbox to Category settings. All is fine with the field until I uncheck the field. In database I can see that value of custom field is set to “false” but as I reopen category settings the checkbox is now checked although the value is still false. Is there something I’m missing?

Here’s the custom field…

~/bump-topic/assets/javascripts/discourse/pre-initializers/extend-for-bumping.js.es6

import Category from 'discourse/models/category';
import computed from 'ember-addons/ember-computed-decorators';

export default {
  name: 'extend-for-bumping',
  before: 'inject-discourse-objects',
  initialize() {

    Category.reopen({

      @computed('custom_fields.enable_bump_topic')
      enable_bump_topic: {
        get(value) {
          return value === "true";
        },
        set(value) {
          value = value ? "true" : "false";
          this.set("custom_fields.enable_bump_topic", value);
          return value;
        }
      }
    });
  }
};

And the interface part…

~/bump-topic/assets/javascripts/discourse/templates/connectors/category-custom-settings/bump-topic.hbs

{{#if this.siteSettings.bump_topic_enabled}}
<section class='field'>
  <div class="enable-bump-topic">
    <label class="checkbox-label">
      {{input type="checkbox" checked=category.custom_fields.enable_bump_topic}}
      {{i18n 'bump_topic.enable_bump_topic'}}
    </label>
  </div>
</section>
{{/if}}

I have a question.
what should we do for adding a custom field to a category?
1.Using plugin.rb:
Category.register_custom_field_type('tracking_number', :integer)
2.using the code above.
Which one should we use?

Began writing the plugin, used the 2 files and they work.
The hardest part for me to understand how this field should be used further.

import property from 'ember-addons/ember-computed-decorators';
import Category from 'discourse/models/category';

export default {
  name: 'extend-category-for-temp',
  before: 'inject-discourse-objects',
  initialize() {

    Category.reopen({

      @property('custom_fields.enable_topic_temp')
      enable_topic_temp: {
        get(enableField) {
          return enableField === "true";
        },
        set(value) {
          value = value ? "true" : "false";
          this.set("custom_fields.enable_topic_temp", value);
          return value;
        }
      }

    });
  }
};

and

<section class='field'>
  <div class="enable-topic-temp">
    <label class="checkbox-label">
      {{input type="checkbox" checked=category.enable_topic_temp}}
      {{i18n 'temp.allow_temp'}}
    </label>
  </div>
</section>

I’m not sure what’s a question here? … In case you would like a showcase, I managed to complete my plugin and it’s available at:

https://github.com/jannolii/discourse-bump-topic

Maybe you can get ideas from my code :slight_smile: