Desenvolvendo Plugins Discourse - Parte 2 - Conectar a um outlet de plugin

Tutorial anterior: Developing Discourse Plugins - Part 1 - Create a basic plugin


Primeiros Passos: Templates Handlebars

A aplicação cliente do Discourse é escrita usando a estrutura Javascript Ember.js. O Ember usa Handlebars para todos os templates HTML. Há uma ótima introdução à linguagem de template naquele link, então, definitivamente, leia-o com atenção.

O Problema: Adicionar elementos à Interface do Usuário do Discourse

Muitos plugins precisam adicionar e estender a interface web do Discourse. Nós fornecemos um mecanismo para fazer isso chamado plugin outlets em templates handlebars.

Se você navegar pelos templates handlebars do discourse, frequentemente verá a seguinte marcação:

<PluginOutlet @name="edit-topic" />

Isso declara um plugin outlet chamado “edit-topic”. É um ponto de extensão no template que os autores de plugins podem alavancar para adicionar sua própria marcação handlebars.

Ao criar seu plugin, procure nos templates handlebars do discourse (em arquivos .hbs) onde você deseja fazer alterações por um <PluginOutlet />. Se não houver um, basta nos pedir para estendê-lo! Teremos prazer em adicioná-los se você tiver um bom caso de uso. Se você quiser tornar isso mais fácil e rápido para nós, envie um pull request no github!

:exclamation: Se você quiser ver alguns dos lugares onde os plugin outlets existem, você pode executar o seguinte comando se estiver no OSX ou Linux:

git grep "<PluginOutlet" -- "*.hbs"

Você também pode exibir os plugin outlets em um site Discourse ativando a Discourse Developer Toolbar. Basta digitar enableDevTools() no console do navegador em um fórum Discourse e clicar no ícone de plugue que aparece no lado esquerdo da página.

Conectando-se a um Plugin Outlet

Depois de encontrar o plugin outlet ao qual deseja adicionar, você precisa escrever um connector para ele. Um connector é realmente apenas um template handlebars cujo nome de arquivo inclui connectors/<nome do outlet> nele.

Por exemplo, se o template handlebars do Discourse tiver:

<PluginOutlet @name="evil-trout" />

Então, quaisquer arquivos handlebars que você criar no diretório connectors/evil-trout serão anexados automaticamente. Então, se você criou o arquivo:

plugins/hello/assets/javascripts/discourse/connectors/evil-trout/hello.hbs

Com o conteúdo:

<b>Hello World</b>

O Discourse inseriria <b>Hello World</b> naquele ponto do template.

Note que chamamos o arquivo de hello.hbs – A parte final do nome do arquivo não importa, mas deve ser única em todos os plugins. É útil nomeá-lo com algo descritivo sobre o que você está estendendo. Isso tornará a depuração mais fácil no futuro.

Solução de Problemas

  • Verifique novamente o nome do connector e certifique-se de que ele corresponda perfeitamente ao nome do plugin.

Mais informações


Mais na série

Parte 1: Noções Básicas de Plugins
Parte 2: Este tópico
Parte 3: Configurações do Site
Parte 4: Configuração do git
Parte 5: Interfaces de Administrador
Parte 6: Testes de Aceitação
Parte 7: Publique seu plugin


Este documento é controlado por versão - sugira alterações no github.

38 curtidas

Is there a list of plugin outlets?

More specifically I would like to know if one could modify the “create topic” template?

1 curtida

In what way? Add new fields to the composer area (like the tagging plugin does)?

Yes, that’s what I am searching for.

I’d take a look at the tagging plugin then. It might have all you want!

2 curtidas

@eviltrout is there a way to output directly to JSON.
basically creating a new API endpoint?

Ahhhhhhhh. new to this whole “ruby fad” :smiley:
I have a partial solution that i’ll detail in:

Hi Robin,

The plugin-outlet-locations plugin seems to declare all the connectors with a register_asset line in plugin.rb. Starting here: discourse-plugin-outlet-locations/plugin.rb at master · Mittineague/discourse-plugin-outlet-locations · GitHub

Is that a necessary step?

Nope - that’s not necessary. All .es6 and .hbs files are loaded automatically in plugins.

4 curtidas

I am still trying to figure out how plugins in Discourse work and I could use a little help.

I would like to modify the timeline scroll bar (in the right side of each topic). Actually, what I need to do is add some stuff (image/banner) below the scroll bar.
What template should I edit? Is there an existing plugin-outlet for that?

Thank you,
Ilias

2018-4-28 work for me!

1. Plugin Outlet name

2. Code

3. Result

7 curtidas

This guide is great! I had my plugin showing in the /admin/plugins quite easily.

I was wondering though, how do you delete items from a template?

For privacy reasons, I wanted to delete the export to csv button in the /admin/users/list/active as well as the show emails button. But I didn’t find a plugin outlet connector for those parts of the UI.

I was trying to overwrite the following templates

/users-list-show.hbs
/users-list.hbs

with my plugin outlet.

But creating these files
/var/www/discourse/plugins/[my-plugin]/assets/javascripts/admin/templates/users-list-show.hbs
/var/www/discourse/plugins/[my-plugin]/assets/javascripts/admin/templates/users-list.hbs
and making changes to them didnt seem to work. Even after deleting the /var/www/discourse/tmp folder and restarting the server.

Is this the correct way to delete those buttons from the UI? Using css display: none; isn’t an option.

I feel like I am missing something really simple, any ideas?

Not a direct technical answer to your question but why would you want to do that?

Only the admins can do this. They usually have full control of the server most likely anyway and could read the database, so hiding a button is a bit futile: emails will also show up in the Email logs and on your mail service logs. I’m afraid site admins are going to see a lot of email addresses!

Admins have full control, they see everything (except passwords presumably), so this is normal.

If you are an admin, get used to the responsibility which comes with knowing all of this!

My concern was that moderators can access some things in the admin dashboard.
They can navigate to the ‘users’ tab and see the Export and the Show Emails buttons
image

We don’t want our moderators to be able to grab bulk emails like this.
I understand that the emails are visible on users profiles, but we didn’t want an easy way to get this information.

So I was trying to create a plugin that hides these buttons.

Could be wrong, but this suggests it’s not an issue … have you Impersonated a Mod?

Yes, the screenshot I took was from impersonating a mod.

We have a similar setup as the link you provided (Moderators ability to see emails inconsistent).

I’m not requesting the Discourse team makes any changes though, this isn’t an issue with Discourse.

I am trying customize Discourse for our situation. I was hoping that using a plugin outlet and referencing the files within that plugin was the right way to overwrite a template in Discourse. I just wasn’t able to successfully overwrite these files:

/users-list-show.hbs
/users-list.hbs

My changes never showed up. So I thought this plugin outlet topic could help

1 curtida

Perhaps someone else can chime in, but I’ve always seen Outlets as additive. You may need an override technique instead (via javascript/Ember). I’m still learning this myself :).

2 curtidas

I’m thinking that if overriding a template is the goal that it might be better to do this with a theme instead of a plugin.

I was thinking that as well, but I didn’t want a user to swap their theme and be able to get different functionality. So I figured a plugin outlet would do the trick

I think you can probably hide those with CSS.

2 curtidas