Je consulte le thème suivant à titre de référence :
Mon objectif est de trouver un moyen d’afficher un bouton « Nouveau sujet » secondaire qui s’adresse aux membres d’un groupe spécifique. Ce groupe a accès à une zone privée du forum, et je souhaiterais que ce bouton permette de démarrer un nouveau sujet dans cette zone, quelle que soit la page actuelle de l’utilisateur. Je cherche également à utiliser un modèle de sujet ou une fonctionnalité similaire de texte prérempli avec ce bouton. Je pense avoir compris comment ajouter sélectivement le bouton en fonction de la classe du groupe principal sur l’élément body, mais je ne suis pas encore assez familier avec la procédure pour insérer du texte prérempli (ou un modèle de sujet) dans les nouveaux sujets créés via ce bouton secondaire. Quelqu’un aurait-il des pistes ?
C’est possible. Vous devez d’abord préparer votre catégorie avec un modèle de sujet. Allez dans la catégorie que vous souhaitez utiliser et modifiez son modèle de sujet comme suit :
Il existe probablement un meilleur moyen de trouver l’ID de la catégorie, mais cette méthode fonctionne selon les besoins ici.
Maintenant, disons que je veux utiliser la catégorie bug. Son ID serait "1" selon /categories.json.
Pour #feature, ce serait "2", et ainsi de suite.
Maintenant que vous avez l’ID et que la catégorie dispose d’un modèle de sujet, vous pouvez copier le code du thème, apporter quelques modifications et l’ajouter à un composant de thème séparé.
J’ai effectué les modifications ici, vous n’avez donc qu’à copier ce code et à modifier les valeurs selon vos besoins :
<script type="text/discourse-plugin" version="0.8.18">
api.decorateWidget("header-buttons:after", helper => {
if (api.getCurrentUser()) {
// modifiez ces deux éléments selon vos préférences
var $ntb_text = "texte du bouton",
$ntb_icon = "plus",
// pas besoin de modifier ces éléments
$ntb_icon_class = ".fa-" + $ntb_icon,
$ntb_button_class = "btn btn-default btn btn-icon-text",
$ntb_button_helper = "button#new-create-topic-custom",
$ntb_icon_helper =
"i.fa" + $ntb_icon_class + ".d-icon .d-icon-" + $ntb_icon,
$ntb_label_helper = "span.d-button-label";
const createTopicCustom = function() {
const container = Discourse.__container__;
const Composer = require("discourse/models/composer").default;
const controller = container.lookup("controller:navigation/category");
const composerController = container.lookup("controller:composer");
composerController.open({
action: Composer.CREATE_TOPIC,
draftKey: Composer.DRAFT,
// définissez l'ID de la catégorie ci-dessous
// consultez site.com/categories.json
// pour référence
categoryId: 3
});
};
return helper.h(
$ntb_button_helper,
{
className: $ntb_button_class,
title: $ntb_text,
onclick: createTopicCustom
},
[helper.h($ntb_icon_helper), helper.h($ntb_label_helper, $ntb_text)]
);
}
});
</script>
Cela doit être placé dans la section <head> sous « Common ».
Une fois terminé, vous devriez avoir quelque chose comme ceci : (j’ai ajouté des marges avec CSS)
En cliquant sur le nouveau bouton, l’éditeur de message s’ouvrira avec la catégorie que vous avez sélectionnée, et le modèle de sujet de la catégorie sera également présent.
For a PM you’d need to set a few of the composer options differently.
Something like this:
(Same as above, in a theme component in the </head> section)
<script type="text/discourse-plugin" version="0.8.18">
api.decorateWidget("header-buttons:after", helper => {
if (Discourse.User.current()) {
// edit these two to your liking
var $ntb_text = "New Personal Message",
$ntb_icon = "heart",
// no need to edit these
$ntb_icon_class = ".fa-" + $ntb_icon,
$ntb_button_class = "btn btn-default btn btn-icon-text",
$ntb_button_helper = "button#new-create-topic-custom",
$ntb_icon_helper =
"i.fa" + $ntb_icon_class + ".d-icon .d-icon-" + $ntb_icon,
$ntb_label_helper = "span.d-button-label";
const createPM = function() {
const container = Discourse.__container__,
Composer = require("discourse/models/composer").default,
composerController = container.lookup("controller:composer");
composerController.open({
action: Composer.PRIVATE_MESSAGE,
// 1- set recipients (Case Sensitive) or comment the line below
usernames: "discobot,john,mike",
// 2- set title or comment the line below
topicTitle: "Title",
// 3- uncomment the line below to set a "template" for PM.
// Not really recommended will
// override any drafts everytime the
// button is pressed
// topicBody: "placeholder content",
// no need to change these
archetypeId: "private_message",
draftKey: Composer.NEW_PRIVATE_MESSAGE_KEY,
});
};
return helper.h(
$ntb_button_helper,
{
className: $ntb_button_class,
title: $ntb_text,
onclick: createPM
},
[helper.h($ntb_icon_helper), helper.h($ntb_label_helper, $ntb_text)]
);
}
});
</script>
Obviously, if you plan on using all three buttons you can reduce the size of the code quite drastically because most of it is shared by all three, but I kept things separate because some might just want to use one and not all three.
What this will do is add a button to the header like so:
I already left a comment in the code about setting any placeholder content in the body for PMs using this method. It’s not recommended unless you have a very specific template. This will override any previously saved PM drafts everytime you use the button.
That’s why it’s “off” by default in the code above.
I’ve tested this a bit and have not seen any problems. If you encounter anything let me know.
Love it. Worked perfectly. Thanks Joe, you’re awesome!
I noticed the user names are case sensitive. If a username’s case is wrong, the name appears in the recipient list, but the PM won’t send. Suggest you add this to your comments: -
// 1- set recipients (Case Sensitive) or comment the line below
usernames: "discobot,john,mike",
Hi @Johani - thank you, I have implemented this, but a bit too successfully!
Would it be possible to make a new personal message header button appear only selectively for those who have access to personal messaging? New users by default don’t have access to PM so should not see this button…
We have set the site setting (for our TL0, TL1 and TL2 users (who therefor are restricted from personal messaging))
One way to do this is to look up the current user’s trust level and use that as a base to determine whether or not to create the PM button.
You would use something like this:
<script type="text/discourse-plugin" version="0.8.18">
// no need to edit these
const container = Discourse.__container__,
controller = container.lookup("controller:application"),
trust = controller.get("currentUser.trust_level");
api.decorateWidget("header-buttons:after", helper => {
// change desired trust level here
if (Discourse.User.current() && trust >= "2") {
// edit these two to your liking
var $ntb_text = "New Personal Message",
$ntb_icon = "heart",
// no need to edit these
$ntb_icon_class = ".fa-" + $ntb_icon,
$ntb_button_class = "btn btn-default btn btn-icon-text",
$ntb_button_helper = "button#new-create-topic-custom",
$ntb_icon_helper =
"i.fa" + $ntb_icon_class + ".d-icon .d-icon-" + $ntb_icon,
$ntb_label_helper = "span.d-button-label";
const createPM = function() {
const Composer = require("discourse/models/composer").default,
composerController = container.lookup("controller:composer");
composerController.open({
action: Composer.PRIVATE_MESSAGE,
// 1- set recipients (Case Sensitive) or comment the line below
usernames: "discobot,john,mike",
// 2- set title or comment the line below
topicTitle: "Title",
// 3- uncomment the line below to set a "template" for PM.
// Not really recommended will
// override any drafts everytime the
// button is pressed
// topicBody: "placeholder content",
// no need to change these
archetypeId: "private_message",
draftKey: Composer.NEW_PRIVATE_MESSAGE_KEY
});
};
return helper.h(
$ntb_button_helper,
{
className: $ntb_button_class,
title: $ntb_text,
onclick: createPM
},
[helper.h($ntb_icon_helper), helper.h($ntb_label_helper, $ntb_text)]
);
}
});
</script>
Yes, that would also be possible as well. You can use something like this in the header section of a theme component
<script type="text/discourse-plugin" version="0.8">
const i18nTopicLable = I18n.lookup("topic.create");
api.modifyClass("component:create-topic-button", {
didInsertElement: function() {
var button = $(this),
category = button[0].parentView.get("category"),
label = button[0].label,
newTopicLabel = "topic.create",
buttonText = "";
if (category) {
categoryName = category.name;
if (label != newTopicLabel) {
return;
} else {
switch (categoryName) {
case "category1": // category name
buttonText = "category1 text"; // button text
break;
// repeat
case "category2":
buttonText = "category2 text";
break;
// add more above this line
default:
buttonText = i18nTopicLable;
}
$("#create-topic .d-button-label").text(buttonText);
}
}
}
});
</script>
To add new categories, you only need to add this above where it says default
case "category":
buttonText = "text";
break;
If you have a pending draft and the button says “open draft” the script won’t fire.
I’ve created a small preview on theme creator and changed the text for the movies, tech, school, videos and gaming categories
Edit: @dax informed me that I had missed something in the snippet I posted previously and so I updated it and the preview so be sure to use the latest one @tophee
Modifiez le fichier category-btn-name/locales/en.yml et saisissez les différents textes de bouton que vous souhaitez utiliser, par exemple :
en:
new_event: Nouveau événement
new_deadline: Nouvelle échéance
Modifiez le fichier category-btn-name/javascripts/discourse/api-initializers/initialize-btn-names.js pour définir quel texte de bouton utiliser pour quelles catégories :