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

You want something like

$.ajax("/session/current").then(function(result) {
  console.log(result.current_user.custom_fields);
});
「いいね!」 2

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

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

「いいね!」 3

got it thanks! :smile:

「いいね!」 3

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

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

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

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

管理画面の「カスタマイズ」>「ユーザーフィールド」セクションで追加したカスタムユーザーフィールドが、コンソールに表示されません。

プラグインに以下を追加しました。

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

ユーザーオブジェクトを見ると、custom_fields の下に空のオブジェクトが表示されます。console.log(user.custom_fields); に変更しても、空のオブジェクトが表示されます。

現在のユーザーには 3 つのカスタムフィールドが入力されています。それらのフィールドキーをすべて test_topic の形式で「公開ユーザーカスタムフィールド」と「スタッフユーザーカスタムフィールド」に追加しましたが、コンソールではオブジェクトは空のままです。

ChatGPT にも聞いてみましたが(笑)、あまり助けになっていません。

何が原因か、何か提案はありますか?

UPDATE:問題の一つは custom_fields を使用していたことだと思います。現在は、こちらによると user_fields というラベルの下にあるようです。

しかし、管理画面の「カスタマイズ」>「ユーザーフィールド」で入力したカスタムキーではなく、カスタムユーザーフィールドの値とキーの数値のみが表示されるようです。

「いいね!」 1

このテーマコンポーネントは、ユーザーフィールドで何をしたいかによっては興味深いかもしれません。

これは、ユーザーが監視する単語の種類で投稿を非表示にするものです。

ソースを表示すると、アイデアが得られるかもしれません。

「いいね!」 1

管理画面の「カスタマイズ」>「ユーザーフィールド」で追加したカスタムユーザーフィールドが、以下のようになっていることを確認したい小さなテーマコンポーネントを作成しようとしています。

  1. 管理画面の「カスタマイズ」>「テキスト」セクションで、それらのテキストをカスタマイズできるように、翻訳可能なi18nに追加されること。
  2. 管理画面の「カスタマイズ」>「テキスト」セクションで %{key} として使用できるように、補間キーとして追加されること。

基本的に、ここで投稿した自分の問題を解決しようとしています。
https://meta.discourse.org/t/create-and-configure-custom-user-fields/113192/55

しかし、ユーザーオブジェクトからキー/値の組み合わせを取得するという最初の部分を乗り越えることができませんでした。

追加された内容を確認します。ありがとうございます!

「いいね!」 1

私はまだ非常に新人で、物事を学びながら進めています。確かなことは言えませんが、言語翻訳に関しては、翻訳を行うプラグインが2つあります。

そして

もし私の理解が間違っていたら、あなたが達成しようとしていることと異なっていたら申し訳ありません。

「いいね!」 1