Discourse Plugins entwickeln - Teil 1 - Ein Basis-Plugin erstellen

Das Erstellen eines Plugins in Discourse kann ganz einfach sein, sobald man ein paar Eigenheiten gelernt hat. Das Ziel dieses Beitrags ist es, ein Skelett-Plugin zu erstellen und Sie mit den Grundlagen vertraut zu machen.

Ihre Entwicklungsumgebung

Stellen Sie sicher, dass Sie eine Entwicklungsumgebung von Discourse auf Ihrem Computer installiert haben. Ich empfehle Ihnen, die entsprechende Einrichtungsanleitung zu verwenden und erst zurückzukommen, wenn Sie fertig sind.

plugin.rb

:tada: Verwenden Sie GitHub - discourse/discourse-plugin-skeleton: Template for Discourse plugins, um ein vollständiges Discourse-Plugin-Skelett in Ihrem Plugins-Verzeichnis zu erstellen :tada:

Das Skelett ist jetzt in den Discourse-Kern integriert. rake plugin:create[plugin-name] erstellt ein Plugin mithilfe des Skeletts.

Wenn Discourse startet, sucht es im plugins-Verzeichnis nach Unterverzeichnissen, die eine plugin.rb-Datei enthalten. Die plugin.rb-Datei hat zwei Zwecke: Sie dient als Manifest für Ihr Plugin mit den erforderlichen Informationen über Ihr Plugin, einschließlich: Name, Kontaktinformationen und einer Beschreibung. Der zweite Zweck besteht darin, jeglichen Ruby-Code zu initialisieren, der für die Ausführung Ihres Plugins notwendig ist.

In unserem Fall werden wir keinen Ruby-Code hinzufügen, aber wir benötigen die plugin.rb trotzdem. Erstellen wir das Verzeichnis basic-plugin mit der Datei plugin.rb darin, mit folgendem Inhalt:

basic-plugin/plugin.rb

# name: basic-plugin
# about: Ein super einfaches Plugin, um zu demonstrieren, wie Plugins funktionieren
# version: 0.0.1
# authors: Awesome Plugin Developer
# url: https://github.com/yourusername/basic-plugin

Sobald Sie diese Datei erstellt haben, sollten Sie Ihren lokalen Server neu starten und das Plugin sollte geladen werden.

Ein wichtiger Stolperstein!

Wenn Sie an die normale Rails-Entwicklung gewöhnt sind, werden Sie vielleicht feststellen, dass Plugins beim Neuladen nicht ganz so komfortabel sind. Im Allgemeinen sollten Sie beim Ändern Ihres Plugins den Server mit Ctrl+c stoppen und ihn dann mit bin/ember-cli -u erneut starten.

Meine Änderungen wurden nicht übernommen! :warning:

Manchmal wird der Cache nicht vollständig gelöscht, insbesondere wenn Sie neue Dateien erstellen oder alte löschen. Um dieses Problem zu umgehen, entfernen Sie Ihren tmp-Ordner und starten Sie Rails neu. Auf einem Mac können Sie dies mit einem Befehl erledigen: rm -rf tmp; bin/ember-cli -u.

Überprüfen, ob Ihr Plugin geladen wurde

Nachdem Sie Ihren lokalen Server neu gestartet haben, besuchen Sie die URL /admin/plugins (stellen Sie sicher, dass Sie sich zuerst als Admin-Konto angemeldet haben, da nur Admins die Plugin-Registrierung sehen können).

Wenn alles funktioniert hat, sollten Sie Ihr Plugin in der Liste sehen:

Herzlichen Glückwunsch, Sie haben gerade Ihr erstes Plugin erstellt!

Fügen wir etwas Javascript hinzu

Im Moment macht Ihr Plugin nichts. Fügen wir eine Javascript-Datei hinzu, die ein Alert-Fenster anzeigt, wenn Discourse geladen wird. Dies wird für jeden Benutzer super nervig sein und wird nicht als tatsächliches Plugin empfohlen, zeigt aber, wie man Javascript in unsere laufende Anwendung einfügt.

Erstellen Sie die folgende Datei:

plugins/basic-plugin/assets/javascripts/discourse/initializers/alert.js

export default {
  name: "alert",
  initialize() {
    alert("alert boxes are annoying!");
  },
};

Wenn Sie nun Ihren lokalen Server neu starten, sollte „alert boxes are annoying!“ auf dem Bildschirm erscheinen. (Falls dies nicht der Fall war, siehe die Überschrift „Meine Änderungen wurden nicht übernommen“ oben).

Lassen Sie uns Schritt für Schritt durchgehen, wie das funktioniert hat:

  1. Javascript-Dateien, die in assets/javascripts/discourse/initializers abgelegt werden, werden automatisch ausgeführt, wenn die Discourse-Anwendung hochfährt.

  2. Diese spezielle Datei exportiert ein Objekt, das einen name und eine initialize-Funktion hat.

  3. Der name muss eindeutig sein, ich habe ihn einfach alert genannt.

  4. Die Funktion initialize() wird aufgerufen, wenn die Anwendung geladen wird. In unserem Fall führt sie nur unseren alert()-Code aus.

Sie sind jetzt ein offizieller Discourse-Plugin-Entwickler!


Mehr in der Serie

Teil 1: Dieses Thema
Teil 2: Plugin Outlets
Teil 3: Site Settings
Teil 4: git setup
Teil 5: Admin interfaces
Teil 6: Acceptance tests
Teil 7: Publish your plugin


Dieses Dokument wird versioniert – schlagen Sie Änderungen auf github vor.

