Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
If you wish to display a block only while showing a page from level 1 or 2 of a specific book (note in Drupal 6 this code does not work below that level), try the following:
- Go to the block you want to edit and click configure
- Go to the "Page specific visibility settings"
- Choose the option: "Show if the following PHP code returns TRUE"
- Paste in the code below
- Change the two values of 46 to the node ID of the book you're testing against
Then the block should only display if you're inside that particular book.
Drupal 5
if (arg(0) == "node" && is_numeric(arg(1))) {
if (46 == arg(1)) {
return TRUE;
}
$result = db_fetch_object(db_query("SELECT parent FROM {book} WHERE nid = %d", arg(1)));
if (46 == $result->parent) {
return TRUE;
} else {
return FALSE;
}
} else {
return FALSE;
}
Drupal 6
In Drupal 6 the column 'parent' is now BID (book ID).
if (arg(0) == "node" && is_numeric(arg(1))) {
if (46 == arg(1)) {
return TRUE;
}
$result = db_fetch_object(db_query("SELECT bid FROM {book} WHERE nid = %d", arg(1)));
if (46 == $result->bid) {
return TRUE;
}
else {
return FALSE;
}
}
else {
return FALSE;
}
'46' is the nid of your book and needs to be changed to the nid of the specific book you are attaching this block to.
These snippets are from help provided in this forum thread: http://drupal.org/node/125344. Thanks to all who helped!
Comments
I have modified this script
I have modified this script to support more than 2 levels of book hierarchy.
Note, you need to replace 46 with your own book id (the most top level book node).
This code works in Drupal 6.
Half-way there
This works when the book_id is the top level book node. Your work on this is really appreciated.
However, there are two issues for me, which make this code pretty unusable:
1. when I try to open nodes that are outside the book, they take a very long time (a minute or so). After I delete the block containing the code, the all nodes load at normal speed; and
2. when the book_id is not a top level node, all children nodes from the node id entered don't render at all. Whereas the D5 version of this PHP code did work if the id was not the top level. I'd love it if the D6 version could start at any level of the book, like D5 did.
For stop recoursive when the loop arrive to top of another book
How can I show a block on any
How can I show a block on any page containing to any book?
So, just check if the current page belongs to a book.