Add allowed_values_function to list field UI
This is related to #1005 somewhat. There is a setting on list fields, "allowed_values_function". It's normally blank for standard list types. You can edit the config file and set it, and thereafter the UI preserves that value. At least, it seems to have done so far for me.
I think it would be preferable to expose that as a text field on the field settings page.
Interesting @bradbulger. Can you please elaborate further? An example use case would help.
My current case is a list field where I would prefer to directly reference existing roles in the system as the options, rather than maintain a separate text copy or vocabulary copy. If I edit the config file and set that setting option to my custom module function, it works fine. That seems like an obscure and fragile solution, though.
How best to present this in the UI, I'm not sure. Probably only visible in the "Manual entry" mode.
Hello, I know this is old, but I needed this exact functionality (and I didn't see it anywhere else), so I created a module to accomplish it:
https://github.com/backdrop-contrib/allowed_values_function
I agree it should just be part of Core, but in the meantime, I hope this helps anyone else looking for this functionality.
Please be sure to remove the dpm() call. That will crash any site that doesn't have devel enabled.
I know-- I just caught it myself. Dumb mistake on my part. The latest version (1.0.2) is clean.
What's the point of allowed_values_function_test()?
Also, it's best to use form_set_value() to set a value in $form_state during validation. It's best to avoid setting that directly, as you never know if the array structure may have changed somewhere. See documentation
You're right, but the only way I could get form_set_error to work was like this: form_set_error('field][settings][allowed_values_function'...... Which I thought wasn't any better than just using form_error.
The test() function was literally just for testing. I am taking it out. Latest version (1.0.3) should be up shortly.
I'm not talking about the form error. I'm talking about the way you assign a value to $form_state. You should not do $form_state['values']['field']['settings']['allowed_values_function'] = $fn;. Instead use form_set_value() to assign that.
Oh, sorry, didn't ready carefully enough. Let me see what I can do.
The main reason why this is important is because often (e.g. when using Paragraphs) the array structure for values MAY change, so direct assignment will fail.
EDIT: although thinking about this a bit more, Paragraphs would not affect the form structure for the Field UI, so this may be fine after all.
Well, it's probably a good idea to do it the "backdrop way" anyway.
Okay! I think this first version 1.0.x is cleaned up now. I'm going to re-paste its link here for anyone coming off Google.
https://github.com/backdrop-contrib/allowed_values_function