You are here

public function AddressFieldProvider::getPositionsFromItem in Geolocation Field 8.3

Same name and namespace in other branches
  1. 8.2 modules/geolocation_address/src/Plugin/geolocation/DataProvider/AddressFieldProvider.php \Drupal\geolocation_address\Plugin\geolocation\DataProvider\AddressFieldProvider::getPositionsFromItem()

Get positions from field item list.

Parameters

\Drupal\Core\Field\FieldItemInterface $fieldItem: Views field definition.

Return value

array Retrieved coordinates.

Overrides DataProviderBase::getPositionsFromItem

File

modules/geolocation_address/src/Plugin/geolocation/DataProvider/AddressFieldProvider.php, line 143

Class

AddressFieldProvider
Provides default address field.

Namespace

Drupal\geolocation_address\Plugin\geolocation\DataProvider

Code

public function getPositionsFromItem(FieldItemInterface $item) {
  if (!$item instanceof AddressItem) {
    return [];
  }
  if (empty($this->geocoder)) {
    return [];
  }
  $address_format = str_replace([
    "\r",
    "\n",
  ], ' ', $this->addressFormatRepository
    ->get($item
    ->getCountryCode())
    ->getFormat());
  $formatted_address = new FormattableMarkup(str_replace('%', ':', $address_format), [
    ':givenName' => $item
      ->getGivenName(),
    ':familyName' => $item
      ->getFamilyName(),
    ':organization' => $item
      ->getOrganization(),
    ':addressLine1' => $item
      ->getAddressLine1(),
    ':addressLine2' => $item
      ->getAddressLine2(),
    ':dependentLocality' => $item
      ->getDependentLocality(),
    ':locality' => $item
      ->getLocality(),
    ':administrativeArea' => $item
      ->getAdministrativeArea(),
    ':postalCode' => $item
      ->getPostalCode(),
    ':sortingCode' => $item
      ->getSortingCode(),
  ]);
  $address = (string) $formatted_address;
  $address = trim($address);
  $address = $address . ' ' . $this->countryRepository
    ->get($item
    ->getCountryCode())
    ->getName();
  $coordinates = $this->geocoder
    ->geocode($address);
  return [
    $coordinates['location'],
  ];
}