Remove Table of Contents from Drupal Book

Continuing on from the project I just mentioned...I wanted to remove the list of pages (which is analogous to a table of contents) from the book pages. On this particular site I'm using the book block on the right hand side, so having this information on the bottom of top level page makes for duplication of links and just seems plain funny. Below is the relevant diff -u on the subject:

Edit: This is actually a really bad idea. See the comments below for the right way to do it for Drupal 6.

@@ -469,9 +469,9 @@
   if ($node->nid) {
     $output .= '

<

div class="book">';

  • /*if ($tree = book_tree($node->nid)) {
  • if ($tree = book_tree($node->nid)) { $output .= '
'. $tree .'

'; - }*/ + }

 if ($prev = book_prev($node)) {
   $links .= '<div class="prev">';

how does this work?

I'm having trouble trying to do the same thing on a Drupal site I'm developing. I'd like to remove the Table of Contents that is always generated.

I was glad to find this page, but I can't seem to figure out how to implement the code above you've suggested. I quite a beginner in these things and could really use some more specific instructions.

I've been trying to make adjustments to the book.module file, based on your code, but I don't seem to be able to get it right.

Any advice would be greatly appreciated.

Thanks!

really bad idea

greggles's picture

This is actually a really bad idea. I'm not sure how to remove the table of contents without hacking the book module, but this is the wrong way to do it, I assure you :(

remove book links

Warning! This is at own risk... (It worked for me. For now...)

(for drupal 6.1)

  1. Go into the dedicated modules: .../drupal/modules/book
  2. Find book-navigation.tpl.php and make a backup in the same folder (book-navigation.tpl.php-backup)
  3. Open the file and remove all this:
    <?php if ($tree || $has_links): ?>

    " class="book-navigation">
    <?php print $tree; ?>

    <?php if ($has_links): ?>

    <?php endif; ?>

    <?php endif; ?>

  4. Replace the original book-navigation.tpl.php with your book-navigation.tpl.php
  5. Look if all is still functioning ok and that obnoxious navigation is gone
    1. If not: replace the file with you backup.
    2. If yes: be happy, and hope it keeps functioning

If this turns out to be not a very smart thing to do, please notify me! ;)

close to right...here's the "Drupal way" to do it

greggles's picture

This is almost the right answer. Basically (as my warning above states) what you want to do is not edit the original code, but use Drupal's override system. When you edit the original code then you have to be sure to re-apply the change whenever you update Drupal (and you are going to update Drupal when Drupal6.2 is release right...

So...by using a theme override, you can apply this in a way which will still work after you've upgraded your site.

  1. Go into the book module directory drupal/modules/book
  2. Copy the file book-navigation.tpl.php into your theme directory (your theme should be something like sites/all/themes/theme_name/
  3. Edit the new book-navigation.tpl.php and remove the part that says <?php print $tree; ?>
  4. Because of the new theme registry, this change won't be visible immediately, so you must visit example.com/admin/settings/performance and go to the bottom of the page where you click the button for "Clear cached data"
  5. Should be done :)

Hmmm... yeey thanks...

Yeey. It worked. I thus restored the original file in the drupal module and used the theme override. Thank you for the advice! (And do update your blogposts, when you find a solution for a problem, it helps the folks googling for the exact same problem, and thus solution... ;))

Thanks again,
Vint

Removing Table of Contents from Drupal 5 Book

To implement this in Drupal5, you need to override the "theme_book_navigation" method in your template.php file. Copy this from "book.module", and replace "theme" with your theme name as per usual.

To remove the tree, simply comment out this line:
$tree = book_tree($node->nid);

There is however one gotcha. Drupal's link function strips out the text of the navigation links because it doesn't like the arrow symbols this uses. Simply change them to "<" and ">" and it should work without a problem.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Allowed HTML tags: <br> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <strike> <h3> <pre> <blockquote> <h2> <div> <p>
  • Lines and paragraphs break automatically.