Desarrollando plugins para Discourse - Parte 5 - Agregar una interfaz de administración

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


A veces, los ajustes del sitio no son suficientes como interfaz de administración para que tu plugin funcione como deseas. Por ejemplo, si instalas el plugin discourse-akismet, es posible que hayas notado que agrega un elemento de navegación en la sección de plugins de administración de tu Discourse:

En este tutorial te mostraremos cómo agregar una interfaz de administración para tu plugin. Voy a llamar a mi plugin purple-tentacle, en honor a uno de mis videojuegos favoritos. En serio, realmente amo ese juego.

Configurando la ruta de administración

Comencemos agregando un archivo plugin.rb, como hemos hecho en partes anteriores del tutorial.

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

La línea add_admin_route le indica a Discourse que este plugin necesitará un enlace en la página /admin/plugins. Su título será purple_tentacle.title desde nuestro archivo de traducciones i18n y se vinculará a la ruta purple-tentacle.

Las líneas siguientes configuran la asignación del lado del servidor de las rutas para nuestro plugin. Discourse asume que casi todas las rutas en el frontend tienen una ruta del lado del servidor que proporciona datos. Para este plugin de ejemplo, en realidad no necesitamos ningún dato del backend, pero debemos decirle a Discourse que sirva algo en caso de que el usuario visite /admin/plugins/purple-tentacle directamente. Esta línea simplemente le dice: «¡oye, si el usuario visita esa URL directamente en el lado del servidor, sirve el contenido predeterminado de plugins!».

(Si esto te confunde, no te preocupes demasiado; volveremos sobre esto en un tutorial futuro cuando manejemos acciones del lado del servidor).

A continuación, agregaremos una plantilla que se mostrará cuando el usuario visite la ruta /admin/plugins/purple-tentacle. Será solo un botón que muestra un gif animado de un tentáculo púrpura cuando el usuario hace clic en el botón:

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>

Si has aprendido los fundamentos de Handlebars, la plantilla debería ser bastante fácil de entender. El componente <DButton /> es un componente en Discourse que usamos para mostrar un botón con una etiqueta y un ícono.

Para conectar nuestra nueva plantilla, necesitamos crear un mapa de rutas:

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

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

Un mapa de rutas es algo que agregamos a Discourse para permitir que los plugins agreguen rutas a la aplicación Ember. La sintaxis dentro de map() es muy similar a la ruta de Ember. En este caso, nuestro mapa de rutas es muy simple; solo declara una ruta llamada purple-tentacle bajo /admin/plugins.

Finalmente, agreguemos nuestras cadenas de traducción:

config/locales/client.en.yml

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

Si reinicias tu servidor de desarrollo, deberías poder visitar /admin/plugins y verás nuestro enlace. Si haces clic en él, verás el botón para mostrar nuestro tentáculo púrpura:

Desafortunadamente, cuando haces clic en el botón, no sucede nada :frowning:

Si miras tu consola de desarrollador, deberías ver un error que proporciona una pista sobre por qué ocurre esto:

Uncaught Error: Nothing handled the action 'showTentacle'`

Ah, sí, la razón es que en nuestra plantilla dependemos de un par de cosas:

  1. Que cuando el usuario haga clic en el botón, se llame a showTentacle en el controlador.
  2. Que showTentacle establezca la propiedad tentacleVisible en true para que aparezca la imagen.

Si aún no has leído las Guías de Ember sobre Controladores, ahora es un buen momento para hacerlo, ya que implementaremos un controlador para nuestra plantilla purple-tentacle que manejará esta lógica.

Crea el siguiente archivo:

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

Y ahora, cuando refrescamos nuestra página, al hacer clic en el botón se muestra nuestro personaje animado.

Dejo como un ejercicio adicional para el lector agregar un botón que oculte el tentáculo cuando se haga clic en él :smile:

Si tienes problemas para hacer funcionar tu versión de este plugin, lo he subido a GitHub.


Más en la serie

Parte 1: Conceptos básicos de plugins
Parte 2: Salidas de plugins
Parte 3: Ajustes del sitio
Parte 4: Configuración de git
Parte 5: Este tema
Parte 6: Pruebas de aceptación
Parte 7: Publica tu plugin


Este documento está controlado por versiones; sugiere cambios en GitHub.

28 Me gusta

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 me gusta

@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 Me gusta

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 me gusta

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 Me gusta

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 Me gusta

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 me gusta

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 Me gusta

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 me gusta

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 Me gusta

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.