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

Options Element settings override should check if allowed_values_function and default_value_function are set

Open herbdool opened this issue 1 year ago • 8 comments

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.

herbdool avatar Jul 12 '24 18:07 herbdool

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_function to a function that returns an array
Screen Shot 2024-07-12 at 5 21 44 PM

I visited the field UI and I'm not seeing a warning. I'm seeing this instead, which is the normal behavior.

Screen Shot 2024-07-12 at 5 23 19 PM

Will you be able to post more detailed instructions?

argiepiano avatar Jul 12 '24 23:07 argiepiano

@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 avatar Jul 13 '24 00:07 herbdool

@herbdool I've been able to reproduce, but there are some caveats.

IF I add BOTH:

  • default_value_function to the field instance info, AND
  • allowed_values_function to the field info

then your PR solves the issue - no more warning. 👍🏽

IF I add ONLY:

  • allowed_values_function to the field info

There is no warning (before or after the PR).

BUT if I add ONLY:

  • default_value_function to 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 avatar Jul 13 '24 01:07 argiepiano

@argiepiano thanks for testing. I was wondering which was better to check.

herbdool avatar Jul 13 '24 02:07 herbdool

@herbdool there are widespread test failures. Your PR does not define $instance.

In my sample code above I had:

$instance = $form['#instance'];

argiepiano avatar Jul 13 '24 12:07 argiepiano

This is what happens when I do it on a phone.

herbdool avatar Jul 13 '24 13:07 herbdool

@herbdool sorry, but there are still a lot of failing tests. Please notice my code - you are using $form['instance'], instead of $form['#instance'].

argiepiano avatar Jul 13 '24 13:07 argiepiano

OK, reviewed and tested, LGTM.

argiepiano avatar Jul 13 '24 21:07 argiepiano

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!

quicksketch avatar Aug 30 '24 20:08 quicksketch