forked from open-y-subprojects/openy_map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopeny_map.module
105 lines (93 loc) · 3.26 KB
/
openy_map.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
/**
* @file
* Module file.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
/**
* Implements hook_theme().
*/
function openy_map_theme() {
$items = [
'openy_map' => [
'#options' => [],
'render element' => 'element',
],
];
return $items;
}
/**
* Implements hook_entity_view_alter().
*/
function openy_map_entity_view_alter(&$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
if ($entity->getEntityTypeID() != 'node' || $display->getMode() != 'teaser') {
return;
}
$activeTypes = \Drupal::configFactory()->get('openy_map.settings')->get('active_types');
$activeTypes = !empty($activeTypes) ? array_keys(array_filter($activeTypes)) : [];
if (!in_array($entity->getType(), $activeTypes)) {
return;
}
// Add attribute with location data.
$typeLabels = \Drupal::config('openy_map.settings')->get('type_labels');
$values = [
'tags' => $typeLabels[$entity->getType()],
];
$build['#attributes']['data-locations'] = json_encode($values);
$amenities = [];
if ($entity->hasField('field_location_amenities')) {
$entityAmenties = $entity->get('field_location_amenities')->getValue();
foreach ($entityAmenties as $amenity) {
$amenities[] = $amenity['target_id'];
}
}
$build['#attributes']['data-openy-map-location-id'] = $entity->id();
$build['#attributes']['data-amenities'] = json_encode($amenities);
}
/**
* Implements hook_library_info_alter().
*/
function openy_map_library_info_alter(&$libraries, $extension) {
if ($extension != 'openy_map') {
return;
}
// Adding Google Maps API key.
foreach ($libraries['gmaps']['js'] as $key => $value) {
if ($key != 'https://maps.googleapis.com/maps/api/js') {
continue;
}
$api_key = \Drupal::configFactory()->get('geolocation_google_maps.settings')->get('google_map_api_key');
unset($libraries['gmaps']['js'][$key]);
$libraries['gmaps']['js'][$key . '?key=' . $api_key] = $value;
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function openy_map_theme_suggestions_node_alter(array &$suggestions, array $variables) {
/** @var \Drupal\node\Entity\Node $node */
$node = $variables['elements']['#node'];
$view_mode = $variables['elements']['#view_mode'];
// Check for active types.
$activeTypes = \Drupal::configFactory()->get('openy_map.settings')->get('active_types');
$activeTypes = !empty($activeTypes) ? array_keys(array_filter($activeTypes)) : [];
// Add universal template for all location content type teasers.
if ($view_mode == 'teaser' && $node->hasField('field_location_coordinates') && in_array($node->bundle(), $activeTypes)) {
$suggestions[] = 'node__location_type__teaser';
}
}
/**
* Helper function.
*
* Returns default location set for leaflet.
*/
function _openy_map_get_default_location() {
$settings = \Drupal::configFactory()->get('openy_map.settings');
if ($default_location = $settings->get('leaflet.location')) {
return $default_location;
}
$countries = \Drupal::service('country_manager')->getList();
$default_country = \Drupal::configFactory()->get('system.date')->get('country.default');
return !empty($countries[$default_country]) ? $countries[$default_country] : 'United States of America';
}