Inherit the full url of the parent book page by using a Drupal token

Recently I created a book in Drupal using two Content Types. One type called Book Description (which has a cover, an ID and some other CCK fields) and another type called Book Page. So far so good. Problems arose when I tried to setup Pathauto. For the Book Description I entered book/[field_book_id-raw], which worked great. For the Book Pages I needed something like book/[field_book_id-raw]/title-of-my-page. This is where I got stuck.

The problem with this setup is that CCK fields of Book Description aren't known in the Book Pages. So saving a Book Page resulted in [field_book_id-raw] in the URL, instead of its value. The book module already comes with some tokens:

  • [book]: The title of the node's book parent.
  • [book_id]: The id of the node's book parent.
  • [bookpath]: The titles of all parents in the node's book hierarchy.
  • [book-raw]: The unfiltered title of the node's book parent. WARNING - raw user input.
  • [bookpath-raw]: The unfiltered titles of all parents in the node's book hierarchy. WARNING - raw user input.
  • [bookpathalias]: URL alias for the parent book.

I expected [bookpathalias] to work, but that resulted in something like book/title-of-the-book/title-of-the-page. None of the tokens did what I wanted. What I needed was a Token that uses the full path of its parent page. After some testing, I got it working using 2 Token hooks. 

/**
 * Implementation of hook_token_values().
 */
function CUSTOMMODULENAME_token_values($type, $object = NULL, $options = array()) {
  if ($type == 'node') {
    $node = $object;
    $tokens['bookurl-path'] = '';
 
  if (!empty($node->book['plid'])) {
    $parent = book_link_load($node->book['plid']);
    $tokens['bookurl-path'] = drupal_lookup_path('alias', $parent['href']);
  }
 
  return $tokens;
  }
}

/**
 * Implementation of hook_token_list().
 */
function
CUSTOMMODULENAME_token_list($type = 'all') {
  if ($type == 'node' || $type == 'all') {
    $tokens['book']['bookurl-path']      = t("The url of the parents node");
    return $tokens;
  }
}

This creates the token [bookurl-path]. Now I can use this token in my pathauto settings to get an url like book/12345/title-of-my-page.

wouldn't that pretty much

wouldn't that pretty much come down to book/[book_id]/[node-title-raw] or something?

oh my bad, just noticed ur

oh my bad, just noticed ur grabbing the alias of the parent, it just happens to be book/[bid]

Not exactly

The parent is using an ID from a CCK field to generate its URL. The problem with the existing tokens that it is not possible to use CCK fields of the parent page from within child URL's. So I have to do a lookup of the parent's pathalias in the database to use this as my base URL. So it's not book/[bid] but book/[my_special_CCK_ID]

Thanks

I am using the Book module for the first time, and this helped me resolve my breadcrumbs and URLs, so they accurately show where you are within a book. Thanks a lot.

Post new comment

The content of this field is kept private and will not be shown publicly.