This repository has been archived by the owner on Jun 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualization_entity_maps.module
181 lines (162 loc) · 5.58 KB
/
visualization_entity_maps.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
/**
* @file
* Code for the Visualization Entity Maps feature.
*/
include_once 'visualization_entity_maps.features.inc';
/**
* Implements hook_form_alter().
*/
function visualization_entity_maps_form_alter(&$form, $form_state, $form_id) {
switch ($form_id) {
case 'eck__entity__form_add_visualization_ve_map':
case 'eck__entity__form_edit_visualization_ve_map':
visualization_entity_maps_attach_libraries($form);
break;
}
}
/**
* Attach needed libraries to an element.
*/
function visualization_entity_maps_attach_libraries(&$element) {
$libraries = array(
'lodash',
'backbone',
'recline',
'd3',
'gdocs',
'csv',
'mustache',
'leaflet',
'leaflet_markercluster',
'slickgrid',
);
foreach ($libraries as $library) {
$element['#attached']['libraries_load'][] = array($library);
}
$module_path = drupal_get_path('module', 'visualization_entity_maps');
$element['#attached']['css'][] = $module_path . '/css/ve_map.css';
$element['#attached']['css'][] = $module_path . '/css/ve_map_edit.css';
}
/**
* Implements hook_entity_view().
*/
function visualization_entity_maps_entity_view($entity, $type, $view_mode, $langcode) {
if ($type == 'visualization' && $entity->type == 've_map') {
if(visualization_entity_resource_accesible($entity, 'field_ve_map_uuid_resource')){
$entity->content['#suffix'] = '<div id="map"></div>';
visualization_entity_maps_attach_libraries($entity->content);
$entity->content['#attached']['css'][] = drupal_get_path('module', 'visualization_entity_maps') . '/css/ve_map.css';
drupal_add_js(drupal_get_path('module', 'visualization_entity_maps') . '/js/visualization_entity_maps_view.js', array('weight' => 101));
drupal_add_js(array('visualizationEntityMaps' => array('leafletPath' => libraries_get_path('leaflet', TRUE))), 'setting');
} else {
drupal_access_denied();
}
}
}
/**
* Implements hook_theme().
*/
function visualization_entity_maps_theme($existing, $type, $theme, $path) {
return array(
'eck__entity__form_add_visualization_ve_map' => array(
'render element' => 'form',
'template' => 've-map-form',
'path' => drupal_get_path('module', 'visualization_entity_maps') . '/templates',
),
'eck__entity__form_edit_visualization_ve_map' => array(
'render element' => 'form',
'template' => 've-map-form',
'path' => drupal_get_path('module', 'visualization_entity_maps') . '/templates',
),
);
}
/**
* Preprocess for map add form.
*/
function template_preprocess_eck__entity__form_add_visualization_ve_map(&$vars) {
$form = $vars['form'];
$form = visualization_entity_maps_attach_libraries($form);
$module_path = drupal_get_path('module', 'visualization_entity_maps');
drupal_add_js($module_path . '/js/visualization_entity_maps_steps.js', array('weight' => 100));
drupal_add_js($module_path . '/js/visualization_entity_maps.js', array('weight' => 102));
unset($vars['form']['actions']);
unset($vars['form']['submit']);
}
/**
* Preprocess for map edit form.
*/
function template_preprocess_eck__entity__form_edit_visualization_ve_map(&$vars) {
$form = $vars['form'];
$form = visualization_entity_maps_attach_libraries($form);
$module_path = drupal_get_path('module', 'visualization_entity_maps');
drupal_add_js($module_path . '/js/visualization_entity_maps_steps.js', array('weight' => 100));
drupal_add_js($module_path . '/js/visualization_entity_maps.js', array('weight' => 102));
unset($vars['form']['actions']);
unset($vars['form']['submit']);
}
/**
* Implements hook_update_N().
*/
function visualization_entity_maps_update_7001() {
// If field_uuid_resource doesn't exists I have to create it.
if(!$field = field_info_instance('visualization', 'field_uuid_resource', 've_map')) {
$instance = array(
'bundle' => 've_map',
'default_value' => NULL,
'deleted' => 0,
'description' => '',
'display' => array(
'default' => array(
'label' => 'above',
'module' => 'uuidreference',
'settings' => array(
'link' => FALSE,
),
'type' => 'uuidreference_label',
'weight' => 2,
),
'teaser' => array(
'label' => 'above',
'settings' => array(),
'type' => 'hidden',
'weight' => 0,
),
),
'entity_type' => 'visualization',
'field_name' => 'field_uuid_resource',
'label' => 'Existing resource',
'required' => 0,
'settings' => array(
'user_register_form' => FALSE,
),
'widget' => array(
'active' => 0,
'module' => 'uuidreference',
'settings' => array(
'autocomplete_path' => 'uuidreference/autocomplete',
),
'type' => 'uuidreference_autocomplete',
'weight' => 2,
),
);
field_create_instance($instance);
}
// If field_ve_map_uuid_resource exists then I copy the data and delete the old field.
if ($instance = field_info_instance('visualization', 'field_ve_map_uuid_resource', 've_map')) {
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'visualization')
->entityCondition('bundle', 've_map');
$result = $query->execute();
if (isset($result['visualization'])) {
$maps_ids = array_keys($result['visualization']);
$maps = entity_load('visualization', $maps_ids);
}
foreach ($maps as $map) {
// We move the value from one to another field.
$map->field_uuid_resource = $map->field_ve_map_uuid_resource;
$map->save();
}
field_delete_instance($instance);
}
}