Developing Discourse Plugins - Part 3 - Add custom site settings

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 ページには以下のように表示されます。

なぜ「設定」ボタンが表示されないのか分かりません。

私は、plugins の代わりに county_fence というプラグイン名を書くべきだと思います。

「いいね!」 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"

デフォルト値を-1ではなく0に設定したところ、表示されるようになりました。

型の検出が正しく行われていないことを知る方法はありますか?

  1. YamlLoader がファイルをロードします。
  2. SiteSettingExtension が setting() を呼び出します。パラメータは YamlLoader によって生成されます。
  3. …これは defaults.load_setting を呼び出し、その後 type_supervisor.load_setting を呼び出します。
  4. type_supervisor.load_setting には値が直接渡されませんが、TypeSupervisor が DefaultsProvider 経由で初期化される際にデフォルト値が渡されているため、アクセスできます。
  5. type_supervisor.load_settingget_data_type を呼び出し、これは parse_value_type() を呼び出します。型が検出できない場合は ArgumentError を発生させるはずです。

この一連の処理は、SiteSetting モデルの load_settings によって呼び出されていると思われますが、スタックのどこにも例外処理が見当たりません。しかし、サーバーログやその他の場所でエラー出力が見られません。

設定の解析に失敗したことを示す、何らかのユーザーフィードバックやエラーログがあるはずだと思います。

もし私の理解が正しければ、設定でデフォルト値として-1を使用すると、「設定」ボタンが表示されなくなるということですね。毎回この動作を再現できますか?

それは奇妙ですね。設定に問題は見当たりませんが、解析に失敗する可能性がどうしてあるのか分かりません。:thinking:

これで動作するので、-1 に戻しても機能し続けます。

データベースを削除し、再初期化しましたが、再現できません。 :person_shrugging:

「いいね!」 1

プラグインを安全のためデフォルトの0のまま本番サイトにデプロイしました。設定ボタンは表示されます。

image

しかし、クリックしても設定が表示されません。

この問題のトラブルシューティングを続ける間、プラグインを無効にします。

Ok、開発と本番の両方で、plugins ディレクトリ内のフォルダが county-fence と一致しなかったことが原因だったと思います。それが設定が表示されなかった理由であり、他の問題を引き起こした可能性もあります。

皆さん、ありがとうございました!

「いいね!」 1

私には不明瞭です。インストールしたプラグインのほとんどは、公式および最近のプラグイン(スケルトンテンプレートを含む)でも、plugins:を使用していません。

その部分は設定のフィルタリングに使用されると思っていましたが、間違っているかもしれません。

「いいね!」 1

なるほど、興味深いですね!では失礼しました。

必要ないとのことですが。

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 -
選択タイプ Enum(ドロップダウン) 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 -
絵文字リスト 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