أنواع الكائنات لإعداد السمة

نعم، أعني تحديد الحد الأقصى لعدد العناصر في مصفوفة 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(() => {
  // استخدام classPrepend لتوسيع مكون SchemaSettingNewEditor لدعم حد max لنوع objects
  // يتم تحميل هذا المكون فقط في سياق المشرفين
  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} عنصرًا"

النتيجة المحددة:

  1. رسالة الخطأ
  2. عدم القدرة على إضافة المزيد
إعجاب واحد (1)