forked from ding2/ding_library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathding_library.module
346 lines (316 loc) · 10.9 KB
/
ding_library.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<?php
/**
* @file
* Code for the Ding library feature.
*/
/**
* Implements hook_init().
*/
function ding_library_init() {
// Load ding library map javascript code if we have loaded the library
// overview page.
if ($_GET['q'] == 'libraries') {
drupal_add_js(drupal_get_path('module', 'ding_library') . '/js/ding_library.map.js', array(
'defer' => TRUE,
'scope' => 'footer',
'weight' => 42,
));
}
/**
* @TODO: What does this next part do and why is it needed.
*/
$args = arg();
if (sizeof($args) == 2 && $args[0] == 'node' && $node = menu_get_object()) {
$types = array(
'ding_event' => 'events',
'ding_news' => 'news',
);
if (isset($types[$node->type])) {
// Check if node is part of a group. Get all groups.
if ($groups = og_get_entity_groups('node', $node)) {
// Load the first.
$group = og_load(array_shift($groups));
// The entity_id the group references, is the nid of the library.
$library_nid = $group->etid;
// Use the nodes path.
$path = 'node/' . $library_nid . '/' . $types[$node->type];
}
else {
// Not part of any group, put them under the site list.
$path = $types[$node->type];
}
// Set the active path for the main menu.
menu_tree_set_path('main-menu', $path);
};
}
}
/**
* Implements hook_enable().
*/
function ding_library_enable() {
$path = array(
'source' => 'libraries',
'language' => 'da',
);
// Create Danish alias for libraries if needed.
if (!path_load($path)) {
$path['alias'] = 'biblioteker';
path_save($path);
}
}
/**
* Implements hook_block_info().
*/
function ding_library_block_info() {
return array(
'library_address' => array(
'info' => 'Library address',
'cache' => DRUPAL_CACHE_GLOBAL,
),
'ding_library_menu' => array(
'info' => 'Ding library menu',
'cache' => DRUPAL_CACHE_PER_PAGE,
),
'ding_sidebar_menu' => array(
'info' => 'Ding sidebar menu',
'cache' => DRUPAL_CACHE_PER_PAGE,
),
);
}
/**
* Implements hook_block_configure().
*/
function ding_library_block_configure($delta = '') {
$form = array();
$libraries = array(
'' => t('None selected'),
);
if ($delta == 'library_address') {
$query = db_select('node', 'n')
->fields('n', array('nid', 'title'))
->condition('type', 'ding_library')
->orderBy('title', 'ASC');
foreach ($query->execute() as $row) {
$libraries[$row->nid] = $row->title;
}
$form['library_nid'] = array(
'#type' => 'select',
'#title' => t('Library'),
'#default_value' => variable_get('ding_library_address_nid', NULL),
'#options' => $libraries,
'#description' => t('The library whose address to display.'),
);
}
return $form;
}
/**
* Implements hook_block_save().
*/
function ding_library_block_save($delta = '', $edit = array()) {
if ($delta == 'library_address') {
variable_set('ding_library_address_nid', $edit['library_nid']);
}
}
/**
* Implements hook_block_view().
*/
function ding_library_block_view($delta = '') {
$block = array();
$node = NULL;
if ($delta == 'library_address' && (($nid = variable_get('ding_library_address_nid', '')) && ($node = node_load($nid)) || user_access('administer blocks'))) {
$block['subject'] = t('Address');
$block['content'] = array(
'#theme' => 'ding_library_address',
'#node' => $node,
);
}
if ($delta == 'ding_library_menu') {
$trail = menu_get_active_trail();
// Get the active menu item.
$current_item = menu_get_item(menu_tree_get_path('main-menu'));
// Find the menu_links for the current item and the libraries menu item.
$links = db_select('menu_links', 'ml')
->fields('ml', array('link_path', 'mlid', 'plid', 'p1', 'p2', 'p3'))
->condition('menu_name', 'main-menu')
->condition('link_path', array('libraries', $current_item['href']))
->execute()->fetchAllAssoc('link_path');
// If p1 (first part of materialised path) is the mlid of the
// libraries item, the current item lies below it.
// Unless p2 is 0, then we're on the libraries page itself.
if (!empty($links['libraries']) && !empty($links[$current_item['href']]) && $links['libraries']->mlid == $links[$current_item['href']]->p1 && $links[$current_item['href']]->p2 != 0) {
// Find the library menu item, by using p2.
$library = db_select('menu_links', 'ml')
->fields('ml', array('mlid', 'link_path', 'link_title'))
->condition('mlid', $links[$current_item['href']]->p2)
->execute()->fetchAssoc();
// Add in the library as the first item of the list.
$links = array(
'menu-' . $library['mlid'] => array(
'href' => $library['link_path'],
'title' => t('Frontpage'),
),
);
// And add the library items children.
$links += menu_navigation_links('main-menu', 2);
// Theme the lot as a list of links.
$block['content']['library-menu'] = array(
'#markup' => theme('links__library_menu', array(
'links' => $links,
'heading' => array(
'text' => $library['link_title'],
'level' => 'h2',
),
)),
'#prefix' => '<div class="library-menu clearfix">',
'#suffix' => '</div>',
);
}
}
if ($delta == 'ding_sidebar_menu') {
$trail = menu_get_active_trail();
if (!empty($trail[1])) {
$tree = menu_tree_page_data($trail[1]['menu_name']);
$level = 1;
// If we have at least 3 levels in the trail and the second item is
// libraries, we're inside the library 'subsite'.
if (!empty($trail[1]) && !empty($trail[2]) && $trail[1]['router_path'] == 'libraries') {
$level = 3;
}
// Logic borrowed from menu_navigation_links();
while ($level-- && $tree) {
// Loop through the current level's items until we find one that is in trail.
while ($item = array_shift($tree)) {
if ($item['link']['in_active_trail']) {
// If the item is in the active trail, we continue in the subtree.
$tree = empty($item['below']) ? array() : $item['below'];
break;
}
}
}
// If we have a tree, render it.
if (!empty($tree)) {
$block['content'] = menu_tree_output($tree);
}
}
}
return $block;
}
/**
* Implements hook_theme().
*/
function ding_library_theme() {
return array(
'ding_library_address' => array(
'render element' => 'element',
'template' => 'ding-library-address'
),
);
}
/**
* Preprocessor.
*/
function template_preprocess_ding_library_address(&$vars) {
$node = $vars['element']['#node'];
// Ensure empty strings as default.
$vars += array(
'name' => '',
'address' => '',
'city' => '',
'phone' => '',
'fax' => '',
'mail' => '',
);
if ($node) {
$languages = field_language('node', $node);
$vars['name'] = check_plain($node->title);
if (!empty($node->field_ding_library_address[$languages['field_ding_library_address']][0]) && ($address = $node->field_ding_library_address[$languages['field_ding_library_address']][0])) {
$vars['address'] = check_plain($address['thoroughfare']);
if ($address['premise']) {
$vars['address'] .= ', ' . $address['premise'];
}
$vars['city'] = join(' ', array(check_plain($address['postal_code']), check_plain($address['locality'])));
}
if (!empty($node->field_phone[$languages['field_phone']][0]) &&
($phone = $node->field_phone[$languages['field_phone']][0]['safe_value'])) {
$vars['phone'] = t('Phone: @phone_number', array('@phone_number' => $phone));
}
if (!empty($node->field_email[$languages['field_email']][0]) &&
($mail = $node->field_email[$languages['field_email']][0])) {
$vars['mail'] = l($mail['email'], 'mailto:' . $mail['email']);
}
}
else {
$vars['name'] = t('Please select a library to display address information for at the <a href="!link">block configuration page</a>.', array('!link' => url('admin/structure/block/manage/ding_library/library_address/configure')));
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function ding_library_form_ding_library_node_form_alter(&$form, &$form_state) {
if (isset($form['field_ding_library_slug'][$form['#node']->language])) {
$form['field_ding_library_slug'][$form['#node']->language][0]['#element_validate'][] = 'ding_library_slug_validate';
}
elseif (isset($form['field_ding_library_slug']['und'])) {
$form['field_ding_library_slug']['und'][0]['#element_validate'][] = 'ding_library_slug_validate';
}
}
/**
* Implements hook_ctools_plugin_directory -
*/
function ding_library_ctools_plugin_directory($owner, $plugin_type) {
if ($owner == 'ctools' && $plugin_type == 'content_types') {
return 'plugins/content_types';
}
}
/**
* Custom validate handler for the slug field.
*
* Don't allow charecters that aren't URL friendly, such as # , etc.
*/
function ding_library_slug_validate(&$form, &$form_state) {
// Try to get the slug value.
if (isset($form_state['values']['field_ding_library_slug'][$form_state['node']->language])) {
$slug = $form_state['values']['field_ding_library_slug'][$form_state['node']->language][0]['value'];
}
elseif (isset($form_state['values']['field_ding_library_slug']['und'])) {
$slug = $form_state['values']['field_ding_library_slug']['und'][0]['value'];
}
// If we found the slug do the actual validation.
if (!empty($slug)) {
if (!(preg_match('/^[a-z0-9_\-]+$/', $slug))) {
form_set_error(implode('][', $form['#parents']), t('Illeagal charecters detected, only small letters (a-z), numbers (0-9), dashes (-) and underscores (_) allowed'));
}
}
}
/**
* Implements hook_views_data_alter().
*/
function ding_library_views_data_alter(&$data) {
foreach (field_info_fields() as $name => $field) {
$name = 'field_data_' . $name;
if ($field['type'] == 'group') {
foreach ($data[$name] as $column => $views_data) {
if (strpos($column, 'gid') !== FALSE) {
$data[$name]['ding_library_nid'] = $views_data;
unset($data[$name]['ding_library_nid']['filter']);
unset($data[$name]['ding_library_nid']['sort']);
$data[$name]['ding_library_nid']['title'] = str_replace('gid', 'nid', $data[$name]['ding_library_nid']['title']);
$data[$name]['ding_library_nid']['title short'] = str_replace('gid', 'nid', $data[$name]['ding_library_nid']['title short']);
$data[$name]['ding_library_nid']['argument']['handler'] = 'ding_library_nid_to_gid_argument';
}
}
}
}
}
/**
* Implements hook_ding_library_menu_links().
*/
function ding_library_menu_ding_library_menu_links() {
return array(
'about' => array(
'title' => 'About the library',
'weight' => 11,
),
);
}
include_once('ding_library.features.inc');