userCustomFields attr is undefined when extending the poster-name widget

סיכום מבוסס בינה מלאכותית

In a development discussion about extending the poster-name widget to display user custom fields, stevenpslade reports that attrs.userCustomFields is undefined despite whitelisting the field via whitelist_staff_user_custom_field in plugin.rb. Falco suggests that user custom fields must also be added to the post serializer to be accessible on the post object, referencing an example plugin. stevenpslade counters by pointing to the Discourse Staff Notes plugin, which appears to achieve similar functionality without modifying the post serializer. daniyal adds that poster custom fields may be empty during new post creation and recommends using allow_public_user_custom_field to expose user fields. The core issue revolves around correctly exposing user custom fields to the widget scope, with conflicting evidence on whether post serializer modification is required. The discussion remains unresolved, highlighting potential gaps in documentation or inconsistent behavior between plugins.

Continuing the discussion from Getting joined data for widget scope:

I am trying to extend the poster-name widget and add some user custom fields next to a user’s name on a post.

As per @eviltrout:

and…

whitelist_staff_user_custom_field("myCustomField") in plugin.rb :white_check_mark:

I have whitelisted the custom field I am trying to access but when I view attrs.userCustomFields of the poster-name widget it is undefined. Am I missing a step to make the user custom fields available to the widget/plugin?

If things haven’t changed, you need to add user custom fields to the post serializer when you want a user custom field on the post object.

Like this:

I was going off the Discourse Staff Notes plugin which uses the poster-name widget in a similar way I want to, and it doesn’t look like they do that.

Poster custom fields are empty when new post is created. Using allow_public_user_custom_field method to allow user fields.