You are here

bookblock.module in Book Block 6

Same filename and directory in other branches
  1. 7 bookblock.module

Enables users to add specific book navigation blocks on non-book pages.

File

bookblock.module
View source
<?php

/**
 * @file
 * Enables users to add specific book navigation blocks on non-book pages.
 */

/**
 * Implementation of hook_menu().
 */
function bookblock_menu() {
  $items['admin/content/book/blocks'] = array(
    'title' => 'Book Blocks',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'bookblock_admin_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 10,
    'file' => 'bookblock.admin.inc',
  );
  return $items;
}

/**
 * Implementation of hook_block().
 */
function bookblock_block($op = 'list', $delta = 0, $edit = array()) {
  $block = array();
  $books = book_get_books();
  $navigationblocks = variable_get('bookblock_books', array());
  switch ($op) {
    case 'list':
      foreach ($navigationblocks as $navigationblock) {
        if ($books[$navigationblock]) {
          $block[$navigationblock]['info'] = t('Book block: @title', array(
            '@title' => $books[$navigationblock]['title'],
          ));
          $block[$navigationblock]['cache'] = BLOCK_CACHE_PER_PAGE | BLOCK_CACHE_PER_ROLE;
        }
      }
      return $block;
    case 'view':
      if ($delta) {
        $expansion = variable_get('bookblock_block_options_' . $delta, TRUE);

        // See if we can use the current node to grab the book information.
        // Call it $node_info as it may not contain everything a node would.
        $node_info = menu_get_object();
        if (!$node_info || $node_info->book['bid'] != $delta || !$expansion) {

          // Pull in the minimum we need to get book info and node access working.
          $node_info = db_fetch_object(db_query('SELECT n.nid, n.uid, n.status FROM {node} n WHERE n.nid = %d', $delta));
          $node_info->book = bookblock_book_info_load($delta);
        }

        // Now see if we can render something.
        if (node_access('view', $node_info) && isset($node_info->book)) {
          if ($expansion) {
            $tree = menu_tree_page_data($node_info->book['menu_name']);
          }
          else {

            // The problem with this is there will be no active trail.
            // I have a feeling something custom is going to have to be written.
            $tree = menu_tree_all_data($node_info->book['menu_name'], $node_info->book);
          }

          // There should only be one element at the top level.
          $data = array_shift($tree);

          // Check we've got some data if we've used menu_tree_page_data.
          // Doesn't appear to have been an opportunity to do this before.
          if ($expansion && !$data['below']) {
            $data = array_shift(menu_tree_all_data($node_info->book['menu_name'], $node_info->book));
          }
          $block['subject'] = theme('book_title_link', $data['link']);
          if ($data['below']) {
            $block['content'] = menu_tree_output($data['below']);
          }
        }
      }
      return $block;
    case 'configure':
      $form['bookblock_block_options'] = array(
        '#type' => 'checkbox',
        '#title' => t('Allow menu to expand'),
        '#default_value' => variable_get('bookblock_block_options_' . $delta, TRUE),
        '#description' => t("If checked, the menu will expand to show child pages as the user navigates through the book. Otherwise it will only show the first level of links. You may not want the menu to expand in the footer, for example."),
      );
      return $form;
    case 'save':
      variable_set('bookblock_block_options_' . $delta, $edit['bookblock_block_options']);
      break;
  }
}

/**
 * Implementation of hook_help().
 */
function bookblock_help($path, $arg) {
  switch ($path) {
    case 'admin/content/book/blocks':
      $output = '<p>' . t('The core book module provides one <em>book navigation block</em>, which is very useful, but occasionally you may need a book navigation block on specific non-book pages or in an additional region (e.g. in the footer).') . '</p>';
      $output .= '<h4>' . t('Configuration') . '</h4>';
      $output .= '<p>' . t('The books you have created on your site will be listed below. Select which ones you would like to create a <em>book navigation block</em> for and they will then be available to you on the <a href="@admin-block">blocks administration page</a>, where you can control on which pages they appear and in which region.', array(
        '@admin-block' => url('admin/build/block'),
      )) . '</p>';
      $output .= '<p>' . t('Each <em>book navigation block</em> can also be configured from the blocks administration page (click <em>configure</em> next to the block name). The title can be replaced and you can control whether the menu expands to reveal child pages or not.') . '</p>';
      $output .= '<p><em>' . t('N.B. Only books with child pages will actually display anything!') . '</em></p>';
      return $output;
  }
}

/**
 * comment
 */
function bookblock_book_info_load($nid) {
  $book = db_fetch_array(db_query('SELECT * FROM {book} b INNER JOIN {menu_links} ml ON b.mlid = ml.mlid WHERE b.nid = %d', $nid));
  if ($book) {
    $book['href'] = $book['link_path'];
    $book['title'] = $book['link_title'];
    $book['options'] = unserialize($book['options']);
    return $book;
  }
}

Functions

Namesort descending Description
bookblock_block Implementation of hook_block().
bookblock_book_info_load comment
bookblock_help Implementation of hook_help().
bookblock_menu Implementation of hook_menu().