-
Notifications
You must be signed in to change notification settings - Fork 0
/
dandelion.module
361 lines (311 loc) · 8.27 KB
/
dandelion.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
<?php
/**
* @file
* Dandelion
*
* @author Hein Bekker <hein@netbek.co.za>
* @copyright (c) 2015 Hein Bekker
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPLv2
*/
/**
* Implements hook_menu().
*/
function dandelion_menu() {
$items = array();
$path = drupal_get_path('module', 'dandelion');
$items['admin/config/dandelion'] = array(
'title' => 'Dandelion',
'description' => 'Configure Dandelion.',
'page callback' => 'drupal_get_form',
'page arguments' => array('dandelion_admin_settings'),
'access arguments' => array('administer dandelion'),
'type' => MENU_NORMAL_ITEM,
'file' => 'dandelion.admin.inc',
'file path' => $path,
);
$items['admin/config/dandelion/settings'] = array(
'title' => 'Settings',
'description' => 'Configure Dandelion settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array('dandelion_admin_settings'),
'access arguments' => array('administer dandelion'),
'type' => MENU_DEFAULT_LOCAL_TASK,
'file' => 'dandelion.admin.inc',
'file path' => $path,
);
return $items;
}
/**
* Implements hook_menu_alter().
*/
function dandelion_menu_alter(&$items) {
// Remove RSS feed from head.
$items['rss.xml']['page callback'] = 'drupal_not_found';
}
/**
* Implements hook_html_head_alter().
*/
function dandelion_html_head_alter(&$head_elements) {
foreach (array_keys($head_elements) as $key) {
// Remove RSS feed from head.
if (strstr($key, 'rss.xml')) {
unset($head_elements[$key]);
}
}
}
/**
* Implements hook_permission().
*/
function dandelion_permission() {
return array(
'administer dandelion' => array(
'title' => t('Administer Dandelion'),
'description' => t('Allows users to change the Dandelion settings.'),
),
);
}
/**
* Implements hook_block_info().
*/
function dandelion_block_info() {
$blocks = array();
$blocks['notes_menu'] = array(
'info' => t('Notes menu'),
'cache' => DRUPAL_NO_CACHE,
);
$blocks['notes_admin'] = array(
'info' => t('Notes admin menu'),
'cache' => DRUPAL_NO_CACHE,
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function dandelion_block_view($delta = '') {
$block = array();
$menu = NULL;
$admin_menu = NULL;
$alias = 'notes';
$path = drupal_lookup_path('source', $alias);
$node = menu_get_object('node', 1, $path);
if (!empty($node)) {
$tree = menu_tree_all_data($node->book['menu_name']);
menu_tree_add_active_path($tree);
$menu = menu_tree_output($tree);
$links = array();
$child_type = variable_get('book_child_type', 'book');
if (user_access('add content to books') && node_access('create', $child_type) && $node->status == 1 && $node->book['depth'] < MENU_MAX_DEPTH) {
$links['book_add_child'] = array(
'title' => t('Add a page'),
'href' => 'node/add/' . str_replace('_', '-', $child_type),
'query' => array('parent' => $node->book['mlid']),
);
}
if (user_access('administer book outlines')) {
$links['book_outline'] = array(
'title' => t('Re-order pages'),
'href' => 'admin/content/' . str_replace('_', '-', $child_type) . '/' . $node->nid,
);
}
if (user_access('access printer-friendly version')) {
$links['book_printer'] = array(
'title' => t('Printable version'),
'href' => 'book/export/html/' . $node->nid,
);
}
$admin_menu = array(
'#theme' => 'links__node__book',
'#links' => $links,
'#attributes' => array('class' => array('links')),
);
}
switch ($delta) {
case 'notes_menu':
$block['subject'] = t('Notes menu');
$block['content'] = $menu;
break;
case 'notes_admin':
$block['subject'] = t('Notes admin menu');
$block['content'] = $admin_menu;
break;
}
return $block;
}
/**
* Implements hook_form_alter().
*/
function dandelion_form_alter(&$form, &$form_state, $form_id) {
// Set height of CKEditor instances.
if (module_exists('ckeditor')) {
$instances = array(
array(
'form_id' => 'comment_node_book_form',
'field_name' => 'comment_body',
'height' => 200,
),
array(
'form_id' => 'book_node_form',
'field_name' => 'body',
'height' => 500,
),
array(
'form_id' => 'page_node_form',
'field_name' => 'body',
'height' => 500,
),
);
foreach ($instances as $instance) {
if ($form_id == $instance['form_id']) {
if (isset($form_state['field'][$instance['field_name']])) {
$field_language = $form[$instance['field_name']]['#language'];
$id = 'edit-' . $instance['field_name'] . '-' . $field_language . '-0-value';
$id = str_replace('_', '-', $id);
drupal_add_js(array('ckeditor' => array('instanceConfig' => array($id => array('height' => $instance['height'] . 'px')))), 'setting');
}
break;
}
}
}
}
/**
* Implements hook_node_view().
*/
function dandelion_node_view($node, $view_mode, $langcode) {
// If "print" view, then remove unnecessary node links.
if ($view_mode == 'print') {
if (isset($node->content['links'])) {
unset($node->content['links']);
}
}
}
/**
* Implements hook_preprocess_panels_pane().
*/
function dandelion_preprocess_panels_pane(&$variables) {
// Build "last updated" message.
if ($variables['pane']->type == 'node_updated') {
$variables['content'] = t('Updated: !datetime', array(
'!datetime' => $variables['content'],
));
}
}
/**
* Implements hook_image_default_styles().
*/
function dandelion_image_default_styles() {
$styles = array();
// Scale
$sizes = array(
400,
800,
1000,
1200,
1600,
2000,
2400
);
foreach ($sizes as $size) {
$styles['s_' . $size] = array(
'label' => $size . ' (' . $size . ' width or height)',
'effects' => array(
array(
'name' => 'image_scale',
'data' => array('width' => $size, 'height' => $size, 'upscale' => 0),
'weight' => 0,
),
)
);
}
$styles['og_facebook'] = array(
'label' => 'Open Graph: Facebook (1200x630)',
'effects' => array(
array(
'name' => 'image_scale_and_crop',
'data' => array('width' => 1200, 'height' => 630),
'weight' => 0,
),
)
);
$styles['og_twitter'] = array(
'label' => 'Open Graph: Twitter (480x250)',
'effects' => array(
array(
'name' => 'image_scale_and_crop',
'data' => array('width' => 480, 'height' => 250),
'weight' => 0,
),
)
);
return $styles;
}
/**
*
* @param int $visibility
* @param string $pages
* @param string $path Path to check. If empty, then current path is checked.
* @return boolean
*/
function dandelion_check_path_visibility($visibility, $pages, $path = NULL) {
if (empty($pages)) {
return FALSE;
}
if ($path === NULL) {
$path = $_GET['q'];
}
$pages = drupal_strtolower($pages);
$alias = drupal_strtolower(drupal_get_path_alias($path));
$match = drupal_match_path($alias, $pages);
if ($alias != $path) {
$match = $match || drupal_match_path($path, $pages);
}
return !($visibility xor $match);
}
/**
* Returns timestamp of last change to a book and its child pages.
*
* @param int $nid
* @return boolean|int
*/
function dandelion_get_book_changed($nid) {
$node = node_load($nid);
if (empty($node) || empty($node->book)) {
return FALSE;
}
$changed = 0;
$tree = menu_tree_all_data($node->book['menu_name']);
$links = dandelion_book_menu_links($tree);
$nids = array();
foreach ($links as $link) {
preg_match('#node/([0-9]+)#i', $link['link_path'], $matches);
if (!empty($matches)) {
$nids[] = $matches[1];
}
}
$nodes = node_load_multiple($nids);
if (empty($nodes)) {
return FALSE;
}
foreach ($nodes as $node) {
if ($node->changed > $changed) {
$changed = $node->changed;
}
}
return $changed;
}
/**
* Returns a flat array of menu links.
*
* @param array $tree
* @return array
*/
function dandelion_book_menu_links($tree) {
$links = array();
foreach ($tree as $node) {
$links[$node['link']['mlid']] = $node['link'];
if (!empty($node['below'])) {
$links += dandelion_book_menu_links($node['below']);
}
}
return $links;
}