GeolocationAddress class does not accept NULL

Created on 14 May 2025, 1 day ago

Problem/Motivation

Currently the GeocodeAddress class does not accept NULL values on PHP8.3:

class GeolocationAddress {

public function __construct(
public string $organization = '',
public string $addressLine1 = '',
public string $addressLine2 = '',
public string $addressLine3 = '',
public string $dependentLocality = '',
public string $locality = '',
public string $administrativeArea = '',
public string $postalCode = '',
public string $sortingCode = '',
public string $countryCode = '',
) {}
}

?>

This can lead to PHP errors if GeocoderController::geocode() does not have ALL the fields present.

Steps to reproduce

1. Set up a Geocode field with Nominatim provider linked to an address field
2. Disable some optional address fields (e.g. Organization)
3. Try to Geocode an address

The following error is generated:

TypeError: Drupal\geolocation\GeolocationAddress::__construct(): Argument #1 ($organization) must be of type string, null given, called in /Users/matt/Sites/mysite/web/modules/contrib/geolocation/modules/geolocation_address/src/Controller/GeocoderController.php on line 71 in Drupal\geolocation\GeolocationAddress->__construct() (line 10 of modules/contrib/geolocation/src/GeolocationAddress.php).

Proposed resolution

Make the defaults NULL:

class GeolocationAddress {

  public function __construct(
    public ?string $organization = NULL,
    public ?string $addressLine1 = NULL,
    public ?string $addressLine2 = NULL,
    public ?string $addressLine3 = NULL,
    public ?string $dependentLocality = NULL,
    public ?string $locality = NULL,
    public ?string $administrativeArea = NULL,
    public ?string $postalCode = NULL,
    public ?string $sortingCode = NULL,
    public ?string $countryCode = NULL,
  ) {}
}

Remaining tasks

I will send a merge request for this change.

🐛 Bug report
Status

Active

Version

4.0

Component

Geolocation Field / Backend

Created by

🇬🇧United Kingdom mattjones86 🇬🇧 GMT+0

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024