Options Element settings override should check if allowed_values_function and default_value_function are set
Description of the bug
When editing a list item field widget, I get:
Warning: Undefined array key "field_list" in list_form_field_ui_field_edit_form_alter() (line 158 of /app/httpdocs/core/modules/field/modules/list/list.module). Warning: Trying to access array offset on value of type null in list_form_field_ui_field_edit_form_alter() (line 158 of /app/httpdocs/core/modules/field/modules/list/list.module).
Steps To Reproduce
This happens if creating a list field that uses allowed_values_function and default_value_function instead of the values set in the UI. This was originally added in https://github.com/backdrop/backdrop-issues/issues/1005
Expected behavior
It should either not bother to alter the form or fetch the default value from default_value_function.
I'm having a hard time reproducing this error.
- I created a text list field attached to an entity.
- I manually set the
allowed_values_functionto a function that returns an array
I visited the field UI and I'm not seeing a warning. I'm seeing this instead, which is the normal behavior.
Will you be able to post more detailed instructions?
@argiepiano I suspect you may have to set default_value_function as well.
I'll try to recreate the error on a fresh site too.
@herbdool I've been able to reproduce, but there are some caveats.
IF I add BOTH:
default_value_functionto the field instance info, ANDallowed_values_functionto the field info
then your PR solves the issue - no more warning. 👍🏽
IF I add ONLY:
allowed_values_functionto the field info
There is no warning (before or after the PR).
BUT if I add ONLY:
default_value_functionto the field instance info
then the warning persists, even with the PR. 👎🏽
I think your check is not quite right. if you do this instead:
$field = $form['#field'];
$instance = $form['#instance'];
if (in_array($field['type'], array('list_integer', 'list_float', 'list_text'))) {
// If the default value is set automatically skip setting the widget.
if ($instance['default_value_function']) {
return;
}
//...
Then I believe the issue is solved.
@argiepiano thanks for testing. I was wondering which was better to check.
@herbdool there are widespread test failures. Your PR does not define $instance.
In my sample code above I had:
$instance = $form['#instance'];
This is what happens when I do it on a phone.
@herbdool sorry, but there are still a lot of failing tests. Please notice my code - you are using $form['instance'], instead of $form['#instance'].
OK, reviewed and tested, LGTM.
Thanks @herbdool, @argiepiano, and @klonos! I made an adjustment to the code formatting to define the $instance variable and then check that, like @argiepiano's code samples above. I merged https://github.com/backdrop/backdrop/pull/4829 into 1.x and 1.28.x. Thanks!