Say we set the page title pattern as this:

[page-title] | [term] | [vocab] | [site-name]

If there's no vocab/term for a given page, the page title will be generated as this:

My title | | | My site

There are 3 separators in a row. Page title module should have an option to clear no needed separators. For example, for the previous url, we would have:

My title | My site

Comments

asb’s picture

Version: 6.x-2.0 » 6.x-2.x-dev
Category: bug » feature

This is not a bug, but a feature request. I'd love to see something like this also, but I have no idea how the UI could look.

Clearly the 'Page Title' module must not remove separator characters like "|", "-", ":" (or whatever the user has chosen as separator, it might be even a text string) at it's own discretion; so probably it'd be necessary to segment the title into tokens and it's belonging-to separator characters, maybe with a syntax like {[page-title] | }{[term] | }{[vocab] | }{[site-name]} (if {...} is empty, do not display), or a extended UI.

Greetings, -asb

jonaswouters’s picture

+1

nicholasThompson’s picture

Title: Clear unnecessary separators in the url » Optional Page Title Parameters
Version: 6.x-2.x-dev » 7.x-2.x-dev

Now THAT is a nice idea! Not sure what the overhead would be of breaking up a string and parsing each segment individually would be...

It could also be hard to detect... {[page-title] and then} some other {[term:name]} text... if page-title was empty, the optional segment would not be empty (it'd be { and then}).

I wonder if this would be a better suited feature request for Token. Then Token could do the replaces using a regex callback... Something like "if you find a token inside curly braces, pass the result into a function. This function returns blank if the replacement string for the token is empty, otherwise it does a normal string replacement on the token"...

komlenic’s picture

Status: Active » Closed (duplicate)

I wonder if this would be a better suited feature request for Token.

Conditional Tokens, or Token Logic have apparently been discussed in various places before. See #354039: Token logic module patch It seems unlikely to happen.

This idea may have merit to be a possible clue to a solution: http://drupal.org/node/279491#comment-1214029

I'm marking this issue as a duplicate of #876880: Conditional display of token value with surrounding characters / Optional Tokens, even though the descriptions and ideas here are very good.

komlenic’s picture

I solved the duplicate title separator problem by adding this to a custom module:

/**
 * Strips multiple, leading, and trailing separators from page titles.
 *
 * @param $title string The page title.
 * @param $separators array An array of page title separators.  
 * @return array The page title with any multiple, leading, or trailing
 *   separators removed.
 */ 
function mymodule_title_trim($title, $separators = array('|','-')) {
  foreach ($separators as $char) {
    $special_chars = array ('[', '\\', '^', '$', '.', '|', '?', '*', '+', '(', ')', '{', '}');
    
    if (in_array($char, $special_chars)) {
      $escaped_char = '\\' . $char;
    }
    else {
      $escaped_char = $char;
    }
    //reduce duplicated separators to one
    $title = preg_replace('/((\s)?' . $escaped_char . '(\s)?){2,}/', ' ' . $char . ' ', trim($title));
    //eliminate any separators that occur at string beginning
    $title = preg_replace('/^' . $escaped_char . '(.*)/', '$1', trim($title)); 
    //eliminate any separators that occur at string end
    $title = preg_replace('/(.*)' . $escaped_char . '$/', '$1', trim($title)); 
  }
  return $title;
}  

/**
 * Implements hook_preprocess_html().
 */
function mymodule_preprocess_html(&$vars) {
  $vars['head_title'] = mymodule_title_trim($vars['head_title']);
}