How to add custom fields to models

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.

4 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?

I installed this plugin today with this code and it appears to be broken: https://github.com/pavilionedu/discourse-category-custom-fields.git

It installs as ā€œEducation Category Custom Fieldā€ and the author information isnā€™t there:

What do you expect to happen? Itā€™s not supposed to do anything without modification.

2 Likes