You are here

yamaps.module in Yandex.Maps 8

Same filename and directory in other branches
  1. 7 yamaps.module

Yandex Maps module main file.

File

yamaps.module
View 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