Développement de plugins Discourse - Partie 1 - Créer un plugin basique

Hmmmm. That’s a tricky one, because the symlink needed to be deleted, otherwise the Docker mount fails (as it cannot overwrite the existing file).

I had a bit of a google around for solutions, and found an interesting solution which would dereference the symlink and copy the symlink’s target into the specified directory, but couldn’t find anything that would create a Docker volume mount, thus preserving the dynamic link and bi-directional synchronisation. Happy to be steered towards a solution as this would be a neat feature to have.

Tell me how to add your validation to the app/models /user.rb model?

1 « J'aime »

You can use regular Ruby code to do this in your plugin.rb:

class ::User < ActiveRecord::Base
  validates_presence_of :your_attribute
end
6 « J'aime »

Just starting on my first small plugin :blush: I have a few questions I hope someone can help with…

First question is does it matter if we use what’s in this guide or should we really be aiming to use the plugin generator?

It wasn’t covered in this guide, but how can we make data available to our templates from the plugin.rb file?

Let’s say for example I want to display a random welcome message at the plugin-outlet topic-above-posts whenever somebody visits a topic, with the message being randomly selected from an array in plugin.rb… how would I send that data to the template? Which I also presume would go here:

plugins/my_new_plugin/assets/javascripts/discourse/templates/connectors/topic-above-posts/my_new_plugin.hbs (Is this correct?)

Any tips greatly appreciated :blush:

2 « J'aime »

Using the plugin generator is generally a great way to get started and see how a plugin should be structured. I recommend it.

Now, regarding how to get ruby code into the front end: those are two different applications. The front end application (Ember) will have to request it from the server (Rails) somehow. Normally we do this via an AJAX call, but you could also do something like add to the SiteSerializer so that it’s sent automatically as part of the forum data.

Otherwise you will have to look up how to add a route/controller in rails and send the messages as JSON.

8 « J'aime »

Thanks Robin!

Would you know if there are any tutorials on this? Or, is there a simple plugin (or even a dummy plugin) we can look at that points us in the right direction? (If not, is this something you could quickly put up for us somewhere please? I think it’ll help a lot of people :blush:)

Plus… any more thoughts on writing a book? I can put you in touch with someone if you fancy it :smiley:

1 « J'aime »

Thanks for the compliment but I’m definitely far too busy to consider writing a book. Plus tech books have SUPER limited range. Those I know who have written them say you do it for yourself, not for the money :slight_smile:

I can’t think of simple plugin for returning something from the server side. discourse-tooltips adds a route and then uses that to get previews of topics as you mouseover so that might be helpful.

8 « J'aime »

Bientôt, je commencerai le développement de plugins…

Question :

Existe-t-il une liste de hooks dans lesquels nous pouvons réellement insérer un plugin ?

Par exemple, si nous voulons écrire un plugin pour traiter les données cuites dans un post avant qu’elles ne soient affichées, nous nous attendrions à ce qu’il y ait au moins un hook au début et un à la fin, comme (par exemple) :

  • display_post_start

  • display_post_complex

Ensuite, notre plugin pourrait s'insérer dans le code à l’emplacement du hook de notre choix ci-dessus.

Existe-t-il une liste de ces hooks de plugin et comment sont-ils appelés dans DiscourseWorld ?

Mise à jour : J’ai trouvé ceci :

git grep "plugin-outlet" -- "*.hbs"

Mais je n’ai pas vu d’outlet pour modifier le contenu cuit d’un post.

Existe-t-il un tutoriel sur la création de nos propres outlets, par exemple un outlet pour modifier la partie cuite des posts ?

2 « J'aime »

Vous recherchez ce guide, section decorateCooked() :

5 « J'aime »

J’utilise ce squelette pour un plugin personnalisé. Dois-je changer quelque chose pour la transition à venir vers Ember 5 ?

Je suppose que non ? Bien que ce ne soit qu’un linter ?

1 « J'aime »

Le squelette est compatible avec Ember 5. Mais si vous avez ajouté du code JavaScript ou des modèles HBS, vous devrez vérifier les dépréciations comme décrit dans l’annonce.

Oui, les éléments dans package.json sont uniquement liés au linting, ils n’affectent pas la fonctionnalité ou la compatibilité d’un plugin.

3 « J'aime »

Veuillez ajouter à ceci

rake plugin:create[plugin-name]

J’ai passé trop de temps aujourd’hui à essayer de trouver Automating Discourse Plugin Setup with `create-discourse-plugin` gem (je ne sais pas comment j’ai pu oublier que c’était le nom de la tâche rake ?! :person_shrugging: )

5 « J'aime »

J’ajouterais également une note lors de l’utilisation de la configuration Docker, car les instructions ci-dessus échoueront avec une erreur similaire à

ambientimpact:~/.../Discourse/discourse$ d/rake plugin:create[neurocracy-date-rewrite]
Cloning 'https://github.com/discourse/discourse-plugin-skeleton' to '/src/plugins/neurocracy-date-rewrite'...
Initializing git repository...
Initialized empty Git repository in /src/plugins/neurocracy-date-rewrite/.git/
Author identity unknown

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: empty ident name (for <discourse@localhost>) not allowed
rake aborted!
Command failed with exit 128: git
/src/lib/tasks/plugin.rake:354:in `system'
/src/lib/tasks/plugin.rake:354:in `block (2 levels) in <main>'
/src/lib/tasks/plugin.rake:344:in `chdir'
/src/lib/tasks/plugin.rake:344:in `block in <main>'
Tasks: TOP => plugin:create
(See full trace by running task with --trace)

Vous devez d’abord configurer votre .gitconfig à l’intérieur du conteneur (changez l’e-mail et le nom si nécessaire) avant d’essayer de créer le plug-in :

d/exec git config --global user.email "you@example.com"
d/exec git config --global user.name "Your Name"
d/rake plugin:create[plugin-name]
1 « J'aime »