[Solved] In plugin: opening the composer without changing the route

I would like to open the composer from my plugin. But I cannot use the composing a new pre-filled topic via URL method, because I don’t want to change the route.

I’ve tried the method explained here, but it doesn’t seem to work. I’ve tried to send the composer:opened event to other objects (router, application route, application controller, composer controller) but without any success.

I’ve been more successful with the method described here, using this kind of code:

container.lookup('controller:composer').open({			
	action: Composer.CREATE_TOPIC,
	draftKey: 'new_topic'
})

The composer opens correctly, but:

  • the preview pane doesn’t seem to work
  • I get the following errors in the console:
Uncaught SyntaxError: Unexpected token <										markdown-it-bundle.js:1
Error: Could not find module pretty-text/engines/discourse-markdown/helpers		discourse/lib/text:40 
    at missingModule (discourse-loader:134)
    at require (discourse-loader:149)
    at setup (pretty-text/engines/discourse-markdown-it:242)
    at buildOptions (pretty-text/pretty-text:100)
    at getOpts (discourse/lib/text:25)
    at cook (discourse/lib/text:30)
    at eval (discourse/lib/text:38)
    at tryCatch (ember:50180)
    at invokeCallback (ember:50195)
    at publish (ember:50163)

Any idea?

You need to use the api.modifyClass plugin api in an initializer

withPluginApi("0.8", api => {
  api.modifyClass("controller:composer", {
    action: Composer.CREATE_TOPIC,
   draftKey: 'new_topic'
  });
});
1 Like

Thanks a lot @zogstrip.

Your code runs correctly but the composer doesn’t show up. Additionally, I get the following warning in the console:

"controller:composer" was already cached in the container. Changes won't be applied.

Does it makes sense to you?

It does, I added that warning yesterday because I got bitten by it.

It means that you (or another plugin) have previously called container.lookup("controller:composer") which cached the instance of the composer controller on the container and any changes made to the composer class (through modifyClass or reopen) won’t apply.

If you did the lookup, then you need to move the modifyClass before it.
If you didn’t, you need to put your initializer before the one calling it.

4 Likes

Thanks, got it, I’ve fixed the warning.
However, I’ve just realized the markdown error is not linked to me wanting to open the composer from my plugin. I’m ashamed I didn’t check earlier, but the problem occurs also when creating a new topic using Discourse UI.
I’m about to report this issue in another topic, so that people landing here are not too confused.
EDIT: here is the new topic.