You are here

public function AddressToGeo::addressArrayToString in Geolocation Address Link 8

Convert an address array into a string address suitable for geocoding.

Expects array structured like the Address module as the input values.

See also

\Drupal\address\Element\Address::applyDefaults().

1 call to AddressToGeo::addressArrayToString()
AddressToGeo::geocode in src/AddressToGeo.php
Geocode an address.

File

src/AddressToGeo.php, line 157

Class

AddressToGeo
Class AddressToGeo.

Namespace

Drupal\geolocation_address_link

Code

public function addressArrayToString(array $address_array) {

  // Make sure the address_array has all values populated.
  $address_array = \Drupal\address\Element\Address::applyDefaults($address_array);

  // Use the Address formatter to create a string ordered appropriately
  // for the country in the address.
  $address = new \CommerceGuys\Addressing\Address();
  $address = $address
    ->withCountryCode($address_array['country_code'])
    ->withPostalCode($address_array['postal_code'])
    ->withAdministrativeArea($address_array['administrative_area'])
    ->withDependentLocality($address_array['dependent_locality'])
    ->withLocality($address_array['locality'])
    ->withAddressLine1($address_array['address_line1'])
    ->withAddressLine2($address_array['address_line2'])
    ->withOrganization($address_array['organization']);
  $address_string = $this->formatter
    ->format($address);

  // Clean up the returned address to turn it into a single line of text.
  $address_string = str_replace("\n", ' ', $address_string);
  $address_string = str_replace("<br>", ' ', $address_string);
  $address_string = strip_tags($address_string);
  return $address_string;
}