You are here

function i18n_book_navigation_block_view in Book translation 7.2

Implements hook_block_view().

Port from the book module block. This function contains much of the same logic as the original book_block_view(), except for the additions of the translation logic.

See also

book_block_view()

File

./i18n_book_navigation.module, line 110
Defines the Book translation module.

Code

function i18n_book_navigation_block_view($delta = '') {
  $tnode = menu_get_object();

  // Not on a node page.
  if (empty($tnode)) {
    return array();
  }
  $block = array();

  // Get the node in the original language.
  $node = i18n_book_navigation_get_original_node($tnode);
  if (!empty($node->book)) {
    $current_bid = $node->book['bid'];
  }
  else {
    $current_bid = 0;
  }
  if (variable_get('book_block_mode', 'all pages') == 'all pages') {
    $book_menus = array();
    foreach (i18n_book_navigation_get_books() as $bid => $book) {
      $tree = array();
      if ($bid == $current_bid) {
        $tree = i18n_book_navigation($node);
      }
      else {
        $book['in_active_trail'] = FALSE;

        // Check whether user can access the book link.
        $book_node = node_load($book['nid']);
        $book['access'] = node_access('view', $book_node);
        $tree[0]['link'] = $book;
        $tree[0]['below'] = FALSE;
        $tree = i18n_book_navigation_translate_tree($tree);
      }

      // Render.
      $book_menus[$bid] = menu_tree_output($tree);
    }
    $block['subject'] = t("i18n Book navigation");
    $book_menus['#theme'] = 'book_all_books_block';
    $block['content'] = $book_menus;
  }
  elseif ($current_bid) {

    // Disable the i18n node selection mode.
    $prev = i18n_select(FALSE);

    // Only display this block when the user is browsing a book.
    $title = db_select('node', 'n')
      ->fields('n', array(
      'title',
    ))
      ->condition('n.nid', $node->book['bid'])
      ->addTag('node_access')
      ->execute()
      ->fetchField();

    // Revert the i18n node selection mode.
    i18n_select($prev);

    // Only show the block if the user has view access for the top-level node.
    if ($title) {
      $tree = i18n_book_navigation($node);
      if (count($tree)) {

        // There should only be one element at the top level.
        $data = array_shift($tree);
        $block['subject'] = theme('book_title_link', array(
          'link' => $data['link'],
        ));
        $block['content'] = $data['below'] ? menu_tree_output($data['below']) : '';
      }
    }
  }
  return $block;
}