PHP 8.0 incompatibility: Trying to access array offset on value of type null in geocoder_widget_parse_addressfield()

Created on 18 June 2023, about 1 year ago
Updated 30 October 2023, 8 months ago

Problem/Motivation

When visiting the "Content" page, I get the following error:

Warning: Trying to access array offset on value of type null in geocoder_widget_parse_addressfield() (line 642 of /var/www/drupal/sites/all/modules/contrib/geocoder/geocoder.widget.inc).

Steps to reproduce

  • Install the latest 1.x version
  • Visit the "/admin/content" page (ensure caches are cleared)

Proposed resolution

Check for an empty value prior to trying to access the value.

Remaining tasks

  1. Review the patch in attached to a comment below.
  2. Commit the patch.
πŸ› Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States jantoine

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @jantoine
  • @jantoine opened merge request.
  • πŸ‡ΊπŸ‡ΈUnited States jantoine

    Patch of the merge request in #2.

  • Status changed to Needs work 8 months ago
  • πŸ‡ΈπŸ‡°Slovakia poker10

    Thanks for reporting this.

    The patch/MR fix is not working, because the variable $areas being checked in the if condition is defined only later. So this updated condition will always evaluate to FALSE:

    -  if (!empty($field_item['country']) && !empty($field_item['administrative_area'])) {
    +  if (!empty($field_item['country']) && !empty($field_item['administrative_area']) && !empty($areas[$field_item['administrative_area']])) {
         module_load_include('inc', 'addressfield', 'addressfield.administrative_areas');
         $areas = addressfield_get_administrative_areas($field_item['country']);
    
    • poker10 β†’ committed 6c276aec on 7.x-1.x
      Issue #3367463 by jantoine, poker10: PHP 8.0 incompatibility: Trying to...
  • Status changed to Fixed 8 months ago
  • πŸ‡ΈπŸ‡°Slovakia poker10

    As per documentation of addressfield_get_administrative_areas(), it can return a NULL:

    /**
    * Returns the predefined administrative areas.
    *
    * @param $country_code
    * The country code for which administrative areas should be returned.
    *
    * @return
    * An array of predefined administrative areas for the given country code, or
    * NULL if not found.
    */
    function addressfield_get_administrative_areas($country_code) {

    Fixed this by adding a is_null check on $areas, see the commit. Thanks!

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

Production build 0.69.0 2024