You are here

class GooglePlacesAPI in Geolocation Field 8

Same name and namespace in other branches
  1. 8.3 modules/geolocation_google_maps/modules/geolocation_google_places_api/src/Plugin/geolocation/Geocoder/GooglePlacesAPI.php \Drupal\geolocation_google_places_api\Plugin\geolocation\Geocoder\GooglePlacesAPI
  2. 8.2 modules/geolocation_google_maps/modules/geolocation_google_places_api/src/Plugin/geolocation/Geocoder/GooglePlacesAPI.php \Drupal\geolocation_google_places_api\Plugin\geolocation\Geocoder\GooglePlacesAPI

Provides the Google Places API.

Plugin annotation


@Geocoder(
  id = "google_places_api",
  name = @Translation("Google Places API"),
  description = @Translation("Attention: This Plugin needs you to follow Google Places API TOS and either use the Attribution Block or provide it yourself."),
  locationCapable = true,
  boundaryCapable = true,
)

Hierarchy

Expanded class hierarchy of GooglePlacesAPI

File

modules/geolocation_google_places_api/src/Plugin/geolocation/Geocoder/GooglePlacesAPI.php, line 22

Namespace

Drupal\geolocation_google_places_api\Plugin\geolocation\Geocoder
View source
class GooglePlacesAPI extends GeocoderBase {
  use GoogleMapsDisplayTrait;

