PHP-8.1: Automatic conversion of false to array is deprecated in cloud_zoom_field_formatter_info()

Created on 10 October 2023, about 1 year ago
Updated 11 October 2023, about 1 year ago

Problem/Motivation

I do not use Cloud Zoom but it is included in Commerce Kickstart distribution for D7. Upon upgrading PHP to 8.1 and clearing all caches, this warning-level error is output to screen multiple times:

Deprecated function: Automatic conversion of false to array is deprecated in cloud_zoom_field_formatter_info() (line 345 of $DOCROOT/profiles/commerce_kickstart/modules/contrib/cloud_zoom/cloud_zoom.module).

The effect is that most content types will not render (lots of empty pages). It may depend on one's error-handling settings whether the fallout is as severe as it was for me.

Steps to reproduce

1. Install Commerce Kickstart site under older PHP version
2. Upgrade PHP to 8.1.*
3. Hit Configuration -> Performance -> Clear all caches

Proposed resolution

In the function, when $default_settings[$setting['fieldset']] has already been set as a scalar value (FALSE), it is overwritten with an array. To prevent the warning being triggered, that must be done explicitly before adding the key to the array.

/**
 * Implements hook_field_formatter()
 */
function cloud_zoom_field_formatter_info() {
  $formatters = array();

  $cloud_zoom_settings = cloud_zoom_settings_info('default_value');
  $default_settings = array();
  // Return a single depth array with the given key as value.
  foreach ($cloud_zoom_settings as $key => $setting) {
    if (isset($setting['fieldset'])) {
      //Add check for existing scalar value
      if(isset($default_settings[$setting['fieldset']]) && !is_array($default_settings[$setting['fieldset']])) {
        $default_settings[$setting['fieldset']] = array();
      }
      $default_settings[$setting['fieldset']][$key] = $setting['default_value'];
    }
    else {
      $default_settings[$key] = $setting['default_value'];
    }
  }

  $formatters['cloud_zoom'] = array(
    'label' => t('Cloud zoom'),
    'field types' => array('image'),
    'settings' => $default_settings,
  );

  return $formatters;
}
🐛 Bug report
Status

Needs review

Version

1.0

Component

Code

Created by

🇬🇧United Kingdom headbank

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

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

Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024