Notice: Trying to access array offset on value of type null in FieldInfo->prepareInstanceWidget()

Created on 5 February 2021, almost 4 years ago
Updated 15 April 2024, 8 months ago

Problem/Motivation

Notice: Trying to access array offset on value of type null in FieldInfo->prepareInstanceWidget() when caches are cleared.

Steps to reproduce

Install Drupal under PHP7.4. Clear caches.

Proposed resolution

Check field properties exist before attempting to access them.

🐛 Bug report
Status

Needs work

Version

7.0 ⚰️

Component
Field 

Last updated 1 day ago

Created by

🇳🇿New Zealand scott.whittaker

Live updates comments and jobs are added and updated live.
  • PHP 7.4

    The issue particularly affects sites running on PHP version 7.4.0 or later.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇮🇹Italy apaderno Brescia, 🇮🇹
  • Status changed to Needs work 8 months ago
  • 🇮🇹Italy apaderno Brescia, 🇮🇹
    +    if (isset($field_type_info['default_widget'])) {
    +      $default_widget = $field_type_info['default_widget'];
    +    } else {
    +      $default_widget = NULL;
    +    }
     
         // Fill in default values.
         $widget += array(
    -      'type' => $field_type_info['default_widget'],
    +      'type' => $default_widget,
           'settings' => array(),
           'weight' => 0,
         );
    @@ -585,10 +590,14 @@ class FieldInfo {
         $widget_type_info = field_info_widget_types($widget['type']);
    

    When NULL is passed to field_info_widget_types(), it returns an array containing information for all the widget types. This would cause errors in the rest of the code, which expects an array with information about a single widget type.

    function field_info_widget_types($widget_type = NULL) {
      $info = _field_info_collate_types();
      $widget_types = $info['widget types'];
      if ($widget_type) {
        if (isset($widget_types[$widget_type])) {
          return $widget_types[$widget_type];
        }
      }
      else {
        return $widget_types;
      }
    }

    Notice that Trying to access array offset on value of type null means trying to access $field_type_info['default_widget'] when $field_type_info is NULL, which means that field_info_field_types($field_type) in $field_type_info = field_info_field_types($field_type); returned NULL.
    That happens when the field type passed as argument is not known to Drupal. At this point, it is not possible to know which is the default widget type for the field, since not even the field type is known. NULL is not a valid widget type, and it should not be used.

Production build 0.71.5 2024