Discourse-Plugins entwickeln – Teil 5: Eine Admin-Oberfläche hinzufügen

Vorheriges Tutorial: Developing Discourse Plugins - Part 4 - Setup git


Manchmal reichen Site-Einstellungen nicht als Admin-Oberfläche aus, damit Ihr Plugin so funktioniert, wie Sie es möchten. Wenn Sie beispielsweise das Plugin discourse-akismet installieren, haben Sie vielleicht bemerkt, dass es einen Navigationseintrag im Admin-Bereich für Plugins in Ihrem Discourse hinzufügt:

In diesem Tutorial zeigen wir Ihnen, wie Sie eine Admin-Oberfläche für Ihr Plugin hinzufügen. Ich nenne mein Plugin „purple-tentacle“ zu Ehren von einem meiner Lieblingscomputerspiele. Ernsthaft, ich liebe dieses Spiel wirklich!

Einrichten der Admin-Route

Beginnen wir damit, eine plugin.rb hinzuzufügen, wie wir es in früheren Teilen des Tutorials getan haben.

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

Die Zeile add_admin_route teilt Discourse mit, dass dieses Plugin einen Link auf der Seite /admin/plugins benötigt. Der Titel wird purple_tentacle.title aus unserer i18n-Übersetzungsdatei sein, und er verweist auf die Route purple-tentacle.

Die darunter liegenden Zeilen richten die serverseitige Zuordnung der Routen für unser Plugin ein. Eine Annahme, die Discourse trifft, ist, dass fast jede Route auf der Frontend-Seite eine serverseitige Route hat, die Daten bereitstellt. Für dieses Beispiel-Plugin benötigen wir zwar keine Daten vom Backend, aber wir müssen Discourse mitteilen, dass es etwas ausliefert, falls der Benutzer direkt /admin/plugins/purple-tentacle aufruft. Diese Zeile sagt einfach: „Hey, wenn der Benutzer diese URL direkt auf der Serverseite aufruft, liefere den Standard-Plugin-Inhalt aus!"

(Wenn das verwirrend ist, machen Sie sich keine Sorgen – wir werden in einem zukünftigen Tutorial darauf zurückkommen, wenn wir serverseitige Aktionen behandeln.)

Als Nächstes fügen wir eine Vorlage hinzu, die angezeigt wird, wenn der Benutzer den Pfad /admin/plugins/purple-tentacle aufruft. Es wird nur ein Button sein, der beim Klicken ein animiertes GIF eines lila Tentakels anzeigt:

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>

Wenn Sie die Grundlagen von Handlebars gelernt haben, sollte die Vorlage ziemlich einfach zu verstehen sein. <DButton /> ist eine Komponente in Discourse, die wir verwenden, um einen Button mit Beschriftung und Icon anzuzeigen.

Um unsere neue Vorlage zu verknüpfen, müssen wir eine Routenmap erstellen:

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

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

Eine Routenmap ist etwas, das wir zu Discourse hinzugefügt haben, damit Plugins Routen zur Ember-Anwendung hinzufügen können. Die Syntax innerhalb von map() ist Embers Router sehr ähnlich. In diesem Fall ist unsere Routenmap sehr einfach; sie deklariert nur eine Route namens purple-tentacle unter /admin/plugins.

Schließlich fügen wir unsere Übersetzungszeichenfolgen hinzu:

config/locales/client.en.yml

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

Wenn Sie Ihren Entwicklungsserver neu starten, sollten Sie /admin/plugins aufrufen können und unseren Link sehen! Wenn Sie darauf klicken, erscheint der Button, um unseren lila Tentakel anzuzeigen:

Leider passiert beim Klicken auf den Button nichts :frowning:

Wenn Sie in Ihrer Entwicklerkonsole nachsehen, sollten Sie einen Fehler sehen, der einen Hinweis darauf gibt, warum dies der Fall ist:

Uncaught Error: Nothing handled the action 'showTentacle'`

Ah ja, der Grund liegt darin, dass wir in unserer Vorlage von ein paar Dingen abhängig sind:

  1. Dass beim Klicken auf den Button showTentacle im Controller aufgerufen wird.
  2. Dass showTentacle die Eigenschaft tentacleVisible auf true setzt, damit das Bild angezeigt wird.

Wenn Sie die Ember-Guides zu Controllern noch nicht gelesen haben, ist jetzt ein guter Zeitpunkt dafür, da wir einen Controller für unsere purple-tentacle-Vorlage implementieren werden, der diese Logik übernimmt.

Erstellen Sie die folgende Datei:

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;
  }
}

Und jetzt, wenn wir unsere Seite neu laden, zeigt das Klicken auf den Button unseren animierten Charakter an!

Ich überlasse es dem Leser als zusätzliche Übung, einen Button hinzuzufügen, der den Tentakel beim Klicken ausblendet :smile:

Wenn Sie Schwierigkeiten haben, Ihre Version dieses Plugins zum Laufen zu bringen, habe ich es auf GitHub hochgeladen.


Mehr in dieser Serie

Teil 1: Plugin-Grundlagen
Teil 2: Plugin-Ausgänge
Teil 3: Site-Einstellungen
Teil 4: Git-Einrichtung
Teil 5: Dieses Thema
Teil 6: Akzeptanztests
Teil 7: Veröffentlichen Sie Ihr Plugin


Dieses Dokument ist versioniert – schlagen Sie Änderungen auf GitHub vor.

28 „Gefällt mir“

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 „Gefällt mir“

@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 „Gefällt mir“

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 „Gefällt mir“

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 „Gefällt mir“

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 „Gefällt mir“

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 „Gefällt mir“

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 „Gefällt mir“

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 „Gefällt mir“

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 „Gefällt mir“

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.