-
Notifications
You must be signed in to change notification settings - Fork 4
/
bookoblock.module
95 lines (86 loc) · 2.72 KB
/
bookoblock.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
<?php
/**
* @file
* Provides a single block that displays the current book's ToC when viewing a
* menu item that is part of a book.
*/
/**
* Implements hook_block_info().
*
* Define the Book Outline Block.
*/
function bookoblock_block_info() {
$block['book_outline'] = array(
'info' => 'Book Outline',
'cache' => DRUPAL_CACHE_PER_PAGE,
);
return $block;
}
/**
* Implements hook_block_view().
*
* Returns a block array with the current book's outline as the body and the top
* level book as the title if the menu item viewed is a node and is part of a
* book. NULL if the current menu item is not part of a book heirarchy.
*/
function bookoblock_block_view() {
if ($book = bookoblock_is_book_node()) {
// menu_build_tree() doesn't accept zero for depth, so we convert that to
// NULL and add 1 if it's not 0 to account for the first (skipped) level.
$max_depth = variable_get('bookoblock_depth', NULL);
$max_depth = ($max_depth == 0) ? NULL : ($max_depth + 1);
// Vars and params for the menu_build_tree() function.
$path = 'node/' . $book['bid'];
$parent = menu_link_get_preferred($path, $book['menu_name']);
$parameters = array(
'only_active_trail' => FALSE,
'min_depth' => $parent['depth'] + 1,
'max_depth' => $max_depth,
);
// Build the tree and block title.
$children = menu_build_tree($parent['menu_name'], $parameters);
$book_name = (book_toc($book['bid'], 1));
// Build and return the $block array.
$block['subject'] = l($book_name[$book['p1']], 'node/' . $book['bid']);
$block['content'] = menu_tree_output($children);
return $block;
}
// If the current node isn't part of a book, just return nothing.
return NULL;
}
/**
* Implements hook_block_configure().
*
* Returns configuration form with depth select list.
*/
function bookoblock_block_configure() {
$form['bookoblock_depth'] = array(
'#type' => 'select',
'#options' => drupal_map_assoc(array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)),
'#title' => t('Book Outline Block Depth'),
'#description' => t('Select the maximum depth of the Book Outline Block. Select "0" for no limit.'),
'#default_value' => variable_get('bookoblock_depth', 0),
);
return $form;
}
/**
* Implements hook_block_save().
*
* Saves the depth value for the block.
*/
function bookoblock_block_save($delta = '', $edit = array()) {
variable_set('bookoblock_depth', $edit['bookoblock_depth']);
}
/**
* Checks to see if current node is part of a book.
*
* @return array
* book portion of the node array if it is part of a book, FALSE if not.
*/
function bookoblock_is_book_node() {
$node = menu_get_object();
if ($node && isset($node->book)) {
return $node->book;
}
return FALSE;
}