Fix #3756 hook_civicrm_geocoderFormat does not alter address components
authorAllen Shaw <allen@JoineryHQ.com>
Fri, 29 Jul 2022 20:44:19 +0000 (15:44 -0500)
committerAllen Shaw <allen@JoineryHQ.com>
Fri, 29 Jul 2022 20:44:38 +0000 (15:44 -0500)
CRM/Utils/Geocode/Google.php

index 0885a3d3ae6a59daa4d31dc7e8a707c33b05af3e..247a54109600316dc24258fb259eb6d925e4c779 100644 (file)
@@ -106,6 +106,8 @@ class CRM_Utils_Geocode_Google {
       $values['geo_code_error'] = $coord['geo_code_error'];
     }
 
+    CRM_Utils_Hook::geocoderFormat('Google', $values, $coord['request_xml']);
+
     return isset($coord['geo_code_1'], $coord['geo_code_2']);
   }
 
@@ -122,11 +124,18 @@ class CRM_Utils_Geocode_Google {
   /**
    * @param string $add
    *   Url-encoded address
+   *
    * @return array
+   *   An array of values with the following possible keys:
+   *     geo_code_error: String error message
+   *     geo_code_1: Float latitude
+   *     geo_code_2: Float longitude
+   *     request_xml: SimpleXMLElement parsed xml from geocoding API
+   *
    * @throws \GuzzleHttp\Exception\GuzzleException
    */
   private static function makeRequest($add) {
-
+    $coords = [];
     $config = CRM_Core_Config::singleton();
     if (!empty($config->geoAPIKey)) {
       $add .= '&key=' . urlencode($config->geoAPIKey);
@@ -140,11 +149,11 @@ class CRM_Utils_Geocode_Google {
 
     libxml_use_internal_errors(TRUE);
     $xml = @simplexml_load_string($string);
-    CRM_Utils_Hook::geocoderFormat('Google', $values, $xml);
+    $coords['request_xml'] = $xml;
     if ($xml === FALSE) {
       // account blocked maybe?
       CRM_Core_Error::debug_var('Geocoding failed.  Message from Google:', $string);
-      return ['geo_code_error' => $string];
+      $coords['geo_code_error'] = $string;
     }
 
     if (isset($xml->status)) {
@@ -155,22 +164,18 @@ class CRM_Utils_Geocode_Google {
       ) {
         $ret = $xml->result->geometry->location->children();
         if ($ret->lat && $ret->lng) {
-          return [
-            'geo_code_1' => (float) $ret->lat,
-            'geo_code_2' => (float) $ret->lng,
-          ];
+          $coords['geo_code_1'] = (float) $ret->lat;
+          $coords['geo_code_2'] = (float) $ret->lng;
         }
       }
-      elseif ($xml->status == 'ZERO_RESULTS') {
-        // reset the geo code values if we did not get any good values
-        return [];
-      }
-      else {
+      elseif ($xml->status != 'ZERO_RESULTS') {
+        // 'ZERO_RESULTS' is a valid status, in which case we'll change nothing in $ret;
+        // but if the status is anything else, we need to note the error.
         CRM_Core_Error::debug_var("Geocoding failed. Message from Google: ({$xml->status})", (string ) $xml->error_message);
-        return ['geo_code_error' => $xml->status];
+        $coords['geo_code_error'] = $xml->status;
       }
     }
-    return [];
+    return $coords;
   }
 
 }