Hide certain group settings if group name includes a specific name

I’ve created a custom plugin to include some extra fields for groups. But they only apply to groups with the name “chapter” in it. So it doesn’t make sense to have them in groups for other purposes. Is there some way I can create a condition that checks e.g. if chapter in group.name; then chapter = true?

I have the following:

I want to hide the control group at the top if the group name does not have the word chapter in it. I seem to already use some Jinja2 stuff from the example I used. If that’s anything like other Jinja2, then I can certainly create such a condition there. But then I need to know a few things.

  • If Jinja2 is fully available, how can I access the group name variable to create the condition?
  • If Jinja2 is not (fully) available, how can I create this condition then? Probably some other JavaScript should set a boolean I suppose. But the structure of these group variables and JavaScript logic is not known to me, any examples would great!

Oh wait… I now suddenly see some methods to test. If Jinja2 works here, then indeed in g.json the group name is visible and can be used for a condition…

An no, that doesn’t work. And it’s not Jinja2, it’s Handlebars specific stuff (but I suppose contains is not a native function?).

{{#contains "chapter" group.name}}
<div class='control-group'>
    <label class="control-label">{{i18n 'admin.groups.my_map.map_title'}}</label>
    {{input type="checkbox" checked=group.custom_fields.show_map}}
    <span>{{i18n 'admin.groups.my_map.map_switch'}}</span>
</div>
{{/contains}}

But that doesn’t work, I get a bunch of errors. I probably don’t use the right variable lookup. Although replacing group.name with simply "chapter_netherlands" also fails.

Error occurred:

- While rendering:
  -top-level
    application
      discourse-root
        group
          group.manage
            group.manage.profile
              groups-form-profile-fields
                plugin-outlet
                  plugin-connector

Uncaught Error: Attempted to resolve `contains`, which was expected to be a component, but nothing was found.

Anyone that can point me in the right direction, please? :nerd_face:

So I see and get that I have to create these functions myself, it’s not like Jinja2 with useful builtin things to do this.

But where do I create these handlebars functions in my plugin? I see quite some examples in here: Search · handlebars · GitHub

But I don’t really see a pattern in this folder structure. I’ve created my own function in discourse-group-custom-fields/assets/javascripts/discourse/app/helpers/contains.js, but when I try to use it in discourse-group-custom-fields/assets/javascripts/discourse/connectors/group-edit/field-container.hbs I get the error Uncaught Error: Attempted to resolve "contains", which was expected to be a component, but nothing was found.. So obviously, this is not a path where Discourse is looking, or I’m doing something wrong.

import RawHandlebars from "discourse-common/lib/raw-handlebars";
import { htmlSafe } from "@ember/template";
import { rawConnectorsFor } from "discourse/lib/plugin-connectors";

//RawHandlebars.registerHelper("contains", function (args) {
//  const connectors = rawConnectorsFor(args.hash.name);
//  if (connectors.length) {
//    const output = connectors.map((c) => c.template({ context: this }));
//    return htmlSafe(output.join(""));
//  }
//});


Handlebars.registerHelper('contains', function(needle, haystack, options) {
     needle = Handlebars.escapeExpression(needle);
     haystack = Handlebars.escapeExpression(haystack);
     return (haystack.indexOf(needle) > -1) ? options.fn(this) : options.inverse(this);
});

The topic below suggests that it could be in the initializers directory and have the es6 extension.

So I moved my contains.js to that directory and added .e6 as the extension and I got the same error. Some help or docs would be great :slight_smile: