Notice: Trying to access array offset on value of type null in _field_write_instance()

Created on 11 October 2022, about 2 years ago
Updated 14 June 2024, 5 months ago

Problem/Motivation

Php 7.4 notice from function _field_write_instance at line 633 of field.crud.inc

Steps to reproduce

Under php 7.4 and when saving a content type's Manage Display page, I am getting
"Notice: Trying to access array offset on value of type null in _field_write_instance() (line 633 of .../modules/field/field.crud.inc)."

Proposed resolution

Changing line 633 to the following:
$display['module'] = isset($formatter_type['module']) ? $formatter_type['module'] : '';

The same approach has been implemented with $formatter['module'] at least at two other places:
https://www.drupal.org/project/drupal/issues/3085151
https://www.drupal.org/project/field_formatter_settings/issues/3166628 🐛 php 7.4 errors at content type manage display page RTBC

🐛 Bug report
Status

Needs review

Version

7.0 ⚰️

Component
Field 

Last updated about 15 hours ago

Created by

🇬🇷Greece arx-e

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

Merge Requests

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, 🇮🇹

    Notice that Trying to access array offset on value of type null means, for example, accessing $formatter_type['module'] when $formatter_type is NULL.
    I am not sure that setting $display['module'] to an empty string is much helpful, in that case. It could be helpful if it were be possible to set $display['module'] to a default module name, for example, for which invoking a hook would be later possible.

  • 🇮🇹Italy frazac

    #5 does not work for me.

    Still receiving the error on both remote production and dev server.

    Warning: Trying to access array offset on value of type null in ctools_context_handler_get_render_handler() (line 68 of /home/.../sites/all/modules/ctools/includes/context-task-handler.inc).
    

    Drupal 7.101
    PHP 8.1.28

  • 🇸🇰Slovakia poker10

    @frazac Not sure your warning is caused by the Drupal core, as it is in the ctools module. You can check a similar issue here: #1244434: Notice: Undefined index: handler type in ctools_context_handler_get_render_handler() (line 67 , maybe it could help.

  • 🇮🇹Italy apaderno Brescia, 🇮🇹

    I think the patch is just hiding a bigger issue.
    Why is $formatter_type NULL? If it is NULL, does continuing as nothing happened make sense?

  • 🇮🇹Italy apaderno Brescia, 🇮🇹

    As a side note, the line containing $display['module'] = $formatter_type['module']; is line 648, now.

  • 🇮🇹Italy apaderno Brescia, 🇮🇹

    Let's try to understand what exactly happens that makes $formatter_type set to NULL (or not initialized to a correct value).

    The code containing the line causing that warning is the following one.

    $display += array(
      'label' => 'above',
      'type' => isset($field_type['default_formatter']) ? $field_type['default_formatter'] : 'hidden',
      'settings' => array(),
    );
    if ($display['type'] != 'hidden') {
      $formatter_type = field_info_formatter_types($display['type']);
      $display['module'] = $formatter_type['module'];
      $display['settings'] += field_info_formatter_settings($display['type']);
    }
    

    $formatter_type can be NULL because field_info_formatter_types() did not find information about the $field_type['default_formatter'] formatter or the cache used by _field_info_collate_types() got corrupted. In the case $field_type['default_formatter'] is an empty string, field_info_formatter_types() would return an array with information about all the formatters (and probably cause other issues).

  • Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    last update 5 months ago
    Waiting for branch to pass
  • Pipeline finished with Failed
    5 months ago
    Total: 262s
    #199260
  • Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    last update 5 months ago
    Waiting for branch to pass
  • Pipeline finished with Canceled
    5 months ago
    Total: 193s
    #199264
  • Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    last update 5 months ago
    Waiting for branch to pass
  • Pipeline finished with Success
    5 months ago
    Total: 266s
    #199267
  • 🇮🇹Italy apaderno Brescia, 🇮🇹

    I changed the code to the following one.

        if ($display['type'] != 'hidden') {
          $formatter_type = field_info_formatter_types($display['type']);
          if ($formatter_type) {
            $display['module'] = $formatter_type['module'];
            $display['settings'] += field_info_formatter_settings($display['type']);
          }
        }

    The reason is that:

    • If the formatter has not been found, we do not know which module implements it. Leaving $formatter_type['module'] not set is probably the more correct way to say that.
    • If the formatter has not been found, field_info_formatter_settings() returns an empty array. Executing $display['settings'] += field_info_formatter_settings($display['type']); is perfectly useless, since $display['settings'] has been already initialized to an empty array.
Production build 0.71.5 2024