Discourseプラグインの開発 - 第5部 - 管理インターフェースの追加

前のチュートリアル: Developing Discourse Plugins - Part 4 - Setup git


サイト設定だけでは、プラグインを希望通りに動作させるための管理インターフェースとして不十分な場合があります。例えば、discourse-akismet プラグインをインストールすると、Discourse の管理プラグインセクションにナビゲーション項目が追加されていることに気づいたかもしれません。

このチュートリアルでは、プラグインに管理インターフェースを追加する方法を紹介します。私のプラグインは お気に入りのコンピュータゲームの一つにちなんで「purple-tentacle」と呼ぶことにします。本当に、そのゲームが大好きです

管理ルートの設定

まず、チュートリアルの前のパートで行ったように plugin.rb を追加しましょう。

plugin.rb

# name: purple-tentacle
# about: A sample plugin showing how to add a plugin route
# version: 0.1
# authors: Robin Ward
# url: https://github.com/discourse/purple-tentacle

add_admin_route 'purple_tentacle.title', 'purple-tentacle'

Discourse::Application.routes.append do
  get '/admin/plugins/purple-tentacle' => 'admin/plugins#index', constraints: StaffConstraint.new
end

add_admin_route の行は、このプラグインが /admin/plugins ページにリンクを必要とすることを Discourse に伝えます。そのタイトルは i18n 翻訳ファイルの purple_tentacle.title から取得され、purple-tentacle ルートにリンクします。

その下の行は、プラグインのサーバー側のルートマッピングを設定します。Discourse が仮定していることの1つは、フロントエンドのほぼすべてのルートに、データを提供するサーバー側のルートが存在することです。このサンプルプラグインの場合、実際にはバックエンドからデータは必要ありませんが、ユーザーが直接 /admin/plugins/purple-tentacle にアクセスした場合に何かを返すように Discourse に指示する必要があります。この行は、「もしユーザーがサーバー側でその URL に直接アクセスしたら、デフォルトのプラグインコンテンツを返してね」という意味です。

(もしこれが混乱を招くようであれば、心配しすぎないでください。サーバー側のアクションを処理する将来のチュートリアルで再び取り上げます。)

次に、ユーザーが /admin/plugins/purple-tentacle パスにアクセスしたときに表示されるテンプレートを追加します。これは、ユーザーがボタンをクリックすると紫色の触手のアニメーション GIF を表示するボタンだけのものです。

assets/javascripts/discourse/templates/admin/plugins-purple-tentacle.gjs

import DButton from "discourse/components/d-button";

<template>
  {{#if @controller.tentacleVisible}}
    <div class="tentacle">
      <img src="https://eviltrout.com/images/tentacle.gif" />
    </div>
  {{/if}}

  <div class="buttons">
    <DButton
      @label="purple_tentacle.show"
      @action={{@controller.showTentacle}}
      @icon="eye"
      @id="show-tentacle"
    />
  </div>
</template>

Handlebars の基礎を学んでいれば、テンプレートは理解しやすいはずです。<DButton /> は、ラベルとアイコン付きのボタンを表示するために Discourse で使用しているコンポーネントです。

新しいテンプレートを接続するために、ルートをマップする必要があります。

assets/javascripts/discourse/purple-tentacle-route-map.js

export default {
  resource: "admin.adminPlugins",
  path: "/plugins",
  map() {
    this.route("purple-tentacle");
  },
};

ルートマップは、プラグインが Ember アプリケーションにルートを追加できるようにするために Discourse に追加したものです。map() 内の構文は Ember のルーター と非常に似ています。この場合、ルートマップは非常にシンプルで、/admin/plugins の下に purple-tentacle という名前の1つのルートだけを宣言しています。

最後に、翻訳文字列を追加しましょう。

config/locales/client.en.yml

en:
  js:
    purple_tentacle:
      title: "Purple Tentacle"
      show: "Show Purple Tentacle"

開発サーバーを再起動すると、/admin/plugins にアクセスできるようになり、リンクが表示されます!クリックすると、紫色の触手を表示するボタンが見えます。

残念ながら、ボタンをクリックしても何も起こりません :frowning:

開発者コンソールを見ると、これがなぜ起こるのかを示すエラーが表示されるはずです。

Uncaught Error: Nothing handled the action 'showTentacle'

ああ、そうです。理由はテンプレートでいくつかのことに依存していることです。

  1. ユーザーがボタンをクリックしたとき、コントローラーで showTentacle が呼び出されること。
  2. showTentacle がプロパティ tentacleVisibletrue に設定して、画像が表示されること。

Ember ガイドのコントローラー をまだ読んでいない場合は、今読むのが良いタイミングです。なぜなら、このロジックを処理する purple-tentacle テンプレートのコントローラーを実装するからです。

次のファイルを作成します。

assets/javascripts/discourse/controllers/admin-plugins-purple-tentacle.js

import Controller from "@ember/controller";
import { action } from "@ember/object";
import { tracked } from "@glimmer/tracking";

export default class AdminPluginsPurpleTentacleController extends Controller {
  @tracked tentacleVisible = false;

  @action
  showTentacle() {
    this.tentacleVisible = true;
  }
}
```\n
そして、ページを更新すると、ボタンをクリックするとアニメーションキャラクターが表示されます!

<img src="upload://lU2ABNcxtZDW5bxcGcHSON8c89N.png" width="647" height="462">

触手を隠すボタンを追加するのは読者のための追加課題としておきます :smile:

このプラグインのバージョンが動作しない場合は、[github](https://github.com/eviltrout/purple-tentacle) にプッシュしました。

---

### シリーズの続き

パート1: [プラグインの基本](https://meta.discourse.org/t/beginners-guide-to-creating-discourse-plugins-part-1/30515)
パート2: [プラグインアウトレット](https://meta.discourse.org/t/beginners-guide-to-creating-discourse-plugins-part-2-plugin-outlets/31001)
パート3: [サイト設定](https://meta.discourse.org/t/beginners-guide-to-creating-discourse-plugins-part-3-custom-settings/31115)
パート4: [git の設定](https://meta.discourse.org/t/beginners-guide-to-creating-discourse-plugins-part-4-git-setup/31272)
**パート5: このトピック**
パート6: [受け入れテスト](https://meta.discourse.org/t/beginner-s-guide-to-creating-discourse-plugins-part-6-acceptance-tests/32619)
パート7: [プラグインの公開](https://meta.discourse.org/t/beginner-s-guide-to-creating-discourse-plugins-part-7-publish-your-plugin/101636)


---

<small>このドキュメントはバージョン管理されています。変更を提案するには [github](https://github.com/discourse/discourse/blob/main/docs/developer-guides/docs/04-plugins/05-admin-interface.md) をご覧ください。</small>
「いいね!」 28

Hm, doesn’t seem to work on my local side… did anyone face the same problem?

What’s the problem? If you install the purple tentacle plugin does it not work?

edit: issue resolved. it works!

@eviltrout nope, it doesn’t work. I believe @ladydanger is also facing the same problem.
nothing appears in the site settings or plugins tabs.
I tried both following the instructions as well as symlink-ing your plugin (dl-ed from github).
I then tried to create server.en.yml, client.en.yml, settings.yml following the format of other plugins, but the image wouldn’t render anyhow.

probably something wrong with the ..route-map.js.es6 or config files.

interestingly enough, akismet’s plugin does work for me.


One issue that keeps on biting me is that I still need to do

rm -fr /tmp/cache

To kick away some odd caching we have in some cases.

「いいね!」 1

@eviltrout

oh now this is an interesting finding - apparently our ad plugins were interfering with the purple-tentacle plugin. or the other way round. it works now, after having removed the ad plugin files.

  • it now works with our ad plugins! possibly some minor bug yesterday. all is good!

@sam, what’s the difference between rm -rf tmp and rm -fr /tmp/cache

I usually only delete the cache directory, no need to nuke the entire tmp directory.

「いいね!」 2

Are you sure it was a plugin conflict?

I copied it into my localhost plugin folder and started up the VM without removing the temp folder and got the same results as you posted.

I was puzzled to see the route in object dot notation but ignored that.

I then started up the VM and did remove the temp folder and all was OK

「いいね!」 1

Actually, sometimes you need just cache, and sometimes you need the whole tmp directory.

Maybe we could have an initializer that checks the timestamps on the plugin folders and nukes tmp if they were updated (dev only).

「いいね!」 3

One thing I really should have warned you about is I started raising errors on ember deprecations. We are really working towards upgrading our version of Ember soon and I wanted to make sure developers caught them. It’s possible your plugin has a deprecation or two and is now raising an error with the latest version of Discourse.

If you can’t figure out how to fix the deprecation, it’s okay to temporarily disable it in the Discourse working directory you are using. Try removing these two lines.

「いいね!」 2

No clue what I’m doing wrong here, but it shows up like this:

It must be the /config/locales/en.yml not loading, but why?

I put this file structure in the root of the plugin directory. Verified it’s there, nuked the tmp, restart rails.

en:
  js:
    purple_tentacle:
      title: "Purple Tentacle"
      show: "Show Purple Tentacle"
「いいね!」 1

I was a bit surprised to see the file named “en.yml” but figured either eviltrout knew something I didn’t (which I’m sure he does … anyway) or that maybe something had changed the way things worked.

I’m guessing that what Robin meant was client,en.yml because that works for me.

the file goes in the plugins config/locales folder

「いいね!」 4

Thanks I’ll try that after I get my dev environment setup on the xeon, went through hell getting ubuntu 16.04 to work right with my hardware. Turned out it wasn’t the server mobo, but the new nvidia card.

Yea I meant I put all the directories listed hierarchically starting with the plugins folder. Was wondering if for some reason that last file went elsewhere.

Same thing.

I tried naming the file client,en.yml, client.en.yml, and creating a client folder placing en.yml inside it.

Same result

Did you stop vagrant, clear the cache
$> rm -rf tmp
and restart vagrant?

I’m not using vagrant, yes I nuked tmp, restart rails.

This is a bit too annoying, and not a big deal. I’m going to move forward, but thanks for the efforts.

I have a feeling you may have the folder structure wrong, or maybe the naming. Mine is like

/discourse
../plugins
..../{yourpluginname}
......plugin.rb
....../config
........settings.yml
......../locales
..........client.en.yml
..........server.en.yml
....../assets
......../javascripts
........../discourse
............{yourpluginname}-route-map.js.es6
............/controllers
..............admin-plugins-{yourpluginname}.js.es6
............/templates
............../admin
................plugins-{yourpluginname}.hbs
「いいね!」 1

That’s the file structure I have.

I don’t see a point in getting stuck on this, already hacked together a couple of rails sites and trying to absorb as much as possible.

Just going to leave this as some simple error on my part that I’ll come look back at later on and probably realize right away what it was.

Thanks, thou

Another mistake of mine! I’ve updated the OP, thanks.

「いいね!」 2

Is there any way I can change a global plugin setting, say, a siteSetting, from here?
I need admins to be able to use this interface to add data into a global array.