yamaps.module in Yandex.Maps 8
Same filename and directory in other branches
Yandex Maps module main file.
File
yamaps.moduleView source
<?php
/**
* @file
* Yandex Maps module main file.
*/
use Drupal\Component\Serialization\Json;
use Drupal\yamaps\Plugin\Field\FieldType\YamapsFieldType;
/**
* Prepares values for js.
*
* @param \Drupal\yamaps\Plugin\Field\FieldType\YamapsFieldType $value
* Values of map.
*
* @return array
* Prepared values.
*/
function yamaps_format_values_to_js(YamapsFieldType $value) {
return [
'coords' => isset($value->coords) ? Json::decode($value->coords) : [],
'type' => $value->coordstype,
'placemarks' => Json::decode($value->placemarks),
'lines' => Json::decode($value->lines),
'polygons' => Json::decode($value->polygons),
];
}
/**
* Implements hook_theme().
*/
function yamaps_theme() {
return [
'yamaps_field_formatter' => [
'variables' => [
'height' => '',
'width' => '',
'map_id' => '',
],
],
];
}
/**
* Implements yamaps_preprocess_page().
*/
function yamaps_preprocess_page(&$variables) {
$current_path = \Drupal::service('path.current')
->getPath();
$patterns = "/admin/structure/block/*\n/admin/structure/views/*";
if (\Drupal::service('path.matcher')
->matchPath($current_path, $patterns)) {
$variables['#attached']['library'][] = 'yamaps/yandex-map-api';
}
}
/**
* Implements hook_library_info_alter().
*/
function yamaps_library_info_alter(&$libraries, $extension) {
// Update yandex map API url
if (isset($libraries['yandex-map-api'])) {
$key = \Drupal::config('yamaps.settings')
->get('yamaps_api_key');
if (!empty($key)) {
$oldDescription = $libraries['yandex-map-api']['js'];
$url = key($oldDescription);
$params = array_values($oldDescription);
$url .= '&apikey=' . $key;
$libraries['yandex-map-api']['js'] = [
$url => $params,
];
}
}
}
Functions
Name | Description |
---|---|
yamaps_format_values_to_js | Prepares values for js. |
yamaps_library_info_alter | Implements hook_library_info_alter(). |
yamaps_preprocess_page | Implements yamaps_preprocess_page(). |
yamaps_theme | Implements hook_theme(). |