Getting user profile custom field via JS

Hi,

I’m wondering if it is possible to retrieve user profile custom fields in our customized header using something like this:

$.ajax("/session/current").then(function (json) { 
    console.log(json.custom.user_field_4);
}

What would be the correct syntax?
Thanks!

1 „Gefällt mir“

You want something like

$.ajax("/session/current").then(function(result) {
  console.log(result.current_user.custom_fields);
});
2 „Gefällt mir“

Thanks, how do I get to the attribute?
console.log(result.current_user.custom_fields); yields an object,
i also tried:
result.current_user.custom_fields.my_field_name
and
result.current_user.custom_fields.user_field_4
they all return “undefined”

2 „Gefällt mir“

You will need to whitelist the custom fields you want using the “public_user_custom_fields” site setting.

3 „Gefällt mir“

got it thanks! :smile:

3 „Gefällt mir“

Is this still accurate? I’ve added pro_expires_at as a custom field on my user (User.find('nuck').tap { |u| u.custom_fields['pro_expires_at'] = 5.weeks.from_now.to_i }.save) and stuffed that into public_user_custom_fields in the site settings panel, but I’m just not seeing it in the preload data?

2 „Gefällt mir“

It definitely should still be working. We haven’t removed it and some plugins rely on it to work.

What preload info are you looking at? Viewing a user’s profile?

currentUser and the custom_user_fields attribute that the PostSerializer says should be present on serialized Post objects

It should definitely work if it’s public_user_custom_fields – I know we’re using this feature on some plugins and the data is indeed being displayed properly. Maybe try restarting your dev server? Does it appear in the JSON request?

2 „Gefällt mir“

If you want a custom_user_field on the post object you can do this:

https://github.com/xfalcox/discourse-signatures/blob/1ff4e7e1d57eed825febdbd58980a73bef03cb6b/plugin.rb#L19

1 „Gefällt mir“

public_user_custom_fields in the admin panel, right? Not some setting elsewhere?

I was actually developing against our staging server which rebuilds the docker every time so I’m certain it was restarted (and then some). I’ll look at the JSON request this weekend and see if I can pin this down.

Wouldn’t this cause an N+1 query? Is there any solid documentation on the whole custom field system somewhere? I didn’t realize you could register the custom field type or append to serialized_current_user_fields

I don’t think so, because the serializer already has the user object, is just a matter of including it in the payload. I can be wrong, tough.

Nope, but there are plenty of opensource plugins and the Discourse source code itself.

I found it trough other plugins code too.

Right, public_user_custom_fields in the admin panel. Those fields should end up in the JSON request. I’d double check there. Without a development environment your life is going to be a lot harder, as the next step would be to go into the code and add Rails.logger.info to try and figure out why those fields are not being passed through when they should.

1 „Gefällt mir“

Aus irgendeinem Grund sehe ich die benutzerdefinierten Benutzerfelder in der Konsole nicht, die ich im Abschnitt Admin > Anpassen > Benutzerfelder hinzugefügt habe.

Ich habe Folgendes in ein Plugin eingefügt:

<script type="text/discourse-plugin" version="0.8.42">
   const user = api.getCurrentUser();
   console.log(user);
</script>

Wenn ich das Benutzerobjekt betrachte, sehe ich ein leeres Objekt unter dem Eintrag custom_fields. Wenn ich es in console.log(user.custom_fields); ändere, wird immer noch ein leeres Objekt angezeigt.

Der aktuelle Benutzer hat drei benutzerdefinierte Felder ausgefüllt. Ich habe alle drei dieser Feldschlüssel im Format test_topic zu den public user custom fields und staff user custom fields hinzugefügt, und dennoch erscheint das Objekt in der Konsole immer noch leer.

Ich habe ChatGPT gefragt (lol) und es scheint mir nicht zu helfen.

Irgendwelche Vorschläge, was passieren könnte?

UPDATE: Ich glaube, ein Problem war, dass ich custom_fields verwendet habe und jetzt scheint es unter dem Label user_fields zu sein, wie hier:

Es scheint mir jedoch nur den Wert des benutzerdefinierten Benutzerfeldes und eine Zahl für den Schlüssel anzuzeigen, anstatt des benutzerdefinierten Schlüssels, den ich in Admin > Anpassen > Benutzerfelder eingegeben habe.

1 „Gefällt mir“

Diese Theme-Komponente könnte von Interesse sein, je nachdem, was Sie mit dem Benutzerfeld vorhaben.

Diese fügt das Ausblenden von Beiträgen mit einer Art von Benutzer-beobachteten Wörtern hinzu.

Die Betrachtung des Quellcodes kann Ideen liefern.

1 „Gefällt mir“

Ich versuche hauptsächlich, eine kleine Theme-Komponente zu erstellen, die sicherstellt, dass die benutzerdefinierten Benutzerfelder, die ich unter Admin > Anpassen > Benutzerfelder hinzufüge:

  1. Zu übersetzbarem i18n hinzugefügt werden, damit ich den Text davon unter Admin > Anpassen > Text anpassen kann.
  2. Als Interpolationsschlüssel hinzugefügt werden, damit ich sie als %{key} unter Admin > Anpassen > Text verwenden kann.

Im Grunde versuche ich, mein eigenes Problem zu lösen, das ich hier gepostet habe:
https://meta.discourse.org/t/create-and-configure-custom-user-fields/113192/55

Aber ich konnte den ersten Teil nicht überwinden, nämlich nur die Schlüssel/Wert-Kombination aus den Benutzerfeldern im Benutzerobjekt zu erhalten.

Ich werde mir ansehen, was Sie hinzugefügt haben, danke!

1 „Gefällt mir“

Ich bin noch ganz neu und lerne, während ich gehe. Ich kann es nicht mit Sicherheit sagen, aber wenn es um Sprachübersetzungen geht, gibt es 2 Plugins, die Übersetzungen durchführen.

Und

Ich entschuldige mich, wenn ich missverstanden habe, was Sie erreichen wollen, falls dies falsch ist.

1 „Gefällt mir“