You are here

bookblock.module in Book Block 7

Same filename and directory in other branches
  1. 6 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.
 */

/**
 * Debug.
 */
define('BOOKBLOCK_DEBUG', FALSE);

/**
 * Implements 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;
}

/**
 * Implements hook_block_info().
 */
function bookblock_block_info() {
  $block = array();
  $books = book_get_books();
  $navigationblocks = variable_get('bookblock_books', array());
  if (BOOKBLOCK_DEBUG) {
    watchdog('bookblock', '$navigationblocks = ' . implode(',', $navigationblocks));
  }
  foreach ($navigationblocks as $navigationblock) {
    if (isset($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;
}

/**
 * Implements hook_block_view().
 */
function bookblock_block_view($delta = '') {
  $block = array();
  $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();
  $bid = NULL;
  if (isset($node_info->book['bid'])) {
    $bid = $node_info->book['bid'];
  }
  if (!$node_info || $bid != $delta || !$expansion) {

    // Pull in the minimum we need to get book info and node access working.
    $node_info = db_query('SELECT n.nid, n.uid, n.status, n.type FROM {node} n WHERE n.nid = :nid', array(
      ':nid' => $delta,
    ))
      ->fetchObject();
    $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);
      _bookblock_fix_tree($tree);
    }

    // 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']) {
      $tree = menu_tree_all_data($node_info->book['menu_name'], $node_info->book);
      $data = array_shift($tree);
    }
    $block['subject'] = theme('book_title_link', array(
      'link' => $data['link'],
    ));
    if ($data['below']) {
      $block['content'] = menu_tree_output($data['below']);
    }
  }
  return $block;
}

/**
 * Implements hook_block_configure().
 */
function bookblock_block_configure($delta) {
  $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;
}

/**
 * Implements hook_block_save().
 */
function bookblock_block_save($delta, $edit) {
  variable_set('bookblock_block_options_' . $delta, $edit['bookblock_block_options']);
}

/**
 * Implements hook_help().
 */
function bookblock_help($path, $arg) {
  switch ($path) {
    case 'admin/content/book/blocks':
      $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/structure/block'),
      )) . '</p>';
      $output .= '<p><em>' . t('N.B. Only books with child pages will actually display anything!') . '</em></p>';
      return $output;
    case 'admin/help#bookblock':
      $output = '<h4>' . t('About') . '</h4>';
      $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. 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/structure/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;
  }
}

/**
 * Loads the minimum information from the database needed to created a book menu.
 *
 * @return
 *   
 */
function bookblock_book_info_load($nid) {
  if ($book = db_query('SELECT * FROM {book} b INNER JOIN {menu_links} ml ON b.mlid = ml.mlid WHERE b.nid = :nid', array(
    ':nid' => $nid,
  ))
    ->fetchAssoc()) {
    $book['href'] = $book['link_path'];
    $book['title'] = $book['link_title'];
    $book['options'] = unserialize($book['options']);
    return $book;
  }
}

/**
 * comment
 */
function _bookblock_fix_tree(&$tree) {

  // dpm($tree);
}

Functions

Namesort descending Description
bookblock_block_configure Implements hook_block_configure().
bookblock_block_info Implements hook_block_info().
bookblock_block_save Implements hook_block_save().
bookblock_block_view Implements hook_block_view().
bookblock_book_info_load Loads the minimum information from the database needed to created a book menu.
bookblock_help Implements hook_help().
bookblock_menu Implements hook_menu().
_bookblock_fix_tree comment

Constants

Namesort descending Description
BOOKBLOCK_DEBUG Debug.