What has been changed in Book module in Drupal 6
In this article I will tell you the recent changes for Book module. Knowing about that will help you to adapt snippets and modules which work with Book module for working in Drupal 6.
Solution.
Drupal 5.х
In Drupal 5.x, once a book page is changed, a record of ‘book’ type (nid) is added to the “node” table. This record defines relation to a parent page: if parent is set to 0, the book is on top. This is so simple!
All module data were stored in one table:
CREATE TABLE book (
vid int unsigned NOT NULL default ‘0’,
nid int unsigned NOT NULL default ‘0’,
parent int NOT NULL default ‘0’,
weight tinyint NOT NULL default ‘0’,
PRIMARY KEY (vid),
KEY nid (nid),
KEY parent (parent)
)
Drupal 6.х
In Drupal 6.x, “book” table contains nid, bid and mlid fields only.
As you can see from the Drupal interface, you can’t specify a parent page on node/add/book page. Instead, you should choose the appropriate book and (IMPORTANT!) parent page in this book.
Book module has been changed to use new Drupal 6.x menu system ({menu_links} table) for storing and recovering of book hierarchy. Any modules which used Book interface have to be modified. All info stored in a node with Book module is now stored in $node->book property.
Many API functions of Book module have been changed. For example, book_recurse function was deleted. For the most cases, it could be replaced with book_export_traverse, but it doesn’t have $depth parameter.
Analysis of database tables
— Structure of `book` table
CREATE TABLE `book` (
`mlid` int(10) unsigned NOT NULL default ‘0’,
`nid` int(10) unsigned NOT NULL default ‘0’,
`bid` int(10) unsigned NOT NULL default ‘0’,
PRIMARY KEY (`mlid`),
UNIQUE KEY `nid` (`nid`),
KEY `bid` (`bid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
— Structure of `menu_links` table
CREATE TABLE `menu_links` (
`menu_name` varchar(32) NOT NULL default ”,
`mlid` int(10) unsigned NOT NULL auto_increment,
`plid` int(10) unsigned NOT NULL default ‘0’,
`link_path` varchar(255) NOT NULL default ”,
`router_path` varchar(255) NOT NULL default ”,
`link_title` varchar(255) NOT NULL default ”,
`options` text,
`module` varchar(255) NOT NULL default ‘system’,
`hidden` smallint(6) NOT NULL default ‘0’,
`external` smallint(6) NOT NULL default ‘0’,
`has_children` smallint(6) NOT NULL default ‘0’,
`expanded` smallint(6) NOT NULL default ‘0’,
`weight` int(11) NOT NULL default ‘0’,
`depth` smallint(6) NOT NULL default ‘0’,
`customized` smallint(6) NOT NULL default ‘0’,
`p1` int(10) unsigned NOT NULL default ‘0’,
`p2` int(10) unsigned NOT NULL default ‘0’,
`p3` int(10) unsigned NOT NULL default ‘0’,
`p4` int(10) unsigned NOT NULL default ‘0’,
`p5` int(10) unsigned NOT NULL default ‘0’,
`p6` int(10) unsigned NOT NULL default ‘0’,
`p7` int(10) unsigned NOT NULL default ‘0’,
`p8` int(10) unsigned NOT NULL default ‘0’,
`p9` int(10) unsigned NOT NULL default ‘0’,
`updated` smallint(6) NOT NULL default ‘0’,
PRIMARY KEY (`mlid`),
KEY `path_menu` (`link_path`(128),`menu_name`),
KEY `menu_plid_expand_child` (`menu_name`,`plid`,`expanded`,`has_children`),
KEY `menu_parents` (`menu_name`,`p1`,`p2`,`p3`,`p4`,`p5`,`p6`,`p7`,`p8`,`p9`),
KEY `router_path` (`router_path`(128))
) ENGINE=MyISAM AUTO_INCREMENT=407 DEFAULT CHARSET=utf8 AUTO_INCREMENT=407 ;
Analysis of $node->book
Let’s create a simple node of Book type and implement the following code there (input format have to be PHP):
if ((arg(0) == ‘node’) && is_numeric(arg(1))) {
$node = node_load(arg(1));
var_dump($node->book);
}
Here is the result:
array(29) {
[“mlid”]=> string(3) “409”
[“nid”]=> string(2) “27”
[“bid”]=> string(2) “56”
[“menu_name”]=> string(11) “book-toc-56″
[“plid”]=> string(3) “257”
[“link_path”]=> string(7) “node/27″
[“router_path”]=> string(6) “node/%”
[“link_title”]=> string(20) “Content”
[“options”]=> array(0) {}
[“module”]=> string(4) “book”
[“hidden”]=> string(1) “0”
[“external”]=> string(1) “0”
[“has_children”]=> string(1) “0”
[“expanded”]=> string(1) “0”
[“weight”]=> string(1) “0”
[“depth”]=> string(1) “2”
[“customized”]=> string(1) “0”
[“p1″]=> string(3) “257”
[“p2″]=> string(3) “409”
[“p3″]=> string(1) “0”
[“p4″]=> string(1) “0”
[“p5″]=> string(1) “0”
[“p6″]=> string(1) “0”
[“p7″]=> string(1) “0”
[“p8″]=> string(1) “0”
[“p9″]=> string(1) “0”
[“updated”]=> string(1) “0”
[“href”]=> string(7) “node/27″
[“title”]=> string(20) “Content”
}
Summary
- book table in Друпал 6.х includes bid (Book id) field that stores nid of a page which is on top in a book
- Every book page also has mlid (Menu links id) field to be connected with menu_links table. This is requried due to page hierarchy is now stored in menu_links table instead of book table.
- Maximum inclusion is limited to 9 levels
Elements of $node->book array
- mlid – id menu_links, allows Book module connect to menu_links
- nid – node id
- bid – abbreviation of “book_id”
- menu_name – for Book nodes, it always starts with “book-toc-” + nid of a top node. menu_name is one for all nodes in a book.
- plid – mlid of a parent (0 – top level)
- link_path – address of a node in a menu
- router_path
- link_title – link text in menu
- options array(0) {}
- module – system module name. Is stored in menu_links.module
- hidden – Hidden menu element
- external
- has_children – if there are child nodes (0 – no, 1 – yes)
- expanded – expanded (0- no, 1 – yes)
- weight – weight (for sorting of one-level elements)
- depth – incapsulation depth (1 – top level)
- customized
- p1 – top level in menu hierarchy. Includes parent mlid
- p2 – hierarchy level. Includes mlid of a current node in our case.
- p3 – 0 – if there are no child nodes.
- p4
- p5
- p6
- p7
- p8
- p9
- updated
- href – node address
- title – node title
Good luck!