开发Discource插件 - 第三部分 - 添加自定义站点设置

You simply need to add a

config/locales/server.zh_CN.yml

file (and similar for any other locales you want to support) with the same English tokens but with language specific values and the plugin will use that instead of falling back to the English.

2 个赞

Thanks for helping! have a nice day!

3 个赞

I was looking how to create a site setting that validates a username, but found there’s a username type to do this. Here the list of site setting types we have today:

  • email
  • username
  • integer
  • regex
  • string
  • list
  • enum

Also, you can do custom validations for your plugin settings, it accepts a validator field that expects a Ruby class. You use it like this.

plugins:
  my_custom_username_validation:
    default: 'system'
    validator: 'UsernameSettingValidator'

You can check the UsernameSettingValidator class to see the interface of validators.

Cheers!

10 个赞

not working (

<a id="forgot-password-link" class="forgot-password" href="{{Discourse.SiteSettings.myplugin_link_forgot_password}}" target="_blank">Forgot password?</a>

settings.yml

plugins:
  myplugin_enabled:
    default: true
    client: true
  myplugin_link_forgot_password:
    default: ''
    client: true

upd:
working!!!
href="{{unbound siteSettings.myplugin_link_forgot_password}}"

how to display text html ?
not working {{unbound siteSettings.forum_text_for_admin}}

upd:
working in *.hbs {{{unbound siteSettings.forum_text_for_admin}}}

Again you need to use a Computed Property and format as safe html. This is EmberJS not vanilla JavaScript. You can’t put ‘code’ directly into templates. Read the ember guides.

1 个赞

already working with EmberJS

我的 settings.yml 文件中有以下内容:

plugins:
  county_fence_enabled:
    default: false
    client: true
  county_fence_latrine_category:
    default: -1
    client: true

这显示在我的 /admin/plugins 页面上:

我不明白为什么没有“设置”按钮?

我认为你应该写插件名称 county_fence 而不是 plugins

2 个赞

我也尝试过,但没有区别。

这份文档(以及 OP)使用了 plugins 键。在其他地方,我看到过像你所说的使用插件名称。

1 个赞

@bitmage 其实,我可能知道原因。

你在 plugin.rb 中写了 # name: County Fence 吗?
如果是,名字必须与你的目录名相同。

插件的名称可以通过本地化覆盖:

假设你的设置格式是:

county_fence:
  county_fence_enabled: true
  ...

client.en.yml 中:

en:
  admin_js:
    admin:
      site_settings:
        categories:
          county_fence: "County Fence"

将默认值设置为 0 而不是 -1 会导致其显示出来。

有什么方法可以知道某个类型没有被正确检测到吗?

  1. YamlLoader 加载文件。
  2. SiteSettingExtension 调用 setting(),参数由 YamlLoader 生成
  3. …然后调用 defaults.load_setting,稍后调用 type_supervisor.load_setting
  4. 即使 type_supervisor.load_setting 没有直接传递值,它也可以访问默认值,因为这些默认值在通过 DefaultsProvider 初始化 TypeSupervisor 时被传递进来了。
  5. type_supervisor.load_setting 然后调用 get_data_type,后者调用 parse_value_type(),如果类型无法检测到,它应该会引发 ArgumentError。

我认为整个序列是由 SiteSetting 模型中的 load_settings 调用的,并且我在堆栈的任何地方都看不到异常处理。但我没有在服务器日志或任何其他地方看到任何错误输出。

似乎应该有一些用户反馈或错误日志来说明某个设置解析失败了。

如果我理解正确的话,您的意思是将 -1 用作设置的默认值会导致“设置”按钮不显示。您每次都能重现此行为吗?

这很奇怪。我看不到您的设置有什么问题,我不确定解析失败是如何发生的。:thinking:

现在它正常工作了,我可以将其改回 -1,它仍然可以正常运行。

我删除了数据库,重新初始化了,仍然无法重现它。 :person_shrugging:

1 个赞

我已将插件部署到我的实时站点,默认设置为 0 以确保安全。设置按钮显示:

image

但当我点击它时,没有显示任何设置:

在继续排查此问题之前,我将禁用该插件。

好的,我认为在开发和生产环境中都是因为插件目录中的文件夹与 county-fence 不匹配。这就是为什么设置没有显示,并且可能还会导致其他问题。

谢谢大家!

1 个赞

我还不清楚。我安装的大多数插件都不使用 plugins:,包括官方和最近的插件(还有骨架模板

我以为那部分用于设置过滤,但可能我错了。

1 个赞

哦,有意思!那抱歉了。

我要说的是,Apparently,这并不是必需的。

我已经数不清我用 plugins: 编写了多少插件了。

1 个赞

为了扩展文档:

Site Settings 类型的完整列表可在 type_supervisor.rb 代码 中找到。在其正上方,您还可以看到 Site Setting 接受的有效子键。

一个使用更多这些选项的高级示例可以在 discourse-welcome-link 主题组件 中找到。这些相同的值也应该可以在插件中使用,但文件将位于 config/settings.yml

3 个赞

插件和主题组件类型之间存在差异。

我尝试制作了一个表格(预计会有小的错误或分组)

类别 功能 插件语法 TC 语法
基本类型 字符串 type: string
min: ..
max: ..
regex: ..
secret: true/false
global
validator: 类名
type: string
min: ..
max: ..
-
-
-
-
多行文本 type: string
textarea: true
type: string
textarea: true
整数 type: integer
min: ..
max: ..
type: integer
min: ..
max: ..
浮点数 type: float
min: ..
max: ..
type: float
min: ..
max: ..
布尔值 type: bool type: bool
时间 type: time -
Null type: null -
选择类型 枚举(下拉菜单) type: enum
choices: [option1, option2]
enum: ..
type: enum
choices: [option1, option2]
类别 type: category -
颜色 type: color -
type: group -
电子邮件 type: email -
用户名 type: username -
列表类型 通用列表 type: list
allow_any: true/false
type: list
简单/紧凑列表 type: simple_list

type: list
list_type: compact
list_type: simple
type: list
list_type: compact
list_type: simple
类别列表 type: category_list type: list
list_type: category
组列表 type: group_list type: list
list_type: group
标签列表 type: tag_list type: list
list_type: tag
标签组列表 type: tag_group_list -
URL 列表 type: url_list -
主机列表 type: host_list -
值列表 type: value_list -
Emoji 列表 type: emoji_list -
文件类型 上传 type: upload type: upload
上传图片列表 type: uploaded_image_list -
文件大小限制 type: file_size_restriction
min: ..
max: ..
-
特殊类型 HTML(已弃用) type: html_deprecated -
JSON 对象 已弃用 已弃用
对象 - type: objects
5 个赞