Problem/Motivation

When a field doesn't have a widget and you save the config it doesn't return an array as defaultValuesFormSubmit is expected to return and then this error will show:

Warning: Invalid argument supplied for foreach() in Drupal\Core\Field\EntityReferenceFieldItemList->defaultValuesFormSubmit() (line 111 of core/lib/Drupal/Core/Field/EntityReferenceFieldItemList.php).

This shows up for me when I use feeds and the feed_item field is saved but it could be any field without a widget.

Proposed resolution

Return an array from defaultValuesFormSubmit, always.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

joelpittet created an issue. See original summary.

joelpittet’s picture

Status: Active » Needs review
FileSize
479 bytes

Here's the one line patch that satisfies the return value on the interface. I expect this doesn't need tests to verify because PHPstorm picks it up as a bug already and all other cases of this return an empty array that extend this method.

andypost’s picture

Status: Needs review » Needs work

The last submitted patch, 2: 3071765-2-return-array.patch, failed testing. View results

joelpittet’s picture

@andypost I don't think this is related. This one doesn't have a widget and it throws warnings because the expected array is NULL which it should be.

joelpittet’s picture

Status: Needs work » Needs review
FileSize
1.08 KB

Fixed the broken test. defaultValuesFormSubmit should always return an array like it's interface says.

   *
   * @return array
   *   The field default value.
   */
  public function defaultValuesFormSubmit(array $element, array &$form, FormStateInterface $form_state);
joelpittet’s picture

Title: Field without widget can't get default value during submit » Field without widget returning NULL instead of array in defaultValuesFormSubmit()

Clarify the title which is where it sounded like the other issue I think

andypost’s picture

Status: Needs review » Reviewed & tested by the community

Fixed test illustrative

  • larowlan committed dbb87e1 on 8.8.x
    Issue #3071765 by joelpittet: Field without widget returning NULL...
larowlan’s picture

Status: Reviewed & tested by the community » Fixed

Committed dbb87e1 and pushed to 8.8.x. Thanks!

Decided not to backport in case this is disruptive - as although the interface specifies the return should be an array, this is a behaviour change.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.