109 „Gefällt mir“
Plugin API documentations?
How to create a new plugins?
How do you learn to build Discourse plugins?
Connecting to database with a plugin
Separating View from Data, Plugins, Modularity
Categories Topic Replies
Added custom initializer
Overwriting controllers in plugin
Create fully custom header
How do you learn to build Discourse plugins?
How can I add "dislike" button?
Which files can you override from a plugin?
Developing Discourse Plugins - Part 2 - Connect to a plugin outlet
Developing Discourse Plugins - Part 5 - Add an admin interface
(Superseded) Plugin Tutorial #1 - How to manipulate the text in the composer?
How to properly display jalali dates for Persian language
Topic Ratings Plugin
Enable Like-Button for not logged-in visitors
Why do I need a block storage?
Custom Field not working
Rails plugin generator
Why GNU License?
Code reading help. ES6, plugin-outlet `_connectorCache`, custom-html `_customizations`, appEvents
Customizing handlebars templates
Application Files after Digital Ocean Setup
Application Files after Digital Ocean Setup
Application Files after Digital Ocean Setup
Make an external http request from discourse
Specify user by external id
How can I make my own Discourse plugins?
Reputation and level on member profile
How to Change User Profile Picture
Something like shortcodes for Discourse?
Calendar plugin features to make it really useful for us
Plugin Documentation Style Guide
Any options for over-riding the username restrictions?
How would updating effect custom overrides?
I would like to change the template completely
Adding a second tag description
Rails plugin generator
Creating a plugin
Fatal: Not a git repository (or any parent up to mount point /discourse)
Fatal: Not a git repository (or any parent up to mount point /discourse)
Adding command line tools support for user api keys
Discourse Code Modification for personal use
Plugin routing problems
[PAID] Mentorship needed
Can You Restrict Signups Using `swot` Email Verification?
Plugin API documentations?
Insert tag based upon word match
Triggering account creation/login on external service when a user logs in on discourse
Place for total coding beginners to learn how to customise?
Add additional button post menu
Fetch third party data for topic list
The Ignore User feature (now in 2.3.0.beta7)
Help with a plugin
Create Custom APIs
Proper Development and Deployment
How to make only a group of users can send messages to staff
How might we better structure #howto?
Theme-Component v Plugin: What's the difference
Creating Parsing Extensions
How can I customized my own site
All latest images in posts from a category
(Ruby) Selecting only posts that are made by non-original posters
Hosting discourse on our infrastructure AWS
Display FullName instead of UserName in the Profile
Calling Python scripts w/ arguments on the backend via plugin?
Extend Existing Controller?
How to add custom fields to models
Extend Existing Controller?
Bot writer's tip: Processing every post
Add user to group after purchase
Support for proposed plug-in
Autocompletion for a user field at signup
Custom discourse with limited features
Overriding user_guardian.rb in a plugin (no fork necessary!)
Problems with git clone
Friendly user profile url
Website Integration
Discourse developement environment setup
How to show Full Name in Embeds
How to display full name in comments embeds
Rake task "assets:precompile" is failing due to JS Compilation issue
Change code does not work in VS Studio code editor
Edit Code
Discourse Chain Topics Plugin
Contributing to Discourse
How to edit topics/show.html.erb
Install Discourse on macOS for development
Developing Discourse Plugins - Part 3 - Add custom site settings
Developing Discourse Plugins - Part 4 - Setup git
Developing Discourse Plugins - Part 7 - Publish your plugin
Developing Discourse Plugins - Part 6 - Add acceptance tests
Learn how to start building stuff for Discourse if you're newbie (like myself)
How to disable updating username for admins & moderators?
Backend code
Backend code
Install Discourse on Ubuntu or Debian for Development
How to make changes on backend side of discourse application using rails
Introduction to Discourse Development
Discourse blog (article and comments)
How to learn more about Discourse back-end?
I want to learn how to program for Discourse
Discourse Audio Message
Create custom Automations
How can one add a toggle button to the post menu and implement an action for that toggle button in a Discourse plugin?
Content box
Create an advertising page
Discourse 有自定义插件模板吗,如何写自定义插件呢
Deploy custom Discourse repository via docker
Multiple custom fields for topics similar to custom userfields
How do I go about making a very customized theme?
How can I create a Discourse that's just like Stemaway.com?
Adding new columns to user lists in groups
How to pin a post within a topic?
Ruby script to send automatic welcome message to new users
:cn: Installing the Discourse development environment on Ubuntu 在 Ubuntu 上安装 Discourse 开发环境
Store pdf and doc files as raw text in the database - Where to start?
Admin not accessible because of auto-minify for Cloudflare
Update Password Encryption Method
I'm trying to run discourse app locally in production env without docker
How to turn off "Body seems unclear, is it a complete sentence?'
Hosting dynamic pages with discourse
Invisible way to force display order of tags?
Controllers and Routes
Learn about Discourse client-side flow?
Admin not accessible because of auto-minify for Cloudflare
Customizing CSS via plugin
About the Plugin category
Visual diagraming to add visual dimension to Conversations?
Custom Onebox engine
How can I install my own plugin written in Ruby?
Allow reply-to individual instead of topic/forum (mailing list feature)
Adapt github changes to my own site
Push to digital ocean from command line and rebuild
How to convince my university to use/install Discourse?
Hack to enable invitations for Trust level 1 users
Dropcaps in Discourse - cannot override span tag
Getting certain posts to Zapier
Plugin: add a menu icon (next to search)
Error When creating custom plugin
Error When creating custom plugin
IOTA as a currency for Discourse
New columns in directory (/users)
Bootstrap fails when plugin file specifies gem version range
Running my own discourse image
How to add a job to cron/anacron when creating Docker container
How to transfer data from plugin to app/views templates?
Which is Better? Discourse or Flarum?
Hide features for non-admin users through plugin
Customizing handlebars templates