You are here

class Yandex in Geolocation Field 8.3

Same name in this branch
  1. 8.3 modules/geolocation_yandex/src/Plugin/geolocation/MapProvider/Yandex.php \Drupal\geolocation_yandex\Plugin\geolocation\MapProvider\Yandex
  2. 8.3 modules/geolocation_yandex/src/Plugin/geolocation/Geocoder/Yandex.php \Drupal\geolocation_yandex\Plugin\geolocation\Geocoder\Yandex
Same name and namespace in other branches
  1. 8.2 modules/geolocation_yandex/src/Plugin/geolocation/MapProvider/Yandex.php \Drupal\geolocation_yandex\Plugin\geolocation\MapProvider\Yandex

Provides Yandex Maps API.

Plugin annotation


@MapProvider(
  id = "yandex",
  name = @Translation("Yandex Maps"),
  description = @Translation("Yandex support."),
)

Hierarchy

Expanded class hierarchy of Yandex

3 files declare their use of Yandex
ControlElementBase.php in modules/geolocation_yandex/src/Plugin/geolocation/MapFeature/ControlElementBase.php
Yandex.php in modules/geolocation_yandex/src/Plugin/geolocation/Geocoder/Yandex.php
YandexMapsSettings.php in modules/geolocation_yandex/src/Form/YandexMapsSettings.php

File

modules/geolocation_yandex/src/Plugin/geolocation/MapProvider/Yandex.php, line 17

Namespace

Drupal\geolocation_yandex\Plugin\geolocation\MapProvider
View source
class Yandex extends MapProviderBase {

  /**
   * Yandex API Url.
   *
   * @var string
   */
  public static $apiBaseUrl = 'https://api-maps.yandex.ru/2.1/';

