You are here

abstract class GoogleGeocoderBase in Geolocation Field 8.2

Same name and namespace in other branches
  1. 8.3 modules/geolocation_google_maps/src/GoogleGeocoderBase.php \Drupal\geolocation_google_maps\GoogleGeocoderBase

Base class.

@package Drupal\geolocation_google_places_api

Hierarchy

Expanded class hierarchy of GoogleGeocoderBase

2 files declare their use of GoogleGeocoderBase
GoogleGeocodingAPI.php in modules/geolocation_google_maps/src/Plugin/geolocation/Geocoder/GoogleGeocodingAPI.php
GooglePlacesAPI.php in modules/geolocation_google_maps/modules/geolocation_google_places_api/src/Plugin/geolocation/Geocoder/GooglePlacesAPI.php

File

modules/geolocation_google_maps/src/GoogleGeocoderBase.php, line 17

Namespace

Drupal\geolocation_google_maps
View source
abstract class GoogleGeocoderBase extends GeocoderBase implements GeocoderInterface {

  /**
   * Google maps provider.
   *
   * @var \Drupal\geolocation_google_maps\Plugin\geolocation\MapProvider\GoogleMaps
   */
  protected $googleMapsProvider;

  /**
   * GoogleGeocoderBase constructor.
   *
   * @param array $configuration
   *   Configuration.
   * @param string $plugin_id
   *   Plugin ID.
   * @param mixed $plugin_definition
   *   Plugin definition.
   * @param \Drupal\geolocation\GeocoderCountryFormattingManager $geocoder_country_formatter_manager
   *   Country formatter manager.
   * @param \Drupal\geolocation\MapProviderManager $map_provider_manager
   *   Map provider management.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, GeocoderCountryFormattingManager $geocoder_country_formatter_manager, MapProviderManager $map_provider_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $geocoder_country_formatter_manager);
    $this->googleMapsProvider = $map_provider_manager
      ->getMapProvider('google_maps');
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('plugin.manager.geolocation.geocoder_country_formatting'), $container
      ->get('plugin.manager.geolocation.mapprovider'));
  }

  /**
   * {@inheritdoc}
   */
  protected function getDefaultSettings() {
    $default_settings = parent::getDefaultSettings();
    $default_settings['component_restrictions'] = [
      'route' => '',
      'locality' => '',
      'administrative_area' => '',
      'postal_code' => '',
      'country' => '',
    ];
    $default_settings['boundary_restriction'] = [
      'south' => '',
      'west' => '',
      'north' => '',
      'east' => '',
    ];
    return $default_settings;
  }

