Objects type for theme setting

Narrator: “It was not landed that week.” :upside_down_face:

2 Likes

Is there any change on this? I just re-wrote a component for someone using objects type and they are unhappy that they can’t reorder.

4 Likes

Hey @awesomerobot do you think this going to get onto someone’s list any time soon? It doesn’t seem like anyone from team has looked at this in a year.

Also, it’s pretty terrible that they are all named “field_name-X”. Would it be possible to have a field name that could get used there? Having to click every single one to see what’s there is a nightmare if you have more than a couple objects.

If we can’t re-order the items and you can’t tell what they are without clicking on them, I’m likely going to have to go back to using a single string setting with multiple values separated with, uh, some character to separate the different fields. And I think with the old-style arrays you could re-order stuff, right? So this objects type seems like a step backward for everyone except programmers.

2 Likes

I don’t think there’s anything in process for reordering at the moment, though it’s still something we’d like to include. Renaming is already possible with the identifier property. For example, from the custom header links theme component

The name field is set as the identifier for each link, so you can see which link is which

2 Likes

Hooray. I thought I must have overlooked that but have not managed to find it before. Thanks!

EDIT: And now I used the identifier property and added an ‘order’ field, so it’s not too bad. I still think being able to reorder settings would be nice, and it’s available with arrays. . .

Thanks again for your help

1 Like

I made a commit a bit ago that adds re-ordering for objects. Here is a little test setting to show the buttons:

10 Likes

I think it is worth announcing that we got uploads as a field type option now, and I think it was mentioned somewhere, but it is nice to mention again that type: objects works on both theme settings and in site settings(plugins)

10 Likes

The correct one should be upload

1 Like

Thank you! Will be fixed in

1 Like

Does the objects type support a max limit? I tried it, but it seems it doesn’t.

Do you mean like for a string type field in a objects tree?
Try max_length

See #Validations for string types on this topic; or a working example:

I think he’s referring to:

1 Like

Yes, I am referring to limiting the maximum number of items in the objects array.

Currently, this is not supported out of the box, so I extended the functionality in a custom private theme component. Here is the configuration (for reference only):

Configuration for maximum array count limit:

themes/quectel-custom-homepage/settings.yml

banner_images:
  type: objects
  default: []
  schema:
    name: "Carousel"
    max: 9 // New extended configuration to limit the maximum number of items in the array
    identifier: image_url
    properties:
      image_url:
        type: upload
        required: true
      link_url:
        type: string
        min: 0
        max: 1023

Extended code:

themes/quectel-custom-homepage/javascripts/discourse/api-initializers/quectel-custom-homepage.gjs

import { action } from "@ember/object";
import { apiInitializer } from "discourse/lib/api";
import classPrepend from "discourse/lib/class-prepend";
import { i18n } from "discourse-i18n";

export default apiInitializer(() => {
  // Use classPrepend to extend the SchemaSettingNewEditor component to support the max limit for objects type
  // Load this component only in the admin context
  let SchemaSettingNewEditor;
  try {
    SchemaSettingNewEditor =
      require("discourse/admin/components/schema-setting/editor").default;
  } catch {
    // Non-admin users cannot load this component, skip extension
    return;
  }

  classPrepend(SchemaSettingNewEditor, (Superclass) => {
    return class extends Superclass {
      @action
      addItem() {
        // Check if the max limit is reached
        const maxLimit = this.args.setting.max || this.activeSchema?.max;

        if (maxLimit && this.activeData.length >= maxLimit) {
          this.validationErrorMessage = i18n(
            themePrefix("theme_settings.errors.objects_value_not_valid_max"),
            {
              count: maxLimit,
            }
          );
          return;
        }

        return super.addItem(...arguments);
      }

      @action
      addChildItem(propertyName, parentNodeIndex) {
        const propertySchema = this.activeSchema.properties[propertyName];

        // Check if the max limit is reached
        if (
          propertySchema?.max &&
          this.activeData[parentNodeIndex][propertyName].length >=
            propertySchema.max
        ) {
          this.validationErrorMessage = i18n(
            themePrefix("theme_settings.errors.objects_value_not_valid_max"),
            {
              count: propertySchema.max,
            }
          );
          return;
        }

        return super.addChildItem(...arguments);
      }
    };
  });
});

Theme translation configuration:

themes/quectel-custom-homepage/locales/en.yml

en:
  theme_settings:
    errors:
      objects_value_not_valid_max: "The number cannot exceed %{count}"

Actual effect:

  1. Error message
  2. Unable to add more items
1 Like