Custom Fields for Groups?

סיכום מבוסס בינה מלאכותית

The discussion revolves around the possibility of adding custom fields to Groups in Discourse, similar to how custom fields are used for Users. JQ331 asks if this is feasible, noting it’s not currently built-in but has been requested before. merefield confirms it can be achieved via a plugin, providing a code snippet to register a custom field type for Groups. AquaL1te expresses interest in using such a feature to add a location field for mapping purposes. JQ331 references a detailed guide by @angus on adding custom fields to models, suggesting it can be adapted for Groups, and shares a personal implementation example.

Is it possible to create custom fields for Groups, just like we can add (and retrieve) custom fields for Users?

I have seen mention of this as a request here and there (like here).

If it’s not built into Discourse right now (it seems to me it’s not), is there a way to mimic this behavior?

You can do this in a plugin.

How would you do it?

eg, in plugin.rb:

Group.register_custom_field_type('my_attribute', :boolean)
Group.preloaded_custom_fields << "my_attribute" if Group.respond_to? :preloaded_custom_fields

This would be awesome indeed. I would like to add a custom location field to groups so that I can use the g.json output to generate a map based on the groups and amount of members.

I’ll experiment a bit with your suggestion. Do you also have some more info about how to use this snippet? Can it be used in a custom theme component? How would someone do that? Has anyone already used it?

To add custom fields to models (topics, categories, groups), the following topic from @angus really lays it out as well as you’re likely to find:

He has put together some sample code specific to topics and categories - I believe the code translates generally well for adding custom fields to groups as well.

This post from me on that topic lays out how I’ve used the resource that Angus has put together: