开发 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 这一行告诉 Discourse,该插件需要在 /admin/plugins 页面上添加一个链接。其标题将来自我们的 i18n 翻译文件中的 purple_tentacle.title,并且它将链接到 purple-tentacle 路由。

其下方的几行代码用于设置插件的服务端路由映射。Discourse 的一个假设是,前端上的几乎每个路由都有一个提供数据的服务端路由。对于这个示例插件,我们实际上不需要从后端获取任何数据,但我们必须告诉 Discourse,如果用户直接访问 /admin/plugins/purple-tentacle,则提供某些内容。这一行只是告诉它:“嘿,如果用户直接在服务端访问该 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");
  },
};

路由映射是我们添加到 Discourse 中的功能,用于让插件能够向 Ember 应用添加路由。map() 内部的语法与 Ember 的路由器 非常相似。在本例中,我们的路由映射非常简单,它只是在 /admin/plugins 下声明了一个名为 purple-tentacle 的路由。

最后,让我们添加翻译字符串:

config/locales/client.en.yml

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

如果您重启开发服务器,应该可以访问 /admin/plugins 并看到我们的链接!点击它,您会看到显示紫色触手的按钮:

不幸的是,当您点击按钮时,没有任何反应 :(。

如果您查看开发者控制台,应该会看到一个错误,提示了原因:

Uncaught Error: Nothing handled the action 'showTentacle'

是的,原因是在我们的模板中,我们依赖以下两点:

  1. 当用户点击按钮时,控制器上的 showTentacle 方法会被调用。
  2. showTentacle 应将属性 tentacleVisible 设置为 true,以便显示图片。

如果您还没有阅读 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;
  }
}

现在,当我们刷新页面时,点击按钮就会显示我们的动画角色!

我将把添加一个点击后隐藏触手的按钮作为读者的额外练习 :smile:

如果您在运行此插件版本时遇到问题,我已将其推送到 GitHub


系列更多内容

第 1 部分:插件基础
第 2 部分:插件出口
第 3 部分:站点设置
第 4 部分:Git 设置
第 5 部分:本文主题
第 6 部分:验收测试
第 7 部分:发布您的插件


本文档已进行版本控制 - 如有建议,请 在 GitHub 上提交

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.