colorized_gmap-8.x-1.x-dev/colorized_gmap.module

colorized_gmap.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
 
/**
 * @file
 * Colorized gmap module file.
 */
 
use Drupal\Core\Form\FormStateInterface;
 
/**
 * Implements hook_library_info_alter().
 *
 * Change the google map library url to add the custom Google API key.
 */
function colorized_gmap_library_info_alter(&$libraries, $extension) {
  if (isset($libraries['colorized_gmap.gmap_api'])) {
    $old_path = array_keys($libraries['colorized_gmap.gmap_api']['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']);
 
      $config = \Drupal::config('colorized_gmap.settings');
      $settings = $config->get();
      $auth_method = isset($settings['colorized_gmap_auth_method']) ? $settings['colorized_gmap_auth_method'] : 1;
 
      switch ($auth_method) {
        case 1:
          $js_url_query['key'] = isset($settings['colorized_gmap_api_key']) ? $settings['colorized_gmap_api_key'] : '';
          break;
 
        case 2:
          $js_url_query['client'] = $settings['colorized_gmap_client_id'];
          $js_url_query['signature'] = $settings['colorized_gmap_private_key'];
          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['colorized_gmap.gmap_api']['js'][$old_path] as $key => $option) {
        $new_js[$new_js_url][$key] = $option;
      }
      $libraries['colorized_gmap.gmap_api']['js'] = $new_js;
    }
  }
}
 
/**
 * Implements hook_theme().
 */
function colorized_gmap_theme() {
  return [
    'colorized_gmap_output' => [
      'variables' => [
        'machine_name' => NULL,
      ],
      'template' => 'colorized-gmap-output',
    ],
  ];
}
 
/**
 * Implements hook_form_alter().
 */
function colorized_gmap_form_block_admin_display_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $form['#attached']['library'][] = 'colorized_gmap/colorized_gmap.gmap_api';
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc