Projet

Général

Profil

Paste
Télécharger (3,85 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / plugins / content_types / node_context / node_book_menu.inc @ c304a780

1
<?php
2

    
3
/**
4
 * @file
5
 */
6

    
7
if (module_exists('book')) {
8
  /**
9
   * Plugins are described by creating a $plugin array which will be used
10
   * by the system that includes this file.
11
   */
12
  $plugin = array(
13
    'single' => TRUE,
14
    'title' => t('Book navigation menu'),
15
    'icon' => drupal_get_path('module', 'ctools') . '/plugins/content_types/block/icon_core_block_menu.png',
16
    'description' => t('The book menu belonging to the current book node.'),
17
    'required context' => new ctools_context_required(t('Node'), 'node'),
18
    'category' => t('Node'),
19
  );
20
}
21

    
22
function ctools_node_book_menu_content_type_render($subtype, $conf, $panel_args, $context) {
23
  $node = isset($context->data) ? clone $context->data : NULL;
24
  $block = new stdClass();
25
  $block->module = 'book_menu';
26

    
27
  if ($conf['override_title']) {
28
    $block->title = t($conf['override_title_text']);
29
  }
30
  else {
31
    $block->title = t('Book navigation menu');
32
  }
33
  if ($node) {
34
    $block->delta = $node->nid;
35
    // TODO: the value is not available somehow?!?
36
    $book_block_mode = isset($conf['book_block_mode']) ? $conf['book_block_mode'] : 'book pages';
37

    
38
    // Code below is taken from function book_block_view().
39
    $current_bid = empty($node->book['bid']) ? 0 : $node->book['bid'];
40

    
41
    if ($book_block_mode === 'all pages') {
42
      $block->subject = t('Book navigation');
43
      $book_menus = array();
44
      $pseudo_tree = array(0 => array('below' => FALSE));
45
      foreach (book_get_books() as $book_id => $book) {
46
        if ($book['bid'] === $current_bid) {
47
          // If the current page is a node associated with a book, the menu
48
          // needs to be retrieved.
49
          $book_menus[$book_id] = menu_tree_output(menu_tree_all_data($node->book['menu_name'], $node->book));
50
        }
51
        else {
52
          // Since we know we will only display a link to the top node, there
53
          // is no reason to run an additional menu tree query for each book.
54
          $book['in_active_trail'] = FALSE;
55
          // Check whether user can access the book link.
56
          $book_node = node_load($book['nid']);
57
          $book['access'] = node_access('view', $book_node);
58
          $pseudo_tree[0]['link'] = $book;
59
          $book_menus[$book_id] = menu_tree_output($pseudo_tree);
60
        }
61
      }
62
      $book_menus['#theme'] = 'book_all_books_block';
63
      $block->content = $book_menus;
64
    }
65
    elseif ($current_bid) {
66
      // Only display this block when the user is browsing a book.
67
      $select = db_select('node', 'n')
68
        ->fields('n', array('title'))
69
        ->condition('n.nid', $node->book['bid'])
70
        ->addTag('node_access');
71
      $title = $select->execute()->fetchField();
72
      // Only show the block if the user has view access for the top-level node.
73
      if ($title) {
74
        $tree = menu_tree_all_data($node->book['menu_name'], $node->book);
75
        // There should only be one element at the top level.
76
        $data = array_shift($tree);
77
        // TODO: subject is not rendered.
78
        $block->subject = theme('book_title_link', array('link' => $data['link']));
79
        $block->content = ($data['below']) ? menu_tree_output($data['below']) : '';
80
      }
81
    }
82
  }
83
  else {
84
    $block->content = t('Book navigation pager goes here.');
85
    $block->delta = 'unknown';
86
  }
87

    
88
  return $block;
89
}
90

    
91
function ctools_node_book_menu_content_type_admin_title($subtype, $conf, $context) {
92
  return t('"@s" book navigation pager', array('@s' => $context->identifier));
93
}
94

    
95
function ctools_node_book_menu_content_type_edit_form($form, &$form_state) {
96
  // Grab block form from the book module.
97
  $block_form = book_block_configure($delta = '');
98
  // TODO: this does not work yet.
99
  //       See TODO in: ctools_node_book_menu_content_type_render.
100
  if (isset($form_state['input']['book_block_mode'])) {
101
    $block_form['book_block_mode']['#default_value'] = $form_state['input']['book_block_mode'];
102
  }
103
  $form += $block_form;
104
  return $form;
105
}