  /**
   * {@inheritdoc}
   */
  public function formAttachGeocoder(array &$render_array, $element_name) {
    parent::formAttachGeocoder($render_array, $element_name);
    $render_array['#attached'] = BubbleableMetadata::mergeAttachments(empty($render_array['#attached']) ? [] : $render_array['#attached'], [
      'library' => [
        'geolocation_google_maps/googlemapsapi',
        'geolocation_google_maps/google',
      ],
    ]);
    if (!empty($this->configuration['component_restrictions'])) {
      foreach ($this->configuration['component_restrictions'] as $component => $restriction) {
        if (empty($restriction)) {
          continue;
        }
        switch ($component) {
          case 'administrative_area':
            $component = 'administrativeArea';
            break;
          case 'postal_code':
            $component = 'postalCode';
            break;
        }
        $render_array['#attached'] = BubbleableMetadata::mergeAttachments(empty($render_array['#attached']) ? [] : $render_array['#attached'], [
          'drupalSettings' => [
            'geolocation' => [
              'geocoder' => [
                $this
                  ->getPluginId() => [
                  'componentRestrictions' => [
                    $component => $restriction,
                  ],
                ],
              ],
            ],
          ],
        ]);
      }
    }
    if (!empty($this->configuration['boundary_restriction'])) {
      $bounds = [];
      foreach ($this->configuration['boundary_restriction'] as $key => $value) {
        if (empty($value)) {
          return;
        }
        $bounds[$key] = (double) $value;
      }
      if (!empty($bounds)) {
        $render_array['#attached'] = BubbleableMetadata::mergeAttachments(empty($render_array['#attached']) ? [] : $render_array['#attached'], [
          'drupalSettings' => [
            'geolocation' => [
              'geocoder' => [
                $this
                  ->getPluginId() => [
                  'bounds' => $bounds,
                ],
              ],
            ],
          ],
        ]);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getOptionsForm() {
    $settings = $this
      ->getSettings();
    $form = parent::getOptionsForm();
    $form += [
      'component_restrictions' => [
        '#type' => 'fieldset',
        '#title' => $this
          ->t('Component Restrictions'),
        '#description' => $this
          ->t('See <a href="https://developers.google.com/maps/documentation/geocoding/intro#ComponentFiltering">Component Filtering</a>'),
        'route' => [
          '#type' => 'textfield',
          '#default_value' => $settings['component_restrictions']['route'],
          '#title' => $this
            ->t('Route'),
          '#size' => 15,
        ],
        'locality' => [
          '#type' => 'textfield',
          '#default_value' => $settings['component_restrictions']['locality'],
          '#title' => $this
            ->t('Locality'),
          '#size' => 15,
        ],
        'administrative_area' => [
          '#type' => 'textfield',
          '#default_value' => $settings['component_restrictions']['administrative_area'],
          '#title' => $this
            ->t('Administrative Area'),
          '#size' => 15,
        ],
        'postal_code' => [
          '#type' => 'textfield',
          '#default_value' => $settings['component_restrictions']['postal_code'],
          '#title' => $this
            ->t('Postal code'),
          '#size' => 5,
        ],
        'country' => [
          '#type' => 'textfield',
          '#default_value' => $settings['component_restrictions']['country'],
          '#title' => $this
            ->t('Country'),
          '#size' => 5,
        ],
      ],
      'boundary_restriction' => [
        '#type' => 'fieldset',
        '#title' => $this
          ->t('Boundary Restriction'),
        '#description' => $this
          ->t('See <a href="https://developers.google.com/maps/documentation/geocoding/intro#Viewports">Viewports</a>'),
        'south' => [
          '#type' => 'textfield',
          '#default_value' => $settings['boundary_restriction']['south'],
          '#title' => $this
            ->t('South'),
          '#size' => 15,
        ],
        'west' => [
          '#type' => 'textfield',
          '#default_value' => $settings['boundary_restriction']['west'],
          '#title' => $this
            ->t('West'),
          '#size' => 15,
        ],
        'north' => [
          '#type' => 'textfield',
          '#default_value' => $settings['boundary_restriction']['north'],
          '#title' => $this
            ->t('North'),
          '#size' => 15,
        ],
        'east' => [
          '#type' => 'textfield',
          '#default_value' => $settings['boundary_restriction']['east'],
          '#title' => $this
            ->t('East'),
          '#size' => 15,
        ],
      ],
    ];
    return $form;
  }

}

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::geocode public function Geocode an address. Overrides GeocoderInterface::geocode 6
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 2
GoogleGeocoderBase::$googleMapsProvider protected property Google maps provider.
GoogleGeocoderBase::create public static function Creates an instance of the plugin. Overrides GeocoderBase::create
GoogleGeocoderBase::formAttachGeocoder public function Attach geocoding logic to input element. Overrides GeocoderBase::formAttachGeocoder 2
GoogleGeocoderBase::getDefaultSettings protected function Return plugin default settings. Overrides GeocoderBase::getDefaultSettings
GoogleGeocoderBase::getOptionsForm public function Return additional options form. Overrides GeocoderBase::getOptionsForm
GoogleGeocoderBase::__construct public function GoogleGeocoderBase constructor. Overrides GeocoderBase::__construct
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.