Ability to place custom fields in post header?

I have a use case where I would like to add the content from a custom user field next to my username in the post header:

not sure the best way to go about this?

very new to discourse and all its wonders

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>

9 « J'aime »

Thank you for this explanation, it’s something that I was also looking for. I’d say what’s missing in this explanation is how you insert the javascript snippet and where you do it. Do you need to create a separate plugin for it?

1 « J'aime »

The code I posted above with the console.log statement that returns the post attributes, is meant to go in a theme or a theme component. For a general overview of developing Discourse themes, see Developer’s guide to Discourse Themes. For details about using the Plugin API methods in a theme, instead of a plugin, see https://meta.discourse.org/t/using-the-pluginapi-in-site-customizations/41281.

3 « J'aime »

Does anybody knows how to do it via themes?

This is exaclty what i look for.

I want to be able to see it just like LinkedIn does (shows Job title):

image

Just like in this conversation here:

Bonjour à tous ! Je suis nouveau sur Discourse et j’ai trouvé ce fil qui ressemble EXACTEMENT à ce que j’essaie de faire. J’ai un champ personnalisé que j’aimerais afficher juste après le nom complet d’un utilisateur sur un message.

Je ne suis pas complètement inexpérimenté dans la création de plugins pour d’autres applications web que j’ai utilisées par le passé, donc je pense qu’avec suffisamment de patience et d’essais et d’erreurs, je pourrais trouver comment faire fonctionner un composant de thème ou quelque chose de similaire. Je suis en train de suivre le tutoriel de développement de thèmes.

Je PENSE avoir trouvé le nom du champ à ajouter au paramètre « champs personnalisés publics de l’utilisateur » (je pense que l’identifiant est 2, donc je l’ai défini sur user_field_2).

Je me suis connecté à mon site Discourse et j’ai créé un nouveau composant de thème. Je l’ai activé pour tous les thèmes que j’ai installés. Je suis allé dans JS et j’ai copié le code ci-dessus (en remplaçant user_field_4 par user_field_2). Cela n’a rien semblé faire, donc je suis sûr que j’ai fait quelque chose de mal. Y a-t-il un autre endroit où j’aurais dû coller cela ? Ou suis-je complètement à côté de la plaque ?

Je sais que ce message est un peu ancien, donc le code peut être obsolète. Y a-t-il une possibilité que quelqu’un puisse m’aider ? Je ferai de mon mieux pour répondre à toutes les questions ou fournir les détails nécessaires. MERCI !