You are here

Geocodio.php in Geolocation Field 8.3

File

modules/geolocation_geocodio/src/Plugin/geolocation/Geocoder/Geocodio.php
View source
<?php

namespace Drupal\geolocation_geocodio\Plugin\geolocation\Geocoder;

use GuzzleHttp\Exception\RequestException;
use Drupal\Core\Url;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Component\Serialization\Json;
use Drupal\geolocation\GeocoderBase;
use Drupal\geolocation\GeocoderInterface;
use Geocodio\Geocodio as GeocodioApi;
use Drupal\geolocation\KeyProvider;

/**
 * Provides a Geocodio integration.
 *
 * @Geocoder(
 *   id = "geocodio",
 *   name = @Translation("Geocodio"),
 *   description = @Translation("See https://www.geocod.io/docs/ for details."),
 *   locationCapable = true,
 *   boundaryCapable = false,
 *   frontendCapable = false,
 * )
 */
class Geocodio extends GeocoderBase implements GeocoderInterface {

  /**
   * {@inheritdoc}
   */
  public function geocode($address) {
    if (empty($address)) {
      return FALSE;
    }

    // Get config.
    $config = \Drupal::config('geolocation_geocodio.settings');
    $fields = $config
      ->get('fields');
    $location = [];

    // Set up connection to geocod.io.
    $geocoder = new GeocodioAPI();
    $key = KeyProvider::getKeyValue($config
      ->get('api_key'));
    $geocoder
      ->setApiKey($key);

    // Attempt to geolocate address.
    try {

      // If fields are defined in settings pull associated
      // metadata.
      if (!empty($fields)) {
        $fields = explode(',', $fields);
        $result = $geocoder
          ->geocode($address, $fields);
      }
      else {
        $result = $geocoder
          ->geocode($address);
      }
    } catch (RequestException $e) {
      watchdog_exception('geolocation', $e);
      return FALSE;
    }
    $results = $result->results[0] ?? FALSE;

    // If no results, return false.
    if (!$results) {
      return FALSE;
    }
    else {
      $location['location'] = [
        'lat' => $results->location->lat,
        'lng' => $results->location->lng,
      ];
    }

    // Add formatted address if it exists.
    if (!empty($results->formatted_address)) {
      $location['address'] = $results->formatted_address;
    }

    // Add metadata coming from fields if it exists.
    if (!empty($results->fields)) {
      $location['metadata'] = $results->fields;
    }
    return $location;
  }

}

Classes

Namesort descending Description
Geocodio Provides a Geocodio integration.