You can do a lot with the methods found in plugin-api.js.es6
. To add content before or after the poster name widget, use the decorateWidget method. Use 'poster-name:after'
as the name/type parameter.
Checking the result of the console.log
statement from the following code will tell you what attributes are available:
<script type="text/discourse-plugin" version="0.8.27">
api.decorateWidget('poster-name:after', helper => {
console.log('attrs', helper.attrs);
});
</script>
What you’ll see is that custom user fields are not available. To get access to them, you need to add the name of the user custom field to the public user custom fields
Site Setting. Finding the name of the field can be tricky. One way to find the name is to go to /admin/customize/user_fields.json
and look at the JSON for the page. You’ll see something like this:
{
"user_fields": [
{
"id": 1,
"name": "Terms of Service",
"description": "<a href=\"http://localhost:3000/tos\">I have read the tos</a>",
"field_type": "confirm",
"editable": true,
"required": false,
"show_on_profile": false,
"show_on_user_card": false,
"position": 1
},
{
"id": 4,
"name": "city",
"description": "your city",
"field_type": "text",
"editable": true,
"required": true,
"show_on_profile": false,
"show_on_user_card": false,
"position": 3
}
]
}
You can get the name of the field by appending “user_field_” to the field’s ID. So the “city” field on my site has the name “user_field_4”. Add the name to the public user custom fields
Site Setting so that it is available in helper.attrs
.
The following code will add the city field after the poster-name
widget for users who have set that custom field. The field is wrapped in <span class="poster-user-field"></span>
tags, so it can be styled with CSS.
<script type="text/discourse-plugin" version="0.8.27">
api.decorateWidget('poster-name:after', helper => {
const attrs = helper.attrs;
if (attrs.userCustomFields && attrs.userCustomFields.user_field_4) {
return helper.h('span.poster-user-field', helper.attrs.userCustomFields.user_field_4);
}
});
</script>