How do I disable the attribute disabled, cannotCreateTopicOnCategory from the 'New Topic' button?


(Ivan) #1

Hi there,

So I currently have the +New Topic in the header of my Discourse, just to the right of the user avatar. What I’m trying to do is enable to a user to create a topic, whilst viewing another topic (that’s the way our site works). I am overriding headers.hbs (via a plugin) and have inserted the following code to do so:

       {{#if currentUser}}
          {{d-button  id="create-topic"
                class="btn-default"
                action="createTopic"
                icon="plus"
                label="topic.create"
                disabled=cannotCreateTopicOnCategory}}
        {{/if}}

I have tried removing disabled=cannotCreateTopicOnCategory (though I have left in here), but this doesn’t work. Any ideas?

Thanks!


(Jeff Atwood) #2

Not sure, I know @rewphus had this configured for his site.


#3

Hmmm, I thought I just reformatted my +New Topic button to be fixed to the bottom right corner of the screen, but it still only appears when you are in the topic list or category list, but not while in a topic itself, which is what I think you are trying to do, correct?

I can definitely play with it and see if I can figure it out though.


(Jeff Atwood) #4

Ah my bad. A few people have asked for this though so we should work up a bit of JS and post it here on meta one of these days @techapj.


(Arpit Jalan) #5

Simply adding the “New Topic” button template in header.hbs is not enough in this case.

Here is the JS code you are looking for:

<script>
  var ApplicationRoute = require('discourse/routes/application').default;    
  ApplicationRoute.reopen({
    actions: {
      createTopic: function() {
          var Composer = require('discourse/models/composer').default;
          composerController = Discourse.__container__.lookup('controller:composer');
          composerController.open({ action: Composer.CREATE_TOPIC, draftKey: Composer.DRAFT });
      },
    }
  });
</script>
  
<script type='text/x-handlebars' data-template-name='/connectors/header-after-home-logo/create-new-topic'>
  {{#if currentUser}}
    {{d-button  id="create-topic"
                  class="btn-default"
                  action="createTopic"
                  icon="plus"
                  label="topic.create"
                  disabled=cannotCreateTopicOnCategory}}
  {{/if}}
</script>

Paste the above code in Admin :arrow_right: CSS/HTML :arrow_right: </body> section.


:sparkles: also add this custom CSS so that the button looks nice :sparkles:

.create-new-topic {
    float: right;
    padding: 5px 0px 0px 5px;
}