node_book_menu.inc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. if (module_exists('book')) {
  3. /**
  4. * Plugins are described by creating a $plugin array which will be used
  5. * by the system that includes this file.
  6. */
  7. $plugin = array(
  8. 'single' => TRUE,
  9. 'title' => t('Book navigation menu'),
  10. 'icon' => '../block/icon_core_block_menu.png',
  11. 'description' => t('The book menu belonging to the current book node.'),
  12. 'required context' => new ctools_context_required(t('Node'), 'node'),
  13. 'category' => t('Node'),
  14. );
  15. }
  16. function ctools_node_book_menu_content_type_render($subtype, $conf, $panel_args, $context) {
  17. $node = isset($context->data) ? clone($context->data) : NULL;
  18. $block = new stdClass();
  19. $block->module = 'book_menu';
  20. if ($conf['override_title']) {
  21. $block->title = t($conf['override_title_text']);
  22. }
  23. else {
  24. $block->title = t('Book navigation menu');
  25. }
  26. if ($node) {
  27. $block->delta = $node->nid;
  28. // TODO: the value is not available somehow?!?
  29. $book_block_mode = isset($conf['book_block_mode']) ? $conf['book_block_mode'] : 'book pages';
  30. // Code below is taken from function book_block_view().
  31. $current_bid = empty($node->book['bid']) ? 0 : $node->book['bid'];
  32. if ($book_block_mode === 'all pages') {
  33. $block->subject = t('Book navigation');
  34. $book_menus = array();
  35. $pseudo_tree = array(0 => array('below' => FALSE));
  36. foreach (book_get_books() as $book_id => $book) {
  37. if ($book['bid'] === $current_bid) {
  38. // If the current page is a node associated with a book, the menu
  39. // needs to be retrieved.
  40. $book_menus[$book_id] = menu_tree_output(menu_tree_all_data($node->book['menu_name'], $node->book));
  41. }
  42. else {
  43. // Since we know we will only display a link to the top node, there
  44. // is no reason to run an additional menu tree query for each book.
  45. $book['in_active_trail'] = FALSE;
  46. // Check whether user can access the book link.
  47. $book_node = node_load($book['nid']);
  48. $book['access'] = node_access('view', $book_node);
  49. $pseudo_tree[0]['link'] = $book;
  50. $book_menus[$book_id] = menu_tree_output($pseudo_tree);
  51. }
  52. }
  53. $book_menus['#theme'] = 'book_all_books_block';
  54. $block->content = $book_menus;
  55. }
  56. elseif ($current_bid) {
  57. // Only display this block when the user is browsing a book.
  58. $select = db_select('node', 'n')
  59. ->fields('n', array('title'))
  60. ->condition('n.nid', $node->book['bid'])
  61. ->addTag('node_access');
  62. $title = $select->execute()->fetchField();
  63. // Only show the block if the user has view access for the top-level node.
  64. if ($title) {
  65. $tree = menu_tree_all_data($node->book['menu_name'], $node->book);
  66. // There should only be one element at the top level.
  67. $data = array_shift($tree);
  68. // TODO: subject is not rendered
  69. $block->subject = theme('book_title_link', array('link' => $data['link']));
  70. $block->content = ($data['below']) ? menu_tree_output($data['below']) : '';
  71. }
  72. }
  73. }
  74. else {
  75. $block->content = t('Book navigation pager goes here.');
  76. $block->delta = 'unknown';
  77. }
  78. return $block;
  79. }
  80. function ctools_node_book_menu_content_type_admin_title($subtype, $conf, $context) {
  81. return t('"@s" book navigation pager', array('@s' => $context->identifier));
  82. }
  83. function ctools_node_book_menu_content_type_edit_form($form, &$form_state) {
  84. // Grab block form from the book module.
  85. $block_form = book_block_configure($delta = '');
  86. // TODO: this does not work yet.
  87. // See TODO in: ctools_node_book_menu_content_type_render
  88. if (isset($form_state['input']['book_block_mode'])) {
  89. $block_form['book_block_mode']['#default_value'] = $form_state['input']['book_block_mode'];
  90. }
  91. $form += $block_form;
  92. return $form;
  93. }