Beginner's Guide to Creating Discourse Plugins Part 5: Admin Interfaces

plugins

(Robin Ward) #1

Previous tutorials in this series:

Part 1: Creating a basic plugin
Part 2: Plugin outlets
Part 3: Custom Settings
Part 4: git setup


Sometimes site settings aren’t enough of an admin interface for your plugin to work the way you want. For example, if you install the discourse-akismet plugin, you might have noticed that it adds a navigation item to the admin plugins section in of your Discourse:

In this tutorial we’ll show you how to add an admin interface for your plugin. I’m going to call my plugin purple-tentacle, in honor of one of my favorite computer games. Seriously, I really love that game!

Setting up the Admin Route

Let’s start by adding a plugin.rb like we’ve done in previous parts of the 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

The add_admin_route line tells Discourse that this plugin will need a link on the /admin/plugins page. Its title will be purple_tentacle.title from our i18n translations file and it will link to the purple-tentacle route.

The lines below that set up the server side mapping of routes for our plugin. One assumption Discourse makes is that almost every route on the front end has a server side route that provides data. For this example plugin we actually don’t need any data from the back end, but we need to tell Discourse to serve up something in case the user visits /admin/plugins/purple-tentacle directly. This line just tells it: ‘hey if the user visits that URL directly on the server side, serve the default plugins content!’

(If this is confusing don’t worry too much, we’ll come back to it in a future tutorial when we handle server side actions.)

Next, we’ll add a template that will be displayed when the user visits the /admin/plugins/purple-tentacle path. It will just be a button that shows an animated gif of purple tentacle when the user clicks a button:

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'>
  {{d-button label="purple_tentacle.show" action="showTentacle" icon="eye"}}
</div>

If you’ve learned the basics of handlebars the template should be pretty simple to understand. The d-button is a component in discourse we use for showing a button with a label and icon.

To wire up our new template we need to create a route map:

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

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

A route map is something we added to discourse to make it so that plugins could add routes to the ember application. The syntax within map() is very similar to Ember’s router. In this case our route map is very simple, it just declares one route called purple-tentacle under /admin/plugins.

Finally, let’s add our translation strings:

config/locales/client.en.yml

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


If you restart your development server, you should be able to visit /admin/plugins and you’ll see our link! If you click it, you’ll see the button to show our purple tentacle:

Unfortunately, when you click the button, nothing happens :frowning:

If you look at your developer console, you should see an error that provides a clue to why this is: Uncaught Error: Nothing handled the action 'showTentacle'

Ah yes, the reason is in our handlebars template we are depending on a couple of things:

  1. That when the user clicks the button, showTentacle will be called.
  2. showTentacle should set the property tentacleVisible to true so that the image shows up.

If you haven’t read the Ember Guides on Controllers now is a good time to do so, because we’ll implement a controller for our purple-tentacle template that will handle this logic.

Create the following file:

assets/javascripts/discourse/controllers/admin-plugins-purple-tentacle.js.es6

export default Ember.Controller.extend({
  tentacleVisible: false,

  actions: {
    showTentacle() {
      this.set('tentacleVisible', true);
    }
  }
});

And now when we refresh our page, clicking the button shows our animated character!

I’ll leave it as an extra exercise to the reader to add a button that hides the tentacle when clicked :smile:

If you are having trouble getting your version of this plugin working, I’ve pushed it to github.


More in the series

Part 1: Plugin Basics
Part 2: Plugin Outlets
Part 3: Site Settings
Part 4: git setup
Part 5: This topic
Part 6: Acceptance tests
Part 7: Publish your plugin


Creating a route in a plugin
Unofficial community docs - learndiscourse.org
Beginner’s Guide to Creating Discourse Plugins Part 6: Acceptance Tests
Add custom route from initializer
Creating Routes in Discourse and Showing Data
Plugin Controller Method never called
Beginner’s Guide to Creating Discourse Plugins Part 7: Publish your plugin
Beginner's Guide to Creating Discourse Plugins - Part 1
Beginner's Guide to Creating Discourse Plugins - Part 1
Plugin Outlet Locations
Adding Dynamic Routes in a Plugin
Seeking a better way to organize plugin site settings
Extending nested resource routes
Allow reply-to individual instead of topic/forum (mailing list feature)
Rails Girls 2015 SoC Banter
Simplified Chinese Translation General / 简体中文翻译
#2

Thanks for this, though the github link appears broken!


(Mittineague) #3

Yes, he linked to “discourse” but it is under "eviltrout"


(Régis Hanol) #4

Thanks @eufunium & @Mittineague. I fixed the link :kissing_heart:


(Sarah Ni) #5

Hm, doesn’t seem to work on my local side… did anyone face the same problem?


(Robin Ward) #6

What’s the problem? If you install the purple tentacle plugin does it not work?


(Sarah Ni) #7

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.



(Sam Saffron) #8

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.


(Sarah Ni) #9

@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


(Sam Saffron) #10

I usually only delete the cache directory, no need to nuke the entire tmp directory.


(Mittineague) #11

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


(Kane York) #12

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).


(Robin Ward) #13

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.


#14

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"

Beginner's Guide to Creating Discourse Plugins - Part 1
(Mittineague) #15

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


Beginner's Guide to Creating Discourse Plugins - Part 1
#16

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.


#17

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


(Mittineague) #18

Did you stop vagrant, clear the cache
$> rm -rf tmp
and restart vagrant?


#19

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.


(Mittineague) #20

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