-
Notifications
You must be signed in to change notification settings - Fork 9
/
ding_library.module
389 lines (341 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
<?php
/**
* @file
* Code for the Ding library feature.
*/
include_once 'ding_library.features.inc';
/**
* Implements hook_menu_alter().
*
* Rewrite the main menus library link to redirect to a single library if only
* one library exists.
*
* Only called after a cache clear or menu rebuild.
*/
function ding_library_menu_alter(&$items) {
$query = db_select('node', 'n')
->fields('n', array('nid'))
->condition('status', 1)
->condition('type', 'ding_library');
// Get the number of libraries.
$count = $query->countQuery()
->execute()
->fetchField();
// If only one library exists try redirect to that library.
if ($count == 1) {
// Find the library's slug.
$query->join('field_data_field_ding_library_slug', 'ls', 'n.nid = ls.entity_id');
$query->addField('ls', 'field_ding_library_slug_value', 'slug');
$data = $query->execute()->fetchAssoc();
if (isset($items['libraries'])) {
// If the menu item "libraries" exists use a menu callback to redirect the
// user. The slug have been loaded above to ensure that the SQL is only
// run once, namely after a cache clear.
$items['libraries']['page callback'] = 'ding_library_single_library_redirect';
$items['libraries']['page arguments'] = array($data['slug']);
}
}
}
/**
* Redirect to a library based on slug value.
*
* Used be the menu alter in this module to handle the case where only one
* library exists on the site.
*
* @param string $slug
* String value that represents the library's slug.
*/
function ding_library_single_library_redirect($slug) {
// Send Moved Permanently - 301 header with the redirect.
drupal_goto('bibliotek/' . $slug, array(), 301);
}
/**
* Implements hook_block_info().
*/
function ding_library_block_info() {
return array(
'library_address' => array(
'info' => 'Library address',
'cache' => DRUPAL_CACHE_GLOBAL,
),
);
}
/**
* Implements hook_block_configure().
*/
function ding_library_block_configure($delta = '') {
$form = array();
if ($delta == 'library_address') {
$query = db_select('node', 'n')
->fields('n', array('nid', 'title'))
->condition('type', 'ding_library')
->orderBy('title', 'ASC');
$libraries = array(
'' => t('None selected'),
);
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,
);
}
return $block;
}
/**
* Implements hook_theme().
*/
function ding_library_theme() {
return array(
'ding_library_address' => array(
'render element' => 'element',
'template' => 'ding-library-address',
),
);
}
/**
* Implements hokk_preprocess_ding_library_address().
*
* Default template implementation.
*/
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'] = implode(' ', 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_ctools_plugin_directory().
*/
function ding_library_ctools_plugin_directory($owner, $plugin_type) {
return 'plugins/' . $plugin_type;
}
/**
* 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';
}
}
/**
* Custom validate handler for the slug field.
*
* Don't allow charecters that are not 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))) {
// @TODO use url filter function for this.
form_set_error(implode('][', $form['#parents']), t('Illeagal charecters detected, only small letters (a-z), numbers (0-9), dashes (-) and underscores (_) allowed'));
}
}
}
/**
* Implements hook_og_context_negotiation_info().
*
* Enabled ding library to find group context panel pages where the group node
* is loaded as argument. E.g. bibliotek/%slug/nyheder.
*/
function ding_library_og_context_negotiation_info() {
$providers = array();
$providers['ding-library-panels'] = array(
'name' => t('Ding library'),
'description' => t("Determine context in panels node."),
'callback' => 'ding_library_og_context_handler',
);
return $providers;
}
/**
* Callback for OG context negotiation that tries to find ding group node.
*
* This is based on the current menu item (path) and that it is a panel.
*
* @return array
* Node ids.
*/
function ding_library_og_context_handler() {
$nids = array();
$item = menu_get_item();
if (isset($item['page_arguments'][1]->data->type) && $item['page_arguments'][1]->data->type == 'ding_library') {
$nids[] = $item['page_arguments'][1]->data->nid;
}
return array(
'node' => $nids,
);
}
/**
* Implements hook_post_features_revert().
*/
function ding_library_post_features_revert() {
ding_library_install_menu_position('revert');
}
/**
* Implements hook_post_features_disable_feature().
*/
function ding_library_post_features_disable_feature() {
ding_library_install_menu_position('delete');
}
/**
* Implements hook_post_features_enable_feature().
*/
function ding_library_post_features_enable_feature() {
ding_library_install_menu_position('install');
}
/**
* Helper function to install menu position rule.
*/
function ding_library_install_menu_position($op = 'install') {
module_load_include('inc', 'menu_position', 'menu_position.admin');
// Check if rule exists.
$exists = db_select('menu_position_rules', 'mpr')
->fields('mpr', array('rid'))
->condition('admin_title', 'Libraries')
->execute()
->fetchField();
if ($exists && $op == 'revert') {
// The rule exists, so we delete it.
menu_position_delete_rule($exists);
// Activate installation of the rule.
$exists = FALSE;
}
if (!$exists && ($op == 'install' || $op == 'revert')) {
// Define conditions.
$conditions = array(
'content_type' => array(
'content_type' => array(
'ding_group' => 'ding_library',
),
),
);
// Find the parent element.
$plid = db_select('menu_links', 'ml')
->fields('ml', array('mlid'))
->condition('link_path', 'libraries', '=')
->execute()->fetchField();
// Add the rule.
if ($plid) {
menu_position_add_rule(array(
'admin_title' => 'Libraries',
'conditions' => $conditions,
'menu_name' => 'main-menu',
'plid' => $plid,
));
}
else {
watchdog('ding_library', 'Unable to create menu position rule for ding library', array(), WATCHDOG_WARNING);
}
}
if ($exists && $op == 'delete') {
// The rule exists, so we delete it.
menu_position_delete_rule($exists);
}
}
/**
* Implements hook_node_insert().
*/
function ding_library_node_insert($node) {
if ($node->type == 'ding_library') {
// Get front page queue.
$queue = nodequeue_load_queue_by_name('ding_library_listing');
// Load sub-queue.
$sub_queue = nodequeue_load_subqueues_by_queue($queue->qid);
$sub_queue = reset($sub_queue);
// Add the node to the queue.
nodequeue_subqueue_add($queue, $sub_queue, $node->nid);
}
}
/**
* Implements hook_node_update().
*/
function ding_library_node_update($node) {
if ($node->type == 'ding_library') {
// Update OG_menu title if group title is changed.
if ($node->title != $node->original->title) {
$menu = menu_load('menu-og-' . $node->nid);
$menu['title'] = $node->title;
$menu['description'] = 'OG Menu for ' . $node->title;
menu_save($menu);
}
}
}
/**
* Implements hook_node_delete().
*/
function ding_library_node_delete($node) {
if ($node->type == 'ding_library') {
// Get front page queue.
$queue = nodequeue_load_queue_by_name('ding_library_listing');
// Load sub-queue.
$sub_queue = nodequeue_load_subqueues_by_queue($queue->qid);
$sub_queue = reset($sub_queue);
// Remove the node to the queue.
nodequeue_subqueue_remove_node($sub_queue->sqid, $node->nid);
}
}