Modify "New Topic" button default behavior

Hi everyone,

I’m trying to modify the default behavior of the “New Topic” button in Discourse 2.6.1. I have a custom plugin that modifies the default Ember actions to run my function. It worked properly when using a previous version of Discourse (1.8.0) but I can’t find how to get the same result in the new version. The code is the following one:

const NavigationDefaultController = require('discourse/controllers/navigation/default').default;
const SearchResultsDefaultController = require('discourse/controllers/full-page-search').default;
...

function myFunction() {
    CODE HERE
}
...
NavigationDefaultController.reopen({
  actions: {
    createTopic: myFunction,
  },
});

SearchResultsDefaultController.reopen({
  actions: {
    createTopic: myFunction,
  },
});

The first controller controller defined the main site’s actions (which doesn’t work in the new version) and the second one defined the actions to run when running a search in the app (and it works in the new version as well).

I saw that the “New Topic” buttons are created using the “createTopic” and “clickCreateTopicButton” actions but none of them work in the main site. Do I need to modify another action or do we need to use a different controller?

Thanks

1 Like

Can anybody help me?

1 Like

I feel your pain.

How to add a custom field to a topic is my best idea. It’s fairly far afield from your issue, but it’s well documented.

1 Like

Hi @pfaffman,

Thank you for the suggestion. However, if I’m not mistaken, that plugin allows you add a new custom field to a topic but what I want to do is to run my own javascript code when clicking on the “New Topic” button. I think my issue is basically related to the Discourse’s controller to use, the name of the function that is run and how to overwrite that function. Can you shed some light here?

Thanks

Well, darn. Sorry. I was thinking that it had some javascript code in it (e.g., in discourse-topic-custom-fields/assets/javascripts/discourse) that might be helpful in seeing how to wire that stuff up. I’m still not very good at the front end stuff, so I’m afraid that was my best free answer. :man_shrugging:

Thanks for your help @pfaffman. I finally found the controller and the action I needed to modify and the plugin is working again in Discourse 2.6.x

const NavigationDefaultController = require('discourse/components/d-navigation').default;
const SearchResultsDefaultController = require('discourse/controllers/full-page-search').default;
...

function myFunction() {
    CODE HERE
}
...
NavigationDefaultController.reopen({
  actions: {
    clickCreateTopicButton: myFunction,
  },
});

SearchResultsDefaultController.reopen({
  actions: {
    createTopic: myFunction,
  },
});

Note: This is the plugin’s code inside yourPlugin/assets/javascripts/discourse/initializers

Once I click on the “New Topic” button, my javascript code is run and users get the menu I built for the forum.

2 Likes

Great! So glad you got it! Just where things go and what to name them remains something of a mystery to me, though I’m getting better.