You are here

class Geocodio in Geolocation Field 8.3

Provides a Geocodio integration.

Plugin annotation


@Geocoder(
  id = "geocodio",
  name = @Translation("Geocodio"),
  description = @Translation("See https://www.geocod.io/docs/ for details."),
  locationCapable = true,
  boundaryCapable = false,
  frontendCapable = false,
)

Hierarchy

Expanded class hierarchy of Geocodio

1 string reference to 'Geocodio'
geolocation_geocodio.schema.yml in modules/geolocation_geocodio/config/schema/geolocation_geocodio.schema.yml
modules/geolocation_geocodio/config/schema/geolocation_geocodio.schema.yml

File

modules/geolocation_geocodio/src/Plugin/geolocation/Geocoder/Geocodio.php, line 26

Namespace

Drupal\geolocation_geocodio\Plugin\geolocation\Geocoder
View source
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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
GeocoderBase::$countryFormatterManager protected property Country formatter manager.
GeocoderBase::addressElements protected function Get formatted address elements from atomics.
GeocoderBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 1
GeocoderBase::formAttachGeocoder public function Attach geocoding logic to input element. Overrides GeocoderInterface::formAttachGeocoder 4
GeocoderBase::getDefaultSettings protected function Return plugin default settings. 2
GeocoderBase::getOptionsForm public function Return additional options form. Overrides GeocoderInterface::getOptionsForm 2
GeocoderBase::getSettings public function Return plugin settings.
GeocoderBase::processOptionsForm public function Process the form built above. Overrides GeocoderInterface::processOptionsForm
GeocoderBase::reverseGeocode public function Reverse geocode an address. Overrides GeocoderInterface::reverseGeocode 3
GeocoderBase::__construct public function GoogleGeocoderBase constructor. Overrides PluginBase::__construct 1
Geocodio::geocode public function Geocode an address. Overrides GeocoderBase::geocode
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.