This is a collection of education plugins that demonstrate how to add a custom field to different models in Discourse. They are intended as learning tools for those looking to learn how to build discourse plugins.
You could use these plugins just to add a custom fields on your Discourse instance, however you would still need to change the code slightly to do so. It’s not designed for plugin-and-play use on a live server.
How they work
As well as containing working code, each plugin contains a step-by-step description of what the code is doing in the form of comments. For example
# type: step
# number: 1
# title: Register the field
# description: Where we tell discourse what kind of field we're adding.
# You can register a string, integer, boolean or json field.
# references: lib/plugins/instance.rb,
Hopefully the steps and comments are self explanatory. The references are there to show you where to look if you want to learn more.
Let me know if you find it useful, if something isn’t working, or the notes are unclear
Thank for build plugin cho our,
i have this error later install plugin:
The software powering this discussion forum encountered an unexpected problem. We apologize for the inconvenience.
Detailed information about the error was logged, and an automatic notification generated. We’ll take a look at it.
No further action is necessary. However, if the error condition persists, you can provide additional detail, including steps to reproduce the error, by posting a discussion topic in the site’s feedback category.
I read, but i can’t catch it. Can you spending time make a video tutorial install this plugin from make local development environment to step by step add a custom field …
So difficult to access this plugin.
If possible, can you upgrade search feature for field type is number?
This resource that @angus has put together is even more helpful than I first realized.
Not only is the code for adding a custom field there with clear explanations, but a lot of it can be plugged directly into any plugin, because the code mostly uses variables like FIELD_NAME and FIELD_VALUE, which you can define in plugin/config/settings.yml (you also need to make sure that your plugin file structure is the same as in the github code @angus has provided). Going through the code has also given me a greater understanding of some discourse functions and methods I have seen before, but never really understood until now.
So far, the code works great to create and save topic custom fields. There are two questions that have been coming up for me:
Topic List Error: It seems to throw an error in the case I try to load a category list (ie, a topic list for a category) where there are topics created prior to adding the custom field. It shows the exception page, and lists this error: Attempting to access a non preloaded custom field, this is disallowed to prevent N+1 queries. What’s the recommended way to solve this one?
Is there a way for me to apply the custom field only for topics in certain categories? So, let’s say I have Category 1, Category 2, and Category 3, and I only want the custom field input to show up and only want the field to be saved if the topic is part of Category 3. Is there a way to do that?
We (Pavilion) will be doing something like that in the future, but for now it’s just the code and the steps. If you’re stuck on a specific issue, create a post in #dev and describe the issue in some detail.
I’ve added a sub-step to the topic custom fields plugin demonstrating how to preload fields if you’re using them in the topic list.
You’ll need a category custom field to identify categories it should be displayed in. I’ve given category custom fields the same treatment in this plugin, complete with the same step-by-step instructions:
Combining these two education plugins won’t take you all the way to your goal, but see if you can make it from there.
A video is always nice–I’m all for making things as straightforward as possible, but I don’t think it’s required to get the key value from these resources that @angus has put together. These resources give the code that you need to accomplish the specific goal the resource is about (having a working topic custom field or a category custom field). A video would probably just be @angus or someone else talking through how to implement the resource, but that is straightforward, and we can probably just lay it out here.
To be clear, these resources are not plugins that you just add to your site as a plug and play that customizes your forum. Rather, they efficiently give you the understanding you need to code your own custom fields in your plugin.
This is how I’ve used these resources:
You will need to add in the name and type of field that you want in config/settings. The code in these resources uses variables that are defined there. So you actually don’t need to do much customizing of the code to get it to work in your own plugin after that–the variables in plugin.rb and elsewhere refer to config/settings, and then should work.
After updating config/settings, you can just follow along with the code, adding it to your plugin:
Start with the code in plugin.rb, and add that to your own plugin’s plugin.rb in order to create the custom field
Then create the form in the view layer that will be where the user (or your app, if the app adds the field automatically) can enter the value for the custom field, here (assets/discourse/connectors/[plugin-outlet-name]/[your special template].hbs.
@angus feel free to correct anything I’ve said here.
Once I got the feel for setting up the custom field by going through the steps above, I then started customizing things a bit further (for example, getting more creative with how the form works), but this was an extremely helpful starting point that saved me hours of work.
After going through it, I did have some questions (like I asked earlier), but getting responses in #dev seems like the most helpful way to go about things from there.