objects 类型允许 主题设置 保存一个由你定义结构的对象列表,例如包含名称和 URL 的链接,或各自包含链接的章节。管理员可以在对象编辑器中编辑该列表,Discourse 会根据你的架构验证每个对象。它取代了较旧的 json_schema 类型。
定义 objects 设置
在设置中添加 type: objects、default 和 schema:
links:
type: objects
default: []
schema:
name: link
identifier: name
properties:
name:
type: string
url:
type: string
default是设置的初始值:一个空列表,或匹配架构的对象列表。schema.name为单个对象命名。编辑器会使用它来标记对象,例如“link 1”。schema.identifier是可选的。它指定一个属性,其值将在编辑器中用于标记每个对象,例如链接的名称。schema.properties列出每个对象可以具有的属性。每个属性都需要一个type。
属性类型
-
string:文本。设置textarea: true以使用可扩展的文本区域:description: type: string textarea: true validations: max_length: 500 -
integer:整数。 -
float:可以有小数的数字。 -
boolean:true或false。 -
enum:choices中列出的值之一:size: type: enum choices: - small - medium - large -
datetime:带有时区的 ISO 8601 格式的日期和时间,例如2026-01-01T10:00:00Z。 -
upload:上传的文件。主题代码接收其 URL。 -
icon:Discourse 图标集中的图标名称。选定的图标会自动添加到精灵图中,因此你无需注册它们。 -
categories:分类 ID 列表。 -
groups:群组 ID 列表。 -
tags:标签名称列表。 -
objects:嵌套的对象列表。请参阅下面的“嵌套对象”。
对于 tags,设置 API 也接受标签 ID,并将其存储为名称。在主题代码中,每个带有 tags 属性的对象还会获得一个 tag_objects 数组,其中包含访问者可见的每个选定标签的 id 和 name,因此你可以按 ID 定位标签,例如在 CSS 中。
默认值
一旦定义了架构,default 就可以包含匹配该架构的对象:
links:
type: objects
default:
- name: Guidelines
url: /guidelines
- name: FAQ
url: /faq
schema:
name: link
identifier: name
properties:
name:
type: string
url:
type: string
必填属性
属性默认是可选的。添加 required: true 使其成为必填项:
properties:
name:
type: string
required: true
url:
type: string
空的 string、datetime 和 icon 值,以及空的 categories、groups 和 tags 列表被视为缺失值:必填属性会拒绝它们,而可选属性会跳过其验证。false 被视为已设置。
验证
某些类型接受 validations 键:
properties:
url:
type: string
required: true
validations:
max_length: 2048
url: true
对于 string 属性:
min_length:最小字符数。max_length:最大字符数。url:当为true时,值必须是一个 URL。接受绝对http、https和ftpURL、mailto:链接、以/开头的路径以及#锚点。
对于 integer 和 float 属性:
min:最小值。max:最大值。
对于 tags、groups 和 categories 属性:
min:选择的最小项目数。max:选择的最大项目数。
解析群组成员资格
主题代码通常只需要知道当前用户是否属于所选群组之一。currentUser.groups 无法可靠地回答这个问题,因为它只包含用户可见的群组。在 groups 属性中添加 resolve_group_membership: true 以便在服务器端解析它:
menu_sections:
type: objects
default:
- name: Staff tools
groups:
- 3
schema:
name: menu section
properties:
name:
type: string
groups:
type: groups
resolve_group_membership: true
管理员界面和存储的值保留 groups 列表。在前端 settings 对象中,Discourse 会将每个对象中的群组 ID 替换为一个以属性名命名、并以 user_in_ 为前缀的布尔值:
for (const section of settings.menu_sections) {
if (section.user_in_groups) {
// 用户属于至少一个选定的群组。
}
}
在对象架构中,此选项仅对 type: groups 属性有效。它也可以在嵌套对象中与自动群组(如 logged_in_users 和 anonymous_users)一起使用。
嵌套对象
具有 type: objects 及其自身 schema 的属性包含一个嵌套列表:
sections:
type: objects
default:
- name: Getting started
links:
- name: Guidelines
url: /guidelines
- name: FAQ
url: /faq
schema:
name: section
identifier: name
properties:
name:
type: string
required: true
links:
type: objects
schema:
name: link
identifier: name
properties:
name:
type: string
url:
type: string
设置描述和属性标签
在主题的 locales/en.yml 中描述设置并标记其属性。对于上面的 sections 设置:
en:
theme_metadata:
settings:
sections:
description: Sections shown in the sidebar
schema:
properties:
name:
label: Name
description: The section's heading
links:
name:
label: Name
description: The link text
url:
label: URL
description: Where the link goes
嵌套属性的标签放在其集合名称下,这里是 links。
可翻译文本
要让管理员翻译他们在对象编辑器中输入的文本,请声明一个必需的字符串 translation_key 属性,并为每个文本属性添加 translatable: true:
links:
type: objects
default:
- translation_key: guidelines
label: Community guidelines
schema:
name: link
properties:
translation_key:
type: string
required: true
label:
type: string
translatable: true
每个可翻译字段都会变成一个主题翻译,例如 links.guidelines.label,管理员可以在管理界面中管理它。有关键规则、嵌套对象、在代码中读取文本以及更新现有组件的信息,请参阅 可翻译设置。
此文档受版本控制 - 在 github 上建议更改。



