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”
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?
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?
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
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.
Pour une raison quelconque, je ne vois pas les champs utilisateur personnalisés dans la console que j’ai ajoutés dans la section Admin > Personnaliser > Champs utilisateur.
J’ai mis ceci dans un plugin :
<script type="text/discourse-plugin" version="0.8.42">
const user = api.getCurrentUser();
console.log(user);
</script>
Lorsque je regarde l’objet utilisateur, je vois un objet vide sous l’entrée custom_fields. Lorsque je le change en console.log(user.custom_fields);, il affiche toujours un objet vide.
L’utilisateur actuel a trois champs personnalisés remplis, j’ai ajouté ces trois clés de champ, au format test_topic, aux champs utilisateur publics et aux champs utilisateur du personnel et pourtant l’objet apparaît toujours vide dans la console.
J’ai demandé à ChatGPT (lol) et il ne semble pas m’aider.
Des suggestions sur ce qui pourrait se passer ?
MISE À JOUR : Je pense qu’un problème était que j’utilisais custom_fields et maintenant il semble être sous l’étiquette user_fields, comme indiqué ici :
Cependant, cela semble juste me montrer la valeur du champ utilisateur personnalisé et un nombre pour la clé, au lieu de la clé personnalisée que j’ai entrée dans Admin > Personnaliser > Champs utilisateur.
J’essaie principalement de créer un petit composant de thème qui garantira que les champs utilisateur personnalisés que j’ajoute dans admin > customize > user fields sont :
Ajoutés aux i18n traduisibles afin que je puisse personnaliser le texte dans la section admin > customize > text
Ajoutés en tant que clés d’interpolation afin que je puisse les utiliser comme %{key} dans la section admin > customize > text
Mais je n’ai pas pu dépasser la première partie, qui consistait simplement à obtenir la combinaison clé/valeur des champs utilisateur dans l’objet utilisateur.
Je suis encore très nouveau et j’apprends au fur et à mesure. Je ne peux pas dire avec certitude, mais s’il existe des plugins pour les traductions linguistiques, il en existe deux qui font des traductions.
Et
Je m’excuse si j’ai mal compris ce que vous essayez d’accomplir si c’est incorrect.