Wijzig het standaardgedrag van de knop "Nieuw onderwerp"

AI-generated summary

In a Dev category thread titled “Modify ‘New Topic’ button default behavior,” jotamartos seeks help modifying the “New Topic” button’s behavior in Discourse 2.6.1, having previously succeeded in version 1.8.0. Their plugin overrides Ember actions via controller reopens, but the main site’s button no longer responds. pfaffman offers a tangential resource on custom fields, which jotamartos clarifies doesn’t address their need to run custom JavaScript on button click. After further investigation, jotamartos resolves the issue by updating the controller import to discourse/components/d-navigation and overriding the clickCreateTopicButton action instead of createTopic. The fix works for both the main navigation and search results, restoring their custom menu functionality. pfaffman celebrates the solution, acknowledging the ongoing mystery of Discourse’s front-end structure.

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

Can anybody help me?

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.

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.

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.