You are here

abstract class GoogleMapsProviderBase in Geolocation Field 8.2

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

Class GoogleMapsProviderBase.

@package Drupal\geolocation_google_maps

Hierarchy

Expanded class hierarchy of GoogleMapsProviderBase

2 files declare their use of GoogleMapsProviderBase
GoogleMaps.php in modules/geolocation_google_maps/src/Plugin/geolocation/MapProvider/GoogleMaps.php
GoogleStaticMaps.php in modules/geolocation_google_maps/modules/geolocation_google_static_maps/src/Plugin/geolocation/MapProvider/GoogleStaticMaps.php

File

modules/geolocation_google_maps/src/GoogleMapsProviderBase.php, line 13

Namespace

Drupal\geolocation_google_maps
View source
abstract class GoogleMapsProviderBase extends MapProviderBase {

  /**
   * Google map style - Roadmap.
   *
   * @var string
   */
  public static $ROADMAP = 'ROADMAP';

  /**
   * Google map style - Satellite.
   *
   * @var string
   */
  public static $SATELLITE = 'SATELLITE';

  /**
   * Google map style - Hybrid.
   *
   * @var string
   */
  public static $HYBRID = 'HYBRID';

  /**
   * Google map style - Terrain.
   *
   * @var string
   */
  public static $TERRAIN = 'TERRAIN';

  /**
   * Google maps url.
   *
   * @var string
   */
  public static $GOOGLEMAPSAPIURLBASE = 'https://maps.googleapis.com';

  /**
   * Google maps url from PR China.
   *
   * @var string
   */
  public static $GOOGLEMAPSAPIURLBASECHINA = 'https://maps.google.cn';

  /**
   * Google maps url from PR China.
   *
   * @var string
   */
  public static $GOOGLEMAPSAPIURLPATH = '/maps/api';

  /**
   * Return all module and custom defined parameters.
   *
   * @param array $additional_parameters
   *   Additional parameters.
   *
   * @return array
   *   Parameters
   */
  public function getGoogleMapsApiParameters(array $additional_parameters = []) {
    $config = \Drupal::config('geolocation_google_maps.settings');
    $geolocation_parameters = [
      'key' => $config
        ->get('google_map_api_key'),
    ];
    $module_parameters = \Drupal::moduleHandler()
      ->invokeAll('geolocation_google_maps_parameters') ?: [];
    $custom_parameters = $config
      ->get('google_map_custom_url_parameters') ?: [];

    // Set the map language to site language if desired and possible.
    if ($config
      ->get('use_current_language') && \Drupal::moduleHandler()
      ->moduleExists('language')) {
      $custom_parameters['language'] = \Drupal::languageManager()
        ->getCurrentLanguage()
        ->getId();
    }
    $parameters = array_replace_recursive($additional_parameters, $custom_parameters, $module_parameters, $geolocation_parameters);
    foreach ($parameters as $key => $value) {
      if (is_string($value) && $value === '') {
        unset($parameters[$key]);
      }
    }
    return $parameters;
  }

  /**
   * Return the fully build URL to load Google Maps API.
   *
   * @param array $additional_parameters
   *   Additional parameters.
   *
   * @return string
   *   Google Maps API URL
   */
  public function getGoogleMapsApiUrl(array $additional_parameters = []) {
    $config = \Drupal::config('geolocation_google_maps.settings');
    if (!empty($config
      ->get('google_maps_base_url'))) {
      $google_url = $config
        ->get('google_maps_base_url');
    }
    elseif ($config
      ->get('china_mode')) {
      $google_url = static::$GOOGLEMAPSAPIURLBASECHINA;
    }
    else {
      $google_url = static::$GOOGLEMAPSAPIURLBASE;
    }
    $parameters = [];
    foreach ($this
      ->getGoogleMapsApiParameters($additional_parameters) as $parameter => $value) {
      $parameters[$parameter] = is_array($value) ? implode(',', $value) : $value;
    }
    $url = Url::fromUri($google_url . static::$GOOGLEMAPSAPIURLPATH, [
      'query' => $parameters,
      'https' => TRUE,
    ]);
    return $url
      ->toString();
  }

  /**
   * An array of all available map types.
   *
   * @return array
   *   The map types.
   */
  private function getMapTypes() {
    $mapTypes = [
      static::$ROADMAP => 'Road map view',
      static::$SATELLITE => 'Google Earth satellite images',
      static::$HYBRID => 'A mixture of normal and satellite views',
      static::$TERRAIN => 'A physical map based on terrain information',
    ];
    return array_map([
      $this,
      't',
    ], $mapTypes);
  }

