? page_title.module_bak
Index: page_title.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/page_title/page_title.module,v
retrieving revision 1.18.2.27
diff -u -p -r1.18.2.27 page_title.module
--- page_title.module	23 Nov 2009 20:51:38 -0000	1.18.2.27
+++ page_title.module	18 Jan 2010 15:14:51 -0000
@@ -440,72 +440,108 @@ function page_title_page_get_title() {
       // Append the pattern for pages with a pager on them
       $page_title_pattern .= isset($_REQUEST['page']) ? variable_get('page_title_pager_pattern', '') : '';
 
-      // Allow hook_page_title_pattern_alter() to modify the pattern. In this case we can use drupal_alter as we have no tokens to alter.
+      // Allow hook_page_title_pattern_alter() to modify the pattern. In this
+      // case we can use drupal_alter as we have no tokens to alter.
       drupal_alter('page_title_pattern', $page_title_pattern);
 
-      // Apply the token patterns using the one-level replacer (frontpage is only "global" scope). Need to flush the token cache first.
+      // Apply the token patterns using the one-level replacer (frontpage is
+      // only "global" scope). Need to flush the token cache first.
       token_get_values('global', NULL, TRUE);
       $title = token_replace($page_title_pattern);
     }
+
     // Otherwise this is a non-frontpage page title.
-    else {
+    // Check for special cases:
+
+    // Special case: forum root - forums and forum containers are picked up
+    // as terms below.
+    elseif (arg(0) == 'forum' && !is_numeric(arg(1)) && module_exists('forum')) {
+      $title = variable_get('page_title_forum_root_title', '');
+    }
+    
+    // Other pages get processed as normal.
+    if (empty($title)) {
       // Initialize some variables we need
       $page_title_pattern = '';
       $types = array('global' => NULL);
+      $override = FALSE;
 
       // Determine scope
       // Node (either node or comment reply)
-      if ((arg(0) == 'node' && is_numeric(arg(1)))  ||  (arg(0) == 'comment' && arg(1) == 'reply' && is_numeric(arg(2)) && module_exists('comment')) ) {
+      if ((arg(0) == 'node' && is_numeric(arg(1))) || (arg(0) == 'comment' && arg(1) == 'reply' && is_numeric(arg(2)) && module_exists('comment'))) {
         $types['node'] = menu_get_object();
-        // If the node has any taxonomy, grab the first time and pass it over to be passed as a token.
-        // TODO: Handle multiple terms? Only pass specific terms per content type?
+        $type = 'term';
+        $id = $types['node']->nid;
+        // If the node has any taxonomy, grab the first time and pass it over
+        // to be passed as a token.
+        // TODO: Handle multiple terms? Only pass specific terms per content
+        // type?
         if (!empty($types['node']->taxonomy)) {
           $types['taxonomy'] = current($types['node']->taxonomy);
         }
         $page_title_pattern = variable_get('page_title_type_'. $types['node']->type, '');
+        $override = variable_get('page_title_type_'. $types['node']->type .'_showfield', FALSE);
+        // See if the node already has an overridden title.
+        if ($override && !empty($node->page_title)) {
+          $title = $node->page_title;
+        }
       }
-      // Term
-      elseif (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2)) && module_exists('taxonomy')) {
-        $types['taxonomy'] = taxonomy_get_term(arg(2));
+      // Term, or a forum or forum container, forum root is processed above.
+      elseif ((arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2)) && module_exists('taxonomy')) || (arg(0) == 'forum' && module_exists('forum') && is_numeric(arg(1)))) {
+        $type = 'term';
+        $id = arg(2);
+        $types['taxonomy'] = taxonomy_get_term($id);
         $page_title_pattern = variable_get('page_title_vocab_'. $types['taxonomy']->vid, '');
-      }
-      // Forum
-      elseif (arg(0) == 'forum' && module_exists('forum')) {
-        if (is_numeric(arg(1))) {
-          $types['taxonomy'] = taxonomy_get_term(arg(1));
-        }
-        $forum_vid = variable_get('forum_nav_vocabulary', '');
-        $page_title_pattern = variable_get('page_title_vocab_'. $forum_vid, '');
+        $override = variable_get('page_title_vocab_'. $types['taxonomy']->vid .'_showfield', FALSE);
       }
       // User
       elseif (arg(0) == 'user' && is_numeric(arg(1))) {
-        $types['user'] = user_load(array('uid' => arg(1)));
+        $type = 'user';
+        $id = arg(1);
+        $types['user'] = user_load(array('uid' => $id));
         $page_title_pattern = variable_get('page_title_user', '');
+        $override = variable_get('page_title_user_showfield', FALSE);
       }
       // Blog
-      elseif (arg(0) == 'blog' && is_numeric(arg(1))) {
+      elseif (arg(0) == 'blog' && module_exists('blog') && is_numeric(arg(1))) {
         $types['user'] = user_load(array('uid' => arg(1)));
         $page_title_pattern = variable_get('page_title_blog', '');
       }
 
-      // If pattern is emtpy (either if the type is not overridable or simply not set) fallback to the default pattern)
-      if (empty($page_title_pattern)) {
-        $page_title_pattern = variable_get('page_title_default', '[page-title] | [site-name]');
-      }
+      // In certain cases there might already be a page title at this point.
+      if (!empty($title)) {
+        // Do nothing.
+      }
+      // If this type can be overridden, see if a title was previously saved.
+      elseif (!empty($type) && $override && $page_title = page_title_load_title($id, $type)) {
+        // We have the page title so don't need to do anything else.
+        $title = $page_title;
+      }
+      // Otherwise load & process the page pattern.
+      else {
+        // If pattern is emtpy (either if the type is not overridable or
+        // simply not set) fallback to the default pattern)
+        if (empty($page_title_pattern)) {
+          $page_title_pattern = variable_get('page_title_default', '[page-title] | [site-name]');
+        }
 
-      // Append the pattern for pages with a pager on them
-      $page_title_pattern .= isset($_REQUEST['page']) ? variable_get('page_title_pager_pattern', '') : '';
+        // Append the pattern for pages with a pager on them
+        $page_title_pattern .= isset($_REQUEST['page']) ? variable_get('page_title_pager_pattern', '') : '';
 
-      // Allow hook_page_title_pattern_alter() to modify the pattern - we cant use drupal_alter as it only supports single arguments (or arrays). We need to pass 2 variables.
-      $data = array(&$page_title_pattern, &$types);
-      foreach (module_implements('page_title_pattern_alter') as $module) {
-        $function = $module .'_page_title_pattern_alter';
-        call_user_func_array($function, $data);
-      }
+        // Allow hook_page_title_pattern_alter() to modify the pattern - we
+        // can't use drupal_alter as it only supports single arguments (or
+        // arrays). We need to pass 2 variables.
+        $data = array(&$page_title_pattern, &$types);
+        foreach (module_implements('page_title_pattern_alter') as $module) {
+          $function = $module .'_page_title_pattern_alter';
+          call_user_func_array($function, $data);
+        }
 
-      // Apply token patterns by resetting the token cache first and then using token_replace_multiple to insert token values
-      token_get_values('global', NULL, TRUE);
-      $title = token_replace_multiple($page_title_pattern, $types);
+        // Apply token patterns by resetting the token cache first and then
+        // using token_replace_multiple to insert token values
+        token_get_values('global', NULL, TRUE);
+        $title = token_replace_multiple($page_title_pattern, $types);
+      }
     }
   }
 
