How to add custom fields to models

I haven’t tested it, but, I believe the reason you are seeing only 1/3 fields is its looping over and registering a non-unique connector class and overriding the previous one.

In general, for the client side, rather than looping through the custom fields and declaring the api methods. I suggest you define components for each field separately, or at least separate actions as you’ll probably need to have different logic associated with each field?

The only portion I would loop over and declare is this:

  api.serializeOnCreate(field.name);
      api.serializeToDraft(field.name);
      api.serializeToTopic(field.name, `topic.${field.name}`);

The rest of the components, its probably best to create separate logic for each case.

3 Likes

@keegan that worked! thank you so much for all the insights. Couldn’t have done it without your help.

4 Likes

Is it possible to add a custom field to tags using the same logic?

2 Likes

Has anyone applied this to groups? If so, can you share what you did?

I guess this would do the trick. Hopefully it’s still up-to-date.

No, tags don’t have custom fields. What are you trying to accomplish?

Yes, that repository should work. Just change all instances of my_field to the name of your field.

1 Like

I am trying to create the following plugin: Creating a User - Tag relation plugin

Is there some more documentation about how to customize these group fields? It works indeed great with this boilerplate code. But how to extend it properly?

I for example want to add a few input fields, like:

<div class="control-group">
  <label class="control-label" for="map">Chapter coordinates</label>
  <input name="chapter_coordinates" id="chapter_coordinates" class="ember-text-field ember-view input-xlarge" value={{group.custom_fields.group_coordinates}} placeholder="E.g. 52.3727598,4.8936041" type="text">
  <div class="control-instructions">Fetch coordinates from https://nominatim.openstreetmap.org/</div>
</div>

But I’m just guessing what to do here and then add the rest of the Discourse stuff. The example above is to use a field for coordinates. I intent to use the g.json to create a map based on the group metadata.

I also intend to add a checkbox for emailing the group with a mail setup for that group.

<div class="control-group">
  <label class="control-label" for="map">Contact chapter by email</label>
  {{input type="checkbox" checked=group.custom_fields.contact_group_by_email}}
  <span>{{i18n 'admin.groups.contact_group_by_email.label'}}</span>
</div>

However, this field is best placed in the interaction tab. How to know what to do to get it there? I would like to become more proficient with this. But I get the idea that this information is just in the code, not documented somewhere. Which is probably fine, but just takes more time and effort to find what I’m looking for. Although what I want to add is quite small, just a few fields to the groups :nerd_face:

Yes, it can be tricky and a bit overwhelming when starting to navigate the Discourse codebase. It sounds like you’re trying to work with GeoJSON. Have you considered using or extending the Locations Plugin? That’s already set up to work with GeoJSON in Discourse.

A course in plugin development?

I’ve been considering running a free course in discourse plugin development, which is essentially what you need. I’ve already written the course materials for a course in theme development (see below). If 30 people vote for one in plugin development I’ll write a give a course (via zoom) in it.

  • Write and give course in plugin development
  • Don’t write and give a course in plugin development

0 voters

Introduction to theme development
  1. GitHub - pavilionedu/discourse-theme-introduction
  2. GitHub - pavilionedu/discourse-theme-css
  3. GitHub - pavilionedu/discourse-theme-colors
  4. GitHub - pavilionedu/discourse-theme-html-one
  5. GitHub - pavilionedu/discourse-theme-html-two
  6. GitHub - pavilionedu/discourse-theme-javascript-one
  7. GitHub - pavilionedu/discourse-theme-javascript-two

*ps if it hits 30 please let me know.

3 Likes

Awesome! I actually will use a fork of this Fairphone community map. They use the raw YAML output of a topic. I’ve modified my fork to use the data from g.json to draw markers on the map. I just miss a few fields such as coordinates and a boolean to use an email address in the map. Maybe some more, but first I’ll need to know how things work. Thanks for the links! Will have a look this week and see how far I’ll get. A course would be awesome too! My JS/Ruby skills are now though. Mainly Python/Bash and some crumbs from other language’s.

Is it possible to add custom fields to posts? Ideally from the post composer.

What is the minimum discourse version required to use this plugin?
(discourse-topic-custom-fields)

Is this GitHub - pavilionedu/discourse-group-custom-fields still up to date?

The reason I ask is because I attempted to implement a new custom group field, but it doesn’t seem to be saving/persisting the value I enter into the input field after clicking Save.

Here’s the commit with all of the changes I did that (I think) correctly followed the edu repo’s structure: add discord_role_id field to groups · aloha-pk/discourse-discord-sync@fd3eef1 · GitHub

Yes, I just tested it and it’s working as expected.

{{input type="text" checked=group.custom_fields.discord_role_id}}

Your issue is that you’ve changed the input type to text, but left the value input as checked. You need to change checked to value.

3 Likes

Welp, that’ll do it :man_facepalming: Thanks for the help! :smile:

1 Like

Was just able to rebuild with this change, but unfortunately am still seeing the same issue. The value I entered gets cleared out after I save and re-load the page.

Any other suggestions @angus?

Did you change anything else from the example? The example works. Try starting with the example itself. If that’s also failing in your environment, then something else is going on. If the example works in your environment, then progressively work through the changes you made until you find the one that’s breaking it.

Hi @angus added a custom filed using this plugin and it is working fine now how can i show this filed on category page below category name

Here’s how you can figure that out:

  1. Find the templates in discourse/discourse that render the category page (search for a html element on the page)

  2. Find a plugin-outlet in one of those templates

  3. Use the plugin outlet as described here

written this code but it is still not working can you tell the issue

<script type="text/discourse-plugin" version="0.8">
  const { getOwner } = require("discourse-common/lib/get-owner");
  const { htmlSafe } = require("@ember/string");

  api.decorateWidget("category-heading:after", (helper) => {
    const category = helper.attrs.category;

    if (category.custom_fields?.location) {
      const customLocation = htmlSafe(category.custom_fields.location);
      const template = getOwner(helper).lookup("template:components/category-header");

      return template.fragment.build("custom-location", { customLocation });
    }
  });
</script>

<script type="text/x-handlebars" data-template-name="components/category-header/custom-location">
  <div class="category-custom-location">{{customLocation}}</div>
</script>

What part of it is not working? For example, what do you see when you stick a console.log(category.custom_fields) in there?