  /**
   * {@inheritdoc}
   */
  public static function getDefaultSettings() {
    return array_replace_recursive(parent::getDefaultSettings(), [
      'type' => static::$ROADMAP,
      'zoom' => 10,
      'height' => '400px',
      'width' => '100%',
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function getSettings(array $settings) {
    $settings = parent::getSettings($settings);
    $settings['zoom'] = (int) $settings['zoom'];
    return $settings;
  }

  /**
   * {@inheritdoc}
   */
  public function getSettingsSummary(array $settings) {
    $types = $this
      ->getMapTypes();
    $settings = array_replace_recursive(self::getDefaultSettings(), $settings);
    $summary = parent::getSettingsSummary($settings);
    $summary[] = $this
      ->t('Map Type: @type', [
      '@type' => $types[$settings['type']],
    ]);
    $summary[] = $this
      ->t('Zoom level: @zoom', [
      '@zoom' => $settings['zoom'],
    ]);
    $summary[] = $this
      ->t('Height: @height', [
      '@height' => $settings['height'],
    ]);
    $summary[] = $this
      ->t('Width: @width', [
      '@width' => $settings['width'],
    ]);
    return $summary;
  }

  /**
   * {@inheritdoc}
   */
  public function getSettingsForm(array $settings, array $parents = []) {
    $settings = $this
      ->getSettings($settings);
    $parents_string = '';
    if ($parents) {
      $parents_string = implode('][', $parents) . '][';
    }
    $form = parent::getSettingsForm($settings, $parents);

    /*
     * General settings.
     */
    $form['general_settings'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('General'),
    ];
    $form['height'] = [
      '#group' => $parents_string . 'general_settings',
      '#type' => 'textfield',
      '#title' => $this
        ->t('Height'),
      '#description' => $this
        ->t('Enter the dimensions and the measurement units. E.g. 200px or 100%.'),
      '#size' => 4,
      '#default_value' => $settings['height'],
    ];
    $form['width'] = [
      '#group' => $parents_string . 'general_settings',
      '#type' => 'textfield',
      '#title' => $this
        ->t('Width'),
      '#description' => $this
        ->t('Enter the dimensions and the measurement units. E.g. 200px or 100%.'),
      '#size' => 4,
      '#default_value' => $settings['width'],
    ];
    $form['type'] = [
      '#group' => $parents_string . 'general_settings',
      '#type' => 'select',
      '#title' => $this
        ->t('Default map type'),
      '#options' => $this
        ->getMapTypes(),
      '#default_value' => $settings['type'],
      '#process' => [
        [
          '\\Drupal\\Core\\Render\\Element\\RenderElement',
          'processGroup',
        ],
        [
          '\\Drupal\\Core\\Render\\Element\\Select',
          'processSelect',
        ],
      ],
      '#pre_render' => [
        [
          '\\Drupal\\Core\\Render\\Element\\RenderElement',
          'preRenderGroup',
        ],
      ],
    ];
    $form['zoom'] = [
      '#group' => $parents_string . 'general_settings',
      '#type' => 'number',
      '#title' => $this
        ->t('Zoom level'),
      '#description' => $this
        ->t('The initial resolution at which to display the map, where zoom 0 corresponds to a map of the Earth fully zoomed out, and higher zoom levels zoom in at a higher resolution up to 20 for streetlevel.'),
      '#default_value' => $settings['zoom'],
      '#process' => [
        [
          '\\Drupal\\Core\\Render\\Element\\RenderElement',
          'processGroup',
        ],
      ],
      '#pre_render' => [
        [
          '\\Drupal\\Core\\Render\\Element\\Number',
          'preRenderNumber',
        ],
        [
          '\\Drupal\\Core\\Render\\Element\\RenderElement',
          'preRenderGroup',
        ],
      ],
    ];
    if ($parents_string) {
      $form['zoom']['#group'] = $parents_string . 'general_settings';
    }
    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
GoogleMapsProviderBase::$GOOGLEMAPSAPIURLBASE public static property Google maps url.
GoogleMapsProviderBase::$GOOGLEMAPSAPIURLBASECHINA public static property Google maps url from PR China.
GoogleMapsProviderBase::$GOOGLEMAPSAPIURLPATH public static property Google maps url from PR China. 2
GoogleMapsProviderBase::$HYBRID public static property Google map style - Hybrid.
GoogleMapsProviderBase::$ROADMAP public static property Google map style - Roadmap.
GoogleMapsProviderBase::$SATELLITE public static property Google map style - Satellite.
GoogleMapsProviderBase::$TERRAIN public static property Google map style - Terrain.
GoogleMapsProviderBase::getDefaultSettings public static function Provide a populated settings array. Overrides MapProviderBase::getDefaultSettings 2
GoogleMapsProviderBase::getGoogleMapsApiParameters public function Return all module and custom defined parameters. 1
GoogleMapsProviderBase::getGoogleMapsApiUrl public function Return the fully build URL to load Google Maps API.
GoogleMapsProviderBase::getMapTypes private function An array of all available map types.
GoogleMapsProviderBase::getSettings public function Provide map provider specific settings ready to handover to JS. Overrides MapProviderBase::getSettings 1
GoogleMapsProviderBase::getSettingsForm public function Provide a generic map settings form array. Overrides MapProviderBase::getSettingsForm 2
GoogleMapsProviderBase::getSettingsSummary public function Provide a summary array to use in field formatters. Overrides MapProviderBase::getSettingsSummary
MapProviderBase::$mapFeatureManager protected property Map feature manager.
MapProviderBase::alterCommonMap public function Alter common map build array. Overrides MapProviderInterface::alterCommonMap 4
MapProviderBase::alterRenderArray public function Alter render array. Overrides MapProviderInterface::alterRenderArray 5
MapProviderBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
MapProviderBase::getControlPositions public static function Return available control positions. Overrides MapProviderInterface::getControlPositions 4
MapProviderBase::validateMapFeatureForms public function Validate form.
MapProviderBase::__construct public function Constructs a new GeocoderBase object. Overrides PluginBase::__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.