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); لا يزال يظهر كائنًا فارغًا.

المستخدم الحالي لديه ثلاثة حقول مخصصة مملوءة، لقد أضفت مفاتيح هذه الحقول الثلاثة، بتنسيق test_topic إلى public user custom fields و staff user custom fields ومع ذلك لا يزال الكائن يبدو فارغًا في وحدة التحكم.

لقد سألت ChatGPT (lol) ويبدو أنها لا تساعدني.

أي اقتراحات حول ما يمكن أن يحدث؟

تحديث: أعتقد أن إحدى المشاكل كانت أنني كنت أستخدم custom_fields والآن يبدو أنها تحت تسمية user_fields، كما هو موضح هنا:

ومع ذلك، يبدو أنه يعرض لي فقط قيمة حقل المستخدم المخصص ورقمًا للمفتاح، بدلاً من المفتاح المخصص الذي أدخلته في المسؤول > تخصيص > حقول المستخدم.

إعجاب واحد (1)

قد يكون مكون السمة هذا محل اهتمام اعتمادًا على ما تتطلع إلى القيام به بحقل المستخدم.

يضيف هذا إخفاء المنشورات بنوع من الكلمات التي يراقبها المستخدم.

قد يوفر عرض المصدر أفكارًا.

إعجاب واحد (1)

أحاول بشكل أساسي إنشاء مكون سمة صغير يضمن أن الحقول المخصصة للمستخدم التي أضيفها في المسؤول > تخصيص > حقول المستخدم هي:

  1. تمت إضافتها إلى i18n قابلة للترجمة حتى أتمكن من تخصيص نصها في المسؤول > تخصيص > قسم النص
  2. تمت إضافتها كمفاتيح استيفاء حتى أتمكن من استخدامها كـ %{key} في المسؤول > تخصيص > قسم النص

بشكل أساسي أحاول حل مشكلتي التي نشرتها هنا:
https://meta.discourse.org/t/create-and-configure-custom-user-fields/113192/55

لكنني لم أتمكن من تجاوز الجزء الأول المتمثل في الحصول على تركيبة المفتاح/القيمة من حقول المستخدم في كائن المستخدم.

سألقي نظرة على ما أضفته، شكرًا لك!

إعجاب واحد (1)

ما زلت جديدًا جدًا وأتعلم الأشياء أثناء تقدمي. لا يمكنني التأكيد، ولكن إذا كانت هناك ترجمات لغوية، فهناك مكونان إضافيان يقومان بالترجمة.

و

أعتذر إذا أسأت فهم ما تعمل على تحقيقه إذا كان الأمر غير صحيح.

إعجاب واحد (1)