Questions about category custom fields


(Dakota) #1

hello!

i’m working on creating a simple plugin that allows a category to be assigned a font awesome icon. The icon will display instead of an image if one is not set.

I’m not really interested in doing it with a theme-specific hack like :before CSS because I’d like it to be something settable in the category settings panel - indeed I’ve already got that part working. Here’s my code so far:

plugin.rb

# name: category-icons
# about: Allows fa-icons to be used as category graphics.
# version: 0.1
# authors: Flower_Child
# url: https://github.com/tipsypastels/category-icons

after_initialize do
  Category.register_custom_field_type('fa_icon', :string)

  add_to_serializer(:basic_category, :fa_icon) do
    object.custom_fields['fa_icon']
  end
end

assets/javascripts/discourse/connectors/category-custom-settings/fa-icon-settings.js.es6

export default {
  setupComponent(args, component) {
    const category = args.category;

    if (!category.custom_fields) {
      category.custom_fields = {};
    }

    if (typeof args.category.custom_fields['fa_icon'] !== 'string') {
      args.category.custom_fields['fa_icon'] = '';
    }
  }
}

assets/javascripts/discourse/connectors/category-custom-settings/fa-icon-settings.hbs

<section class="field">
  <label>
    {{i18n 'fa_icon_categories.fa_icon'}}
    {{input type="text" value=category.custom_fields.fa_icon}}
  </label>
</section>

Those are all the files I have so far - I’ve tried adding more based on things I’ve seen in other plugins but haven’t made any headway. This bit works in the sense of adding a custom field to the category settings, and the value I enter into it persists, but I can’t actually make it display on the categories list page. Using {{category.fa_icon}} or {{category.custom_fields.fa_icon}} in a theme-specific customization to categories-only.hbs or other templates is just blank.

Does it take more than this to be able to use custom fields in a template? I’m not sure what I’m missing. Is there any resource for how to use these in detail?


(Dakota) #2

:frowning: :frowning: :frowning:
no one have any ideas? i havent made much progress. idk how the people who do make plugins are figuring stuff like this out when there’s no documentation on it.