-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepeat_mapbox.module
157 lines (147 loc) · 5.34 KB
/
repeat_mapbox.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/**
* @file
* Provides map related functionality.
*/
declare(strict_types = 1);
use Drupal\Component\Utility\Html;
use Drupal\Core\Link;
use Drupal\views\ViewExecutable;
use Drupal\views\Views;
/**
* Implements hook_preprocess_HOOK().
*/
function repeat_mapbox_preprocess_paragraph(array &$vars): void {
/** @var \Drupal\paragraphs\Entity\Paragraph $paragraph */
$paragraph = $vars['paragraph'];
if ($paragraph->getType() === 'mapbox_map') {
$config = Drupal::config('repeat_mapbox.settings')->get();
if (isset($config['token'])) {
$vars['#attached']['drupalSettings']['repeatMapbox']['settings'] = $config;
$vars['#attached']['library'][] = 'repeat_mapbox/mapbox';
$vars['content']['map'] = [
'#weight' => -100,
'#type' => 'container',
'#attributes' => [
'id' => Html::getId('map'),
'class' => ['mapbox_map', 'repeat_mapbox'],
'data-mapbox-id' => $paragraph->id(),
],
];
if ($paragraph->hasField('field_repeat_mapbox_base_url') && !$paragraph->get('field_repeat_mapbox_base_url')->isEmpty()) {
$vars['content']['map']['#attributes']['data-style'] = $paragraph->get('field_repeat_mapbox_base_url')->value;
}
if ($paragraph->hasField('field_repeat_mapbox_data_view') && !$paragraph->get('field_repeat_mapbox_data_view')->isEmpty()) {
/** @var ViewExecutable $list_view */
$list_view = $vars["content"]["field_repeat_mapbox_list_view"][0]["contents"]["#view"];
$list_view->build_info['mapbox'] = $paragraph->get('field_repeat_mapbox_data_view')->first()->getValue();
$list_view->build_info['mapbox']['map_id'] = $paragraph->id();
}
}
else {
Drupal::logger('Repeat Mapbox')->error('Mapbox access token not set.', [
'link' => Link::createFromRoute('Configure Mapbox', 'repeat_mapbox.mapbox_settings')
->toString(TRUE)
->getGeneratedLink(),
]);
$vars['content']['map'] = [
'#markup' => t('Map unavailable'),
'#weight' => -100,
];
}
}
}
/**
* Get data from a view for use in the map.
*
* Usually used to attach data as a json array with decode_json() to
* drupalSettings in a paragraph or views preprocess.
*
* @param \Drupal\views\ViewExecutable|null $parent_view
* View to get arguments from.
* @param string $json_view
* The view name to load.
* @param string $display
* The view display to load - should be a GEOJson or REST Export.
*
* @return string
* Rendered view.
*/
function _repeat_mapbox_get_rendered_view(string $json_view, string $display, ?ViewExecutable $parent_view = NULL): string {
$json_view = Views::getView($json_view);
if (empty($json_view->storage->get('display')[$display])) {
Drupal::logger('Repeat Mapbox')->error('Invalid display passed to _repeat_mapbox_get_rendered_view().');
return '[]';
}
$json_view->setDisplay($display);
if ($parent_view) {
$args = $parent_view->args;
$exposed_input = $parent_view->getExposedInput();
if (!empty($exposed_input['search_api_fulltext'])) {
$args['search_api_fulltext'] = $exposed_input['search_api_fulltext'];
}
$json_view->setArguments($args);
$json_view->setExposedInput($exposed_input);
}
$rendered_view = $json_view->render();
return $rendered_view['#markup']->__toString();
}
/**
* Implements hook_geo_json_view().
*
* Mapbox doesn't handle multipoint geometries in geojson so split them out to
* single Point geometries with the same data.
*/
function repeat_mapbox_geojson_view_alter(array &$features, ViewExecutable $view): void {
// Only update geojson views that are tagged as mapbox views.
$view_entity = \Drupal\views\Entity\View::load($view->id());
$tags = \Drupal\Component\Utility\Tags::explode($view_entity->get('tag'));
if (!in_array('mapbox', $tags)) {
return;
}
$new = [
'type' => 'FeatureCollection',
'features' => [],
];
foreach ($features['features'] as $feature) {
if ($feature['geometry']['type'] === 'Point') {
$new['features'][] = $feature;
}
elseif ($feature['geometry']['type'] === 'MultiPoint') {
foreach ($feature['geometry']['coordinates'] as $point) {
$feat = $feature;
$feat['geometry']['type'] = 'Point';
$feat['geometry']['coordinates'] = $point;
$new['features'][] = $feat;
}
}
else {
$new['features'][] = $feature;
}
}
$features = $new;
}
/**
* Implements hook_preprocess_HOOK().
*/
function repeat_mapbox_preprocess_views_view(array &$variables): void {
/** @var \Drupal\views\ViewExecutable $view */
$view = $variables['view'];
if (!empty($view->build_info['mapbox'])) {
$data = $view->build_info['mapbox'];
}
else {
$request = Drupal::request();
if ($request->query->has('mapbox')) {
$query = $request->query->all();
$data = $query['mapbox'] ?? [];
}
else {
return;
}
}
$variables['#attached']['drupalSettings']['repeatMapbox'][$data['map_id']]['items'] = json_decode(_repeat_mapbox_get_rendered_view($data['target_id'], $data['display_id'], $view), TRUE);
$variables['#attached']['drupalSettings']['repeatMapbox'][$data['map_id']]['update'] = TRUE;
$variables['#attached']['drupalSettings']['repeatMapbox']['update'] = TRUE;
$variables['#attached']['drupalSettings']['views']['ajaxViews']["views_dom_id:{$view->dom_id}"]['mapbox'] = $data;
}