backdrop-issues icon indicating copy to clipboard operation
backdrop-issues copied to clipboard

Add allowed_values_function to list field UI

Open bradbulger opened this issue 7 years ago • 13 comments

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.

bradbulger avatar Mar 06 '19 23:03 bradbulger

Interesting @bradbulger. Can you please elaborate further? An example use case would help.

klonos avatar Mar 07 '19 06:03 klonos

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.

bradbulger avatar Mar 07 '19 16:03 bradbulger

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.

swampopus avatar Nov 26 '25 17:11 swampopus

Please be sure to remove the dpm() call. That will crash any site that doesn't have devel enabled.

argiepiano avatar Nov 26 '25 17:11 argiepiano

I know-- I just caught it myself. Dumb mistake on my part. The latest version (1.0.2) is clean.

swampopus avatar Nov 26 '25 18:11 swampopus

What's the point of allowed_values_function_test()?

argiepiano avatar Nov 26 '25 18:11 argiepiano

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

argiepiano avatar Nov 26 '25 18:11 argiepiano

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.

swampopus avatar Nov 26 '25 18:11 swampopus

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.

argiepiano avatar Nov 26 '25 18:11 argiepiano

Oh, sorry, didn't ready carefully enough. Let me see what I can do.

swampopus avatar Nov 26 '25 18:11 swampopus

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.

argiepiano avatar Nov 26 '25 18:11 argiepiano

Well, it's probably a good idea to do it the "backdrop way" anyway.

swampopus avatar Nov 26 '25 18:11 swampopus

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

swampopus avatar Nov 26 '25 18:11 swampopus