  /**
   * {@inheritdoc}
   */
  public function getOptionsForm() {
    $settings = [
      'route' => '',
      'locality' => '',
      'administrativeArea' => '',
      'postalCode' => '',
      'country' => '',
    ];
    if (isset($this->configuration['component_restrictions'])) {
      $settings = array_replace($settings, $this->configuration['component_restrictions']);
    }
    return [
      'description' => [
        '#type' => 'html_tag',
        '#tag' => 'span',
        '#value' => $this
          ->getPluginDefinition()['description'],
      ],
      'component_restrictions' => [
        '#type' => 'fieldset',
        '#title' => $this
          ->t('Component Restrictions'),
        'route' => [
          '#type' => 'textfield',
          '#default_value' => $settings['route'],
          '#title' => $this
            ->t('Route'),
          '#size' => 15,
        ],
        'locality' => [
          '#type' => 'textfield',
          '#default_value' => $settings['locality'],
          '#title' => $this
            ->t('Locality'),
          '#size' => 15,
        ],
        'administrativeArea' => [
          '#type' => 'textfield',
          '#default_value' => $settings['administrativeArea'],
          '#title' => $this
            ->t('Administrative Area'),
          '#size' => 15,
        ],
        'postalCode' => [
          '#type' => 'textfield',
          '#default_value' => $settings['postalCode'],
          '#title' => $this
            ->t('Postal code'),
          '#size' => 5,
        ],
        'country' => [
          '#type' => 'textfield',
          '#default_value' => $settings['country'],
          '#title' => $this
            ->t('Country'),
          '#size' => 5,
        ],
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function formAttachGeocoder(array &$render_array, $element_name) {
    $render_array['#attached']['drupalSettings']['geolocation']['google_map_url'] = $this
      ->getGoogleMapsApiUrl();
    $render_array['geolocation_geocoder_google_places_api'] = [
      '#type' => 'textfield',
      '#description' => $this
        ->t('Enter an address to filter results.'),
      '#attributes' => [
        'class' => [
          'form-autocomplete',
          'geolocation-views-filter-geocoder',
          'geolocation-geocoder-google-places-api',
        ],
        'data-source-identifier' => $element_name,
      ],
    ];
    $render_array['geolocation_geocoder_google_places_api_state'] = [
      '#type' => 'hidden',
      '#default_value' => 1,
      '#attributes' => [
        'class' => [
          'geolocation-geocoder-google-places-api-state',
        ],
        'data-source-identifier' => $element_name,
      ],
    ];
    if (!empty($render_array[$element_name]['#title'])) {
      $render_array['geolocation_geocoder_google_places_api']['#title'] = $render_array[$element_name]['#title'];
    }
    elseif ($this->configuration['label']) {
      $render_array['geolocation_geocoder_google_places_api']['#title'] = $this->configuration['label'];
    }
    $render_array['geolocation_geocoder_google_places_api'] = array_merge_recursive($render_array['geolocation_geocoder_google_places_api'], [
      '#attached' => [
        'library' => [
          0 => 'geolocation_google_places_api/geolocation_google_places_api.geocoder.googleplacesapi',
        ],
      ],
    ]);
    if (!empty($this->configuration['component_restrictions'])) {
      foreach ($this->configuration['component_restrictions'] as $component => $restriction) {
        if (empty($restriction)) {
          continue;
        }
        $render_array['geolocation_geocoder_google_places_api'] = array_merge_recursive($render_array['geolocation_geocoder_google_places_api'], [
          '#attached' => [
            'drupalSettings' => [
              'geolocation' => [
                'geocoder' => [
                  'googlePlacesAPI' => [
                    'restrictions' => [
                      $component => $restriction,
                    ],
                  ],
                ],
              ],
            ],
          ],
        ]);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function formValidateInput(FormStateInterface $form_state) {
    $input = $form_state
      ->getUserInput();
    if (!empty($input['geolocation_geocoder_google_places_api']) && empty($input['geolocation_geocoder_google_places_api_state'])) {
      $location_data = $this
        ->geocode($input['geolocation_geocoder_google_places_api']);
      if (empty($location_data)) {
        $form_state
          ->setErrorByName('geolocation_geocoder_google_places_api', $this
          ->t('Failed to geocode %input.', [
          '%input' => $input['geolocation_geocoder_google_places_api'],
        ]));
        return FALSE;
      }
    }
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function formProcessInput(array &$input, $element_name) {
    if (!empty($input['geolocation_geocoder_google_places_api']) && empty($input['geolocation_geocoder_google_places_api_state'])) {
      $location_data = $this
        ->geocode($input['geolocation_geocoder_google_places_api']);
      if (empty($location_data)) {
        $input['geolocation_geocoder_google_places_api_state'] = 0;
        return FALSE;
      }
      $input['geolocation_geocoder_google_places_api'] = $location_data['address'];
      $input['geolocation_geocoder_google_places_api_state'] = 1;
      return $location_data;
    }
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function geocode($address) {
    if (empty($address)) {
      return FALSE;
    }
    $request_url = 'https://maps.googleapis.com/maps/api/place/autocomplete/json?input=' . $address;
    $google_key = '';
    if (!empty($this->geolocationSettings
      ->get('google_map_api_server_key'))) {
      $google_key = $this->geolocationSettings
        ->get('google_map_api_server_key');
    }
    elseif (!empty($this->geolocationSettings
      ->get('google_map_api_key'))) {
      $google_key = $this->geolocationSettings
        ->get('google_map_api_key');
    }
    if (!empty($google_key)) {
      $request_url .= '&key=' . $google_key;
    }
    if (!empty($this->configuration['component_restrictions']['country'])) {
      $request_url .= '&components=country:' . $this->configuration['component_restrictions']['country'];
    }
    if (!empty($this->geolocationSettings
      ->get('google_map_custom_url_parameters')['language'])) {
      $request_url .= '&language=' . $this->geolocationSettings
        ->get('google_map_custom_url_parameters')['language'];
    }
    try {
      $result = Json::decode(\Drupal::httpClient()
        ->request('GET', $request_url)
        ->getBody());
    } catch (RequestException $e) {
      watchdog_exception('geolocation', $e);
      return FALSE;
    }
    if ($result['status'] != 'OK' || empty($result['predictions'][0]['place_id'])) {
      return FALSE;
    }
    try {
      $details_url = 'https://maps.googleapis.com/maps/api/place/details/json?placeid=' . $result['predictions'][0]['place_id'];
      if (!empty($google_key)) {
        $details_url .= '&key=' . $google_key;
      }
      $details = Json::decode(\Drupal::httpClient()
        ->request('GET', $details_url)
        ->getBody());
    } catch (RequestException $e) {
      watchdog_exception('geolocation', $e);
      return FALSE;
    }
    if ($details['status'] != 'OK' || empty($details['result']['geometry']['location'])) {
      return FALSE;
    }
    return [
      'location' => [
        'lat' => $details['result']['geometry']['location']['lat'],
        'lng' => $details['result']['geometry']['location']['lng'],
      ],
      // TODO: Add viewport or build it if missing.
      'boundary' => [
        'lat_north_east' => empty($details['result']['geometry']['viewport']) ? $details['result']['geometry']['location']['lat'] + 0.005 : $details['result']['geometry']['viewport']['northeast']['lat'],
        'lng_north_east' => empty($details['result']['geometry']['viewport']) ? $details['result']['geometry']['location']['lng'] + 0.005 : $details['result']['geometry']['viewport']['northeast']['lng'],
        'lat_south_west' => empty($details['result']['geometry']['viewport']) ? $details['result']['geometry']['location']['lat'] - 0.005 : $details['result']['geometry']['viewport']['southwest']['lat'],
        'lng_south_west' => empty($details['result']['geometry']['viewport']) ? $details['result']['geometry']['location']['lng'] - 0.005 : $details['result']['geometry']['viewport']['southwest']['lng'],
      ],
      'address' => empty($details['result']['formatted_address']) ? '' : $details['result']['formatted_address'],
    ];
  }

}

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::$geolocationSettings protected property Geolocation settings config instance.
GeocoderBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
GeocoderBase::processOptionsForm public function Process the form built above. Overrides GeocoderInterface::processOptionsForm
GeocoderBase::__construct public function Constructs a new GeocoderBase object. Overrides PluginBase::__construct
GoogleMapsDisplayTrait::$GOOGLEMAPSAPIURL public static property Google maps url with default parameters.
GoogleMapsDisplayTrait::$HYBRID public static property Google map style - Hybrid.
GoogleMapsDisplayTrait::$MAXZOOMLEVEL public static property Google map max zoom level.
GoogleMapsDisplayTrait::$MINZOOMLEVEL public static property Google map min zoom level.
GoogleMapsDisplayTrait::$ROADMAP public static property Google map style - Roadmap.
GoogleMapsDisplayTrait::$SATELLITE public static property Google map style - Satellite.
GoogleMapsDisplayTrait::$TERRAIN public static property Google map style - Terrain.
GoogleMapsDisplayTrait::getGoogleMapDefaultSettings public static function Provide a populated settings array.
GoogleMapsDisplayTrait::getGoogleMapsApiParameters public function Return all module and custom defined parameters.
GoogleMapsDisplayTrait::getGoogleMapsApiUrl public function Return the fully build URL to load Google Maps API.
GoogleMapsDisplayTrait::getGoogleMapsSettings public function Provide settings ready to handover to JS to feed to Google Maps.
GoogleMapsDisplayTrait::getGoogleMapsSettingsForm public function Provide a generic map settings form array.
GoogleMapsDisplayTrait::getGoogleMapsSettingsSummary public function Provide a summary array to use in field formatters.
GoogleMapsDisplayTrait::getMapTypes private function An array of all available map types.
GoogleMapsDisplayTrait::validateGoogleMapsSettingsForm public function Validate the form elements defined above.
GooglePlacesAPI::formAttachGeocoder public function Attach geocoding logic to input element. Overrides GeocoderBase::formAttachGeocoder
GooglePlacesAPI::formProcessInput public function Process from as altered above. Overrides GeocoderBase::formProcessInput
GooglePlacesAPI::formValidateInput public function Process from as altered above. Overrides GeocoderBase::formValidateInput
GooglePlacesAPI::geocode public function Geocode an address. Overrides GeocoderBase::geocode
GooglePlacesAPI::getOptionsForm public function Return additional options form. Overrides GeocoderBase::getOptionsForm
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.