You are here

google_map_field.module in Google Map Field 8

Same filename and directory in other branches
  1. 7.2 google_map_field.module
  2. 7 google_map_field.module

File

google_map_field.module
View source
<?php

/**
 * @file
 * Contains google_map_field.module.
 */

/**
 * Implements hook_theme().
 */
function google_map_field_theme($existing, $type, $theme, $path) {
  return [
    'google_map_field' => [
      'variables' => [
        'name' => NULL,
        'lat' => NULL,
        'lon' => NULL,
        'zoom' => NULL,
        'type' => NULL,
        'width' => NULL,
        'height' => NULL,
        'show_marker' => NULL,
        'marker_icon' => NULL,
        'traffic' => NULL,
        'show_controls' => NULL,
        'infowindow' => NULL,
      ],
      'template' => 'google_map_field',
    ],
    'google_map_field_embed' => [
      'variables' => [
        'name' => NULL,
        'lat' => NULL,
        'lon' => NULL,
        'zoom' => NULL,
        'type' => NULL,
        'width' => NULL,
        'height' => NULL,
        'show_marker' => NULL,
        'show_controls' => NULL,
        'infowindow' => NULL,
        'api_key' => NULL,
      ],
      'template' => 'google_map_field_embed',
    ],
    'olmap_field' => [
      'variables' => [
        'name' => NULL,
        'lat' => NULL,
        'lon' => NULL,
        'zoom' => NULL,
        'width' => NULL,
        'height' => NULL,
        'show_marker' => NULL,
        'show_controls' => NULL,
        'infowindow' => NULL,
        'default_marker' => NULL,
      ],
      'template' => 'olmap_field',
    ],
  ];
}

/**
 * Implements hook_library_info_alter().
 *
 * Change the google map library url to add the custom Google API key.
 */
function google_map_field_library_info_alter(&$libraries, $extension) {
  $config = \Drupal::config('google_map_field.settings');
  $settings = $config
    ->get();
  if (isset($libraries['google-map-apis'])) {
    $old_path = array_keys($libraries['google-map-apis']['js']);
    $old_path = $old_path[0];
    if (strpos($old_path, 'key') == FALSE) {
      $js_url = parse_url($old_path);
      parse_str($js_url['query'], $js_url_query);

      // Modify the query parameters.
      unset($js_url_query['sensor']);
      switch ($settings['google_map_field_auth_method']) {
        case 1:
          $js_url_query['key'] = $settings['google_map_field_apikey'];
          break;
        case 2:
          $js_url_query['client'] = $settings['google_map_field_map_client_id'];
          break;
      }

      // Build the new js url with the modified params.
      $js_url['query'] = http_build_query($js_url_query);
      $new_js_url = '//' . $js_url['host'] . $js_url['path'] . '?' . $js_url['query'];
      $new_js = [
        $new_js_url => [],
      ];
      foreach ($libraries['google-map-apis']['js'][$old_path] as $key => $option) {
        $new_js[$new_js_url][$key] = $option;
      }
      $libraries['google-map-apis']['js'] = $new_js;
    }
  }
}