Community Documentation

Sections Module: Section based on book chapter

Last updated March 19, 2012. Created by Magilla on January 4, 2011.
Edited by bfr. Log in to edit this page.

This snippet goes in Administer -> Site Building -> Sections -> <section> -> Page Specific Visibility Settings, and allows you to specify a section based on the book chapter name. The sectioning applies to the selected chapter and all children.

Prerequisite: Sections Module

Change $chapter_name to the exact name of the book chapter that you want to be the start of the new section.
Change $chapter_level to the chapter level to check at. Level 0 may work, but is untested.

How the levels are counted by this snippet:

Book Title (level 0)
-- Chapter 1 (level 1)
---- Chapter 1.1 (level 2)
------ Chapter 1.1.1 (level 3)
------ Chapter 1.1.2 (level 3)
------ Chapter 1.1.3 (level 3)
---- Chapter 1.2 (level 2)
------ Chapter 1.2.1 (level 3)
------ Chapter 1.2.2 (level 3)
-- Chapter 2 (level 1)
---- Chapter 2.1 (level 2)
------ Chapter 2.1.1 (level 3)
------ Chapter 2.1.2 (level 3)
------ Chapter 2.1.3 (level 3)
---- Chapter 2.2 (level 2)
------ Chapter 2.2.1 (level 3)
------ Chapter 2.2.2 (level 3)

Why? Well, I wanted to set different themes for different book chapters, but any module that uses the Sections Module should be able to work with this.

<?php
$chapter_name = "Chapter 1"; $chapter_level = 1;
$match = FALSE;
if (arg(0) == 'node' && is_numeric(arg(1))) {
  $nid  = arg(1);
  $node = node_load(array('nid' => $nid));
  $type = $node->type;
  if ($type == 'book') {
    $book_array = $node->book;
    $book_id    = $book_array["bid"];
    $mlid       = $book_array["mlid"];
    $temp_toc   = book_toc($book_id, array(), 1000);
    while (key($temp_toc) != $mlid) {
      next($temp_toc);
      }

    $tmp_string = current($temp_toc);
    while ($tmp_string[$chapter_level * 2] != " " && $tmp_string !== FALSE) {
      $tmp_string = prev($temp_toc);
      }

    if ($tmp_string == str_repeat("--", $chapter_level) . " " . $chapter_name) {
      $match = TRUE;
    }
  }
}
return $match;
nobody click here