Problem/Motivation

When use link field widget in custom form, start seeing following notice on PHP 7.4

Notice: Trying to access array offset on value of type null in field_widget_instance() (line 625 of /app/web/modules/field/field.form.inc)

Steps to reproduce

With PHP 7.4, use link widget on a custom form, see https://www.drupal.org/project/link/issues/2718563.

Proposed resolution

Instead of calling field_widget_instance() directly in link_field_process(), called field_form_get_state()and check for instance.

 function link_field_process($element, $form_state, $complete_form) {
-  $instance = field_widget_instance($element, $form_state);
+  $field_state = field_form_get_state($element['#field_parents'], $element['#field_name'], $element['#language'], $form_state);
+  $instance = !empty($field_state['instance']) ? $field_state['instance'] : array();
function field_widget_instance($element, $form_state) {
  $field_state = field_form_get_state($element['#field_parents'], $element['#field_name'], $element['#language'], $form_state);
  return $field_state['instance'];
}
CommentFileSizeAuthor
#3 link-php74-3276037-3.patch702 bytesxlin1003

Comments

xlin1003 created an issue. See original summary.

xlin1003’s picture

Title: PHP 7.4 Notice: Trying to access array offset on value of type null in field_widget_instance() when use in custoom form » PHP 7.4 Notice: Trying to access array offset on value of type null in field_widget_instance() when use in custom form
xlin1003’s picture

Status: Active » Needs review
StatusFileSize
new702 bytes

Patched attached to not use the field_widget_instance() directly. Not sure if it make sense to patch/fix the core as I only see this notice when using link widget on custom form.

 function link_field_process($element, $form_state, $complete_form) {
-  $instance = field_widget_instance($element, $form_state);
+  $field_state = field_form_get_state($element['#field_parents'], $element['#field_name'], $element['#language'], $form_state);
+  $instance = !empty($field_state['instance']) ? $field_state['instance'] : array();
damienmckenna’s picture

This seems reasonable, and all of the tests pass.

damienmckenna’s picture

damienmckenna’s picture

Status: Needs review » Fixed

Committed. Thank you.

Status: Fixed » Closed (fixed)

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

damienmckenna’s picture

Category: Feature request » Bug report