Dynamically update available user field values during a profile fill in

Thank you for the prompt responses @JammyDodger and @Canapin , I have a specific question:

I would like to update values in a custom field through a text field, which, for example, indicates the place of residence and is accompanied by a JSON.

How to change user fields with API, there is only a example to creat new users or change value instead of the profil of a user trought custom field.

As an example:

  1. There is a dropdown custom field with values (e.g. Berlin, Hamburg).

  2. There is a custom field with text, which is used when the user is not from Berlin or Hamburg; they enter “Hannover.”

  3. I want to then add the dropdown custom field with values (e.g. Berlin, Hamburg, Hannover), but I do not know how to do this. It is a kind of automation to add cities that are not listed.

  4. The dropdown custom field should depend on a JSON list from a custom field text.

I’m not sure I understand it all…

You want to have dynamic dropdown values - loaded from a json file - depending on the content of another user field?

That would probably need a theme component, but I’m not sure it’s super easy.

1 Like

I know i know, actually im struggling on the update issue for custom field values, i don‘t want to update the user profil. I want update the options in a Dropdown Custom Field :sweat_smile:

Ok so that’s a very specific question, and I’m not sure it’s necessarily related to the API. More of DOM manipulation. But I’ll let the experts share their knowledge :slight_smile:

1 Like

I have found a solution to automatically update this specific case. No API update possibility for customfield options , I have only found a server-side solution here through a cron job. I didn’t use the json file, i used the database entries for automation.

The use case

The use case for this automation is to allow new users to select existing options when registering, which can help the system to evolve as more users make their preferences known. If a new user cannot find an appropriate option, they can enter their own text in the custom text field, which can then be added to the dropdown list for the next user.

This should be used with caution, as changes to the databases for Discourse can crash, recommended only for experienced developers!


Steps 1:

Create two custom fields in /admin/customize/user_fields, one with a dropdown and the other with just text (in my example: dropdown with user_field_5 and text with user_field_8).

Note: The ID can be retrieved from /admin/customize/user_fields.json.

Step 2:

It is best to use Discourse Data Explorer to check your inputs: Discourse Data Explorer

Create a new query under /admin/plugins/explorer:

E.g.: User Field Options

SELECT * FROM user_field_options WHERE user_field_id='5'

Change user_field_id=‘5’ to your dropdown user_field_id=‘x’ Then the values that are displayed for your dropdown options will be output.

Create a second query:

E.g.: User Custom Fields

SELECT * FROM user_custom_fields WHERE name = 'user_field_8'

Change ‘user_field_8’ to your text custom field ‘user_field_x’ Then the values that your users have entered for text will be output.

Step 3: This is the basic foundation for your cron job to update the user_field_options database, where all options for custom fields are listed, which have the associated value 5 for me because of user_field_5. I want to add options there.

First, we create a bash script in the directory for Discourse

cd /var/discourse
nano update_dropdown.sh

Add the following content if Discourse is run as Docker:

cd /var/discourse
docker exec -i app su postgres -c "psql discourse -c \"UPDATE user_custom_fields SET value = (SELECT value FROM user_field_options WHERE user_field_id = 5 AND user_field_options.value = user_custom_fields.value) WHERE name = 'user_field_8' AND value IS NOT NULL AND value NOT IN (SELECT value FROM user_field_options);\""

Make sure you save the script and make it executable

chmod +x update_dropdown.sh

Then you can start and test the script

cd /var/discourse

Step 4: To automate this, we need to add the bash script as a cron job:

crontab -e

This opens the cron job editor, where you add the code in the next line:

* * * * * /var/discourse/update_dropdown.sh

Then the command will be executed every minute. This can of course be adjusted, and the path must be adjusted depending on where you have stored update_dropdown.sh on the server.


This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.