Sviluppare plugin per Discourse - Parte 5 - Aggiungere un'interfaccia amministratore

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


A volte le impostazioni del sito non sono sufficienti come interfaccia di amministrazione affinché il tuo plugin funzioni come desideri. Ad esempio, se installi il plugin discourse-akismet, potresti aver notato che aggiunge una voce di navigazione alla sezione plugin di amministrazione di Discourse:

In questo tutorial ti mostreremo come aggiungere un’interfaccia di amministrazione per il tuo plugin. Chiamerò il mio plugin purple-tentacle, in onore di uno dei miei giochi per computer preferiti. Seriamente, adoro quel gioco!

Impostazione della rotta di amministrazione

Iniziamo aggiungendo un plugin.rb come abbiamo fatto nelle parti precedenti del tutorial.

plugin.rb

# name: purple-tentacle
# about: Un plugin di esempio che mostra come aggiungere una rotta di plugin
# 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

La riga add_admin_route dice a Discourse che questo plugin avrà bisogno di un link sulla pagina /admin/plugins. Il suo titolo sarà purple_tentacle.title dal nostro file di traduzioni i18n e si collegherà alla rotta purple-tentacle.

Le righe sottostanti impostano la mappatura lato server delle rotte per il nostro plugin. Un’ipotesi che Discourse fa è che quasi ogni rotta sul frontend abbia una rotta lato server che fornisce dati. Per questo plugin di esempio in realtà non abbiamo bisogno di dati dal backend, ma dobbiamo dire a Discourse di servire qualcosa nel caso in cui l’utente visiti /admin/plugins/purple-tentacle direttamente. Questa riga gli dice semplicemente: “ehi, se l’utente visita quell’URL direttamente sul lato server, servi il contenuto predefinito dei plugin!”

(Se questo è confuso, non preoccuparti troppo, ci torneremo in un tutorial futuro quando gestiremo le azioni lato server.)

Successivamente, aggiungeremo un template che verrà visualizzato quando l’utente visita il percorso /admin/plugins/purple-tentacle. Sarà solo un pulsante che mostra una gif animata di un tentacolo viola quando l’utente fa clic su un pulsante:

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

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

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

Se hai imparato le basi di handlebars, il template dovrebbe essere piuttosto semplice da capire. Il <DButton /> è un componente in Discourse che usiamo per mostrare un pulsante con un’etichetta e un’icona.

Per collegare il nostro nuovo template dobbiamo creare una mappa delle rotte:

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

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

Una mappa delle rotte è qualcosa che abbiamo aggiunto a discourse per consentire ai plugin di aggiungere rotte all’applicazione ember. La sintassi all’interno di map() è molto simile al router di Ember. In questo caso la nostra mappa delle rotte è molto semplice, dichiara solo una rotta chiamata purple-tentacle sotto /admin/plugins.

Infine, aggiungiamo le nostre stringhe di traduzione:

config/locales/client.en.yml

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

Se riavvii il tuo server di sviluppo, dovresti essere in grado di visitare /admin/plugins e vedrai il nostro link! Se ci fai clic, vedrai il pulsante per mostrare il nostro tentacolo viola:

Sfortunatamente, quando fai clic sul pulsante, non succede nulla :frowning:

Se guardi nella console degli sviluppatori, dovresti vedere un errore che fornisce un indizio sul perché:

Uncaught Error: Nothing handled the action 'showTentacle'

Ah sì, la ragione è nel nostro template handlebars stiamo dipendendo da un paio di cose:

  1. Che quando l’utente fa clic sul pulsante, venga chiamata showTentacle.
  2. showTentacle dovrebbe impostare la proprietà tentacleVisible su true in modo che l’immagine venga visualizzata.

Se non hai letto le Guide Ember sui Controller ora è un buon momento per farlo, perché implementeremo un controller per il nostro template purple-tentacle che gestirà questa logica.

Crea il seguente file:

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

E ora, quando aggiorniamo la nostra pagina, facendo clic sul pulsante viene visualizzato il nostro personaggio animato!

Lascerò come esercizio extra al lettore aggiungere un pulsante che nasconda il tentacolo quando viene cliccato :smile:

Se hai problemi a far funzionare la tua versione di questo plugin, l’ho caricata su github.


Altro nella serie

Parte 1: Basi dei plugin
Parte 2: Plugin Outlets
Parte 3: Impostazioni del sito
Parte 4: Configurazione git
Parte 5: Questo argomento
Parte 6: Test di accettazione
Parte 7: Pubblica il tuo plugin


Questo documento è controllato in versione - suggerisci modifiche su github.

28 Mi Piace

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 Mi Piace

@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 Mi Piace

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 Mi Piace

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 Mi Piace

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 Mi Piace

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 Mi Piace

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 Mi Piace

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 Mi Piace

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 Mi Piace

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.