View source
<?php
namespace Drupal\geolocation_yandex\Plugin\geolocation\MapProvider;
use Drupal\geolocation\MapProviderBase;
use Drupal\Core\Render\BubbleableMetadata;
class Yandex extends MapProviderBase {
public static $apiBaseUrl = 'https://api-maps.yandex.ru/2.1/';
public static function getDefaultSettings() {
return array_replace_recursive(parent::getDefaultSettings(), [
'zoom' => 10,
'height' => '400px',
'width' => '100%',
]);
}
public function getSettings(array $settings) {
$settings = parent::getSettings($settings);
$settings['zoom'] = (int) $settings['zoom'];
return $settings;
}
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;
}
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;
}
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,
],
],
],
],
],
'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);
}
public static function getControlPositions() {
return [
'top' => t('Top'),
'right' => t('Right'),
'left' => t('Left'),
'bottom' => t('Bottom'),
];
}
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'),
];
}
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;
}
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";
}
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;
}
}