  /**
   * {@inheritdoc}
   */
  public static function getDefaultSettings() {
    return array_replace_recursive(parent::getDefaultSettings(), [
      '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) {
    $settings = array_replace_recursive(self::getDefaultSettings(), $settings);
    $summary = parent::getSettingsSummary($settings);
    $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 += self::getDefaultSettings();
    if ($parents) {
      $parents_string = implode('][', $parents);
    }
    else {
      $parents_string = NULL;
    }
    $form = parent::getSettingsForm($settings, $parents);
    $form['height'] = [
      '#group' => $parents_string,
      '#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,
      '#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['zoom'] = [
      '#group' => $parents_string,
      '#type' => 'select',
      '#title' => $this
        ->t('Zoom level'),
      '#options' => range(0, 20),
      '#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.'),
      '#default_value' => $settings['zoom'],
      '#process' => [
        [
          '\\Drupal\\Core\\Render\\Element\\RenderElement',
          'processGroup',
        ],
        [
          '\\Drupal\\Core\\Render\\Element\\Select',
          'processSelect',
        ],
      ],
      '#pre_render' => [
        [
          '\\Drupal\\Core\\Render\\Element\\RenderElement',
          'preRenderGroup',
        ],
      ],
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function alterRenderArray(array $render_array, array $map_settings, array $context = []) {
    $yandex_url_parts = parse_url(self::$apiBaseUrl);
    $render_array['#attached'] = BubbleableMetadata::mergeAttachments(empty($render_array['#attached']) ? [] : $render_array['#attached'], [
      'library' => [
        'geolocation_yandex/geolocation.yandex',
      ],
      'drupalSettings' => [
        'geolocation' => [
          'maps' => [
            $render_array['#id'] => [
              'settings' => [
                'yandex_settings' => $map_settings,
              ],
            ],
          ],
        ],
      ],
      // Add 'preconnect' resource hint.
      'html_head' => [
        [
          [
            '#tag' => 'link',
            '#attributes' => [
              'rel' => 'preconnect',
              'href' => $yandex_url_parts['scheme'] . "://" . $yandex_url_parts['host'],
            ],
          ],
          'geolocation_yandex_link_preconnect_map',
        ],
      ],
    ]);
    return parent::alterRenderArray($render_array, $map_settings, $context);
  }

  /**
   * {@inheritdoc}
   */
  public static function getControlPositions() {
    return [
      'top' => t('Top'),
      'right' => t('Right'),
      'left' => t('Left'),
      'bottom' => t('Bottom'),
    ];
  }

  /**
   * Selection of Yandex API packages.
   *
   * @see https://tech.yandex.ru/maps/archive/doc/jsapi/2.0/ref/reference/packages-docpage/
   */
  public static function getPackages() {
    return [
      'full' => t('Full'),
      'standard' => t('Standard'),
      'map' => t('Map'),
      'controls' => t('Controls'),
      'search' => t('Search'),
      'geoObjects' => t('GeoObjects'),
      'clusters' => t('Clusters'),
      'traffic' => t('Traffic'),
      'route' => t('Route'),
      'geoXml' => t('GeoXml'),
      'editor' => t('Editor'),
      'overlays' => t('Overlays'),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function alterCommonMap(array $render_array, array $map_settings, array $context) {
    $render_array['#attached'] = BubbleableMetadata::mergeAttachments(empty($render_array['#attached']) ? [] : $render_array['#attached'], [
      'library' => [
        'geolocation_yandex/commonmap.yandex',
      ],
    ]);
    return $render_array;
  }

  /**
   * Get Yandex API Base URL.
   *
   * @return string
   *   Base Url.
   */
  public function getApiUrl() {
    $config = \Drupal::config('geolocation_yandex.settings');
    $api_key = $config
      ->get('api_key');
    $packages = $config
      ->get('packages');
    foreach ($packages as &$package) {
      $package = 'package.' . $package;
    }
    $packages_str = implode(',', $packages);
    $base_url = self::$apiBaseUrl;
    $langcode = self::getApiUrlLangcode();
    return "{$base_url}?apikey={$api_key}&load={$packages_str}&lang={$langcode}&coordorder=longlat";
  }

  /**
   * Get allowed langcode by language ID.
   *
   * @param string $langId
   *   Two-letter language code.
   *
   * @return string
   *   Yandex API allowed language code.
   */
  public static function getApiUrlLangcode($langId = NULL) {
    if (empty($langId)) {
      $langId = \Drupal::languageManager()
        ->getCurrentLanguage()
        ->getId();
    }
    $langId = strtolower((string) $langId);
    $langcode = 'en_US';
    $langcode_mapping = [
      'ru' => 'ru_RU',
      'uk' => 'uk_UA',
      'tr' => 'tr_TR',
    ];
    if (!empty($langcode_mapping[$langId])) {
      return $langcode_mapping[$langId];
    }
    return $langcode;
  }

}

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
MapProviderBase::$mapFeatureManager protected property Map feature manager.
MapProviderBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
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.
Yandex::$apiBaseUrl public static property Yandex API Url.
Yandex::alterCommonMap public function Alter common map build array. Overrides MapProviderBase::alterCommonMap
Yandex::alterRenderArray public function Alter render array. Overrides MapProviderBase::alterRenderArray
Yandex::getApiUrl public function Get Yandex API Base URL.
Yandex::getApiUrlLangcode public static function Get allowed langcode by language ID.
Yandex::getControlPositions public static function Return available control positions. Overrides MapProviderBase::getControlPositions
Yandex::getDefaultSettings public static function Provide a populated settings array. Overrides MapProviderBase::getDefaultSettings
Yandex::getPackages public static function Selection of Yandex API packages.
Yandex::getSettings public function Provide map provider specific settings ready to handover to JS. Overrides MapProviderBase::getSettings
Yandex::getSettingsForm public function Provide a generic map settings form array. Overrides MapProviderBase::getSettingsForm
Yandex::getSettingsSummary public function Provide a summary array to use in field formatters. Overrides MapProviderBase::getSettingsSummary