はい、objects 配列の最大数を制限することを指しています。
現在、公式ではサポートされていないため、カスタムプライベートテーマコンポーネント内でこの機能を拡張しました。設定は以下の通りです(参考用)。
最大配列数制限の設定:
themes/quectel-custom-homepage/settings.yml
banner_images:
type: objects
default: []
schema:
name: "スライダー"
max: 9 // 新たに追加された拡張設定。配列の最大数を制限
identifier: image_url
properties:
image_url:
type: upload
required: true
link_url:
type: string
min: 0
max: 1023
拡張された具体的なコード:
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(() => {
// SchemaSettingNewEditor コンポーネントを classPrepend を使用して拡張し、objects 型の max 制限をサポート
// 管理者コンテキストでのみこのコンポーネントを読み込む
let SchemaSettingNewEditor;
try {
SchemaSettingNewEditor =
require("discourse/admin/components/schema-setting/editor").default;
} catch {
// 管理者以外のユーザーはこのコンポーネントを読み込めないため、拡張をスキップ
return;
}
classPrepend(SchemaSettingNewEditor, (Superclass) => {
return class extends Superclass {
@action
addItem() {
// max 制限に達しているか確認
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];
// max 制限に達しているか確認
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);
}
};
});
});
テーマの翻訳設定:
themes/quectel-custom-homepage/locales/zh_CN.yml
zh_CN:
theme_settings:
errors:
objects_value_not_valid_max: "数量不能超过 %{count} 个"
実際の効果:
- エラーメッセージの表示
- 追加できなくなる
