Page Title module: token issue with root pages

Drupal

Using the contrib module Page Title (currently at version 7.2) I entered the pattern for the content type "book page" as:

[node:book:root:title]: [current-page:title] | [site:name]

When on a book page other than the root this works great. For example, when visiting The Balkans -> Chronology -> SFOR it displays the HTML title correctly as The Balkans: SFOR | planken.org. The title for the root of the book however became [node:book:root:title]: The Balkans | planken.org, with the literal token. No idea if this is something I am not understanding or not, but I tried all of the various book related tokens, and it doesn't seem to make a difference for the books themselves.

In order to get it right, I added the function preprocess_html() to template.php. In it, I obtain the title, do a few checks, and strip out the raw token if it occurs
at the beginning of the title string.

<?php function YOUR_THEME_preprocess_html(&$vars) {

  if (
module_exists('page_title')) {
   
// Check for presence of token delimiters
   
$pos1 = strripos($vars['head_title'], '[', 0);
   
$pos2 = strripos($vars['head_title'], ']', -1);
   
// Both delimiters were found, alter title
   
if ($pos1 !== false && $pos2 !== false) {
     
$new_title = substr($vars['head_title'], $pos2+1);
     
// Strip unwanted stuff
     
if (strpos('|:/-', substr($new_title, 0, 1)) !== false) {
       
$new_title = substr($new_title, 1);
      }
     
$new_title = ltrim(rtrim($new_title));
     
$vars['head_title'] = $new_title; // Voila
   
}
  }

}
?>

I tend to separate the parts for the title with ":", hence the code under "strip unwanted stuff".

Until I find a better solution, I will work with this.

(I have page_title 7.x-2.7 on Drupal 7.23.)

Update September 21, 2013

It resembles this issue: [node:book:parent]not convert on the first node book parent. And also this one (although that was for 6.x): If the token value is null, then the token name will be shown on page title directly..

The second post mentioned a patch that changes the function page_title_page_get_title. In page_title.module version 7.x-2.7 the function starts at line #599.

Existing:

<?php
   
// Apply token patterns using token_replace
   
$title = token_replace($page_title_pattern, $types, array('sanitize' => FALSE));
?>

After the proposed patch this becomes:

<?php
   
// Apply token patterns using token_replace
   
$title = token_replace($page_title_pattern, $types, array('sanitize' => FALSE, 'clear' => TRUE));
?>

I can confirm that this additional parameter does indeed clear the empty token. However, the title still may need cleaning up if you added any additional characters between the root and other parts, like I did.

Without my preprocess_html override, my root title would otherwise look like : The Balkans | planken.org.