| [ Index ] |
PHP Cross Reference of Drupal 6 (gatewave) |
[Summary view] [Print] [Text view]
1 <?php 2 // $Id: page_title.module,v 1.18.2.27 2009/11/23 20:51:38 njt1982 Exp $ 3 4 /** 5 * @file 6 * Enhanced control over the page title (in the head tag). 7 * 8 * This module gives you control over the page title. It gives you the chance 9 * to provide patterns for how the title should be structured, and on node 10 * pages, gives you the chance to specify the page title rather than defaulting 11 * to the node title. 12 */ 13 14 15 /** 16 * Implementation of hook_help(). 17 */ 18 function page_title_help($path, $arg) { 19 $output = NULL; 20 switch ($path) { 21 case 'admin/content/page_title': 22 $output = '<p>'. t('Page Title provides control over the <title> element on a page using token patterns and an optional textfield to override the title of the item (be it a node, term, user or other). The Token Scope column lets you know which tokens are available for this field (Global is always available). Please click on the <strong><em>more help…</em></strong> link below if you need further assistance.') .'</p>'; 23 break; 24 case 'admin/help#page_title': 25 $output = '<p>'. t('Drupal\'s default page title follows one of two patterns:') .'</p>'; 26 $items = array( 27 t('<strong>Default Page</strong>: <samp><em>page title</em> | <em>site name</em></samp>'), 28 t('<strong>Default Frontpage</strong>: <samp><em>site name</em> | <em>site slogan</em></samp>'), 29 ); 30 $output .= theme('item_list', $items, NULL, 'ol'); 31 $output .= '<p>'. t('The <strong>Page Title</strong> module lets you change these defaults in two ways. First, you can adjust the patterns below using the placeholders given. This will change the way the default page titles are created. Second, on enabled forms (curently node, term & user editing forms) you have the option of specifying a title that is different to the title of the item. This field only appears if the <em>Show Field</em> box is checked for the item. If a value is provided it will be used to generate the <samp>[page-title]</samp> placeholder however if it is left blank the <samp>[page-title]</samp> token will inherit the item\'s own title.') .'</p>'; 32 $output .= '<p>'. t('The <samp>[page-title]</samp> token will default to the value returned from <samp>drupal_get_title</samp> if there is no value specified or no available page title field.') .'</p>'; 33 $output .= '<p>'. t('Certain types of page title pattern have access to special tokens which others do not, depending on their <em>scope</em>. All patterns have access to the <strong>Global</strong> scope. Content type patterns have access to the <strong>Node</strong> tokens, vocabulary patterns have access to the <strong>Taxonomy</strong> tokens and finally the user patterns have access to the <strong>User</strong> tokens.') .'</p>'; 34 break; 35 } 36 return $output; 37 } 38 39 40 /** 41 * Implementation of hook_perm(). 42 */ 43 function page_title_perm() { 44 return array('set page title', 'administer page titles'); 45 } 46 47 48 /** 49 * Implementation of hook_menu(). 50 */ 51 function page_title_menu() { 52 $items = array(); 53 54 $items['admin/content/page_title'] = array( 55 'title' => 'Page titles', 56 'description' => 'Enhanced control over the page titles (in the <head> tag).', 57 'page callback' => 'drupal_get_form', 58 'page arguments' => array('page_title_admin_settings'), 59 'access callback' => 'user_access', 60 'access arguments' => array('administer page titles'), 61 'type' => MENU_NORMAL_ITEM, 62 'file' => 'page_title.admin.inc', 63 ); 64 65 return $items; 66 } 67 68 69 /** 70 * Implementation of hook_theme(). 71 */ 72 function page_title_theme() { 73 return array( 74 'page_title_admin_settings' => array( 75 'template' => 'page_title-admin-settings-form', 76 'arguments' => array('form' => NULL), 77 ), 78 79 'page_title_preprocess_page' => array( 80 'arguments' => array('vars' => NULL), 81 ), 82 ); 83 } 84 85 86 /** 87 * Implementation of hook_node_type(). 88 * 89 * Updates settings after a node type change. 90 */ 91 function page_title_node_type($op, $info) { 92 // Handle a content type rename 93 if ($op == 'update' && !empty($info->old_type) && $info->type != $info->old_type) { 94 // Load the old node type settings. 95 $temp = variable_get('page_title_type_'. $info->old_type, ''); 96 97 // If the settings aren't empty, then save them into the new type 98 if (!empty($temp)) { 99 variable_set('page_title_type_'. $info->type, $temp); 100 } 101 102 // Delete the old setting 103 variable_del('page_title_type_'. $info->old_type); 104 105 // Essentially, do the same as above but with the _showfield suffix for the node type 106 $temp = variable_get('page_title_type_'. $info->old_type .'_showfield', 0); 107 if ($temp) { 108 variable_set('page_title_type_'. $info->type .'_showfield', $temp); 109 } 110 variable_del('page_title_type_'. $info->old_type .'_showfield'); 111 112 } 113 114 // If deleted, remove the variables 115 if ($op == 'delete') { 116 variable_del('page_title_type_'. $info->type); 117 variable_del('page_title_type_'. $info->type .'_showfield'); 118 } 119 } 120 121 122 /** 123 * Implementation of hook_form_alter(). 124 */ 125 function page_title_form_alter(&$form, $form_state, $form_id) { 126 // If we dont have permission to set the title then we need to abort this alter now! 127 if (!user_access('set page title')) return; 128 129 // Check we're editing a node and also check that the node type's 'show field' is enabled 130 if ($form['#id'] == 'node-form') { 131 $key = 'page_title_type_'. $form['type']['#value'] .'_showfield'; 132 if (variable_get($key, 0)) { 133 $form['page_title'] = array( 134 '#type' => 'textfield', 135 '#title' => t('Page title'), 136 '#description' => t('Provide a description of this node to appear in the <title> tag which search engines can use in search result listings (optional). It is generally accepted this field should be less than 70 characters.'), 137 '#default_value' => $form['#node']->page_title, 138 '#size' => 60, 139 '#maxlength' => 255, 140 '#weight' => -4, 141 ); 142 } 143 } 144 // Check we're editing a taxonomy term and also check that the terms vocabulary's 'show field' is enabled 145 elseif ($form_id == 'taxonomy_form_term') { 146 $key = 'page_title_vocab_'. $form['vid']['#value'] .'_showfield'; 147 if (variable_get($key, 0)) { 148 $form['advanced']['page_title'] = array( 149 '#type' => 'textfield', 150 '#title' => t('Page title'), 151 '#description' => t('Provide a description of this term to appear in the <title> tag which search engines can use in search result listings (optional). It is generally accepted this field should be less than 70 characters.'), 152 '#default_value' => page_title_load_title($form['tid']['#value'], 'term'), 153 '#size' => 60, 154 '#maxlength' => 255, 155 '#weight' => -20, 156 ); 157 } 158 } 159 // Check we're editing a forum container or forum "forum" and also check that the terms vocabulary's 'show field' is enabled 160 elseif ($form_id == 'forum_form_forum' || $form_id == 'forum_form_container') { 161 $key = 'page_title_vocab_'. $form['vid']['#value'] .'_showfield'; 162 if (variable_get($key, 0)) { 163 $form['page_title'] = array( 164 '#type' => 'textfield', 165 '#title' => t('Page title'), 166 '#description' => t('Provide a description of this forum to appear in the <title> tag which search engines can use in search result listings (optional). It is generally accepted this field should be less than 70 characters.'), 167 '#default_value' => page_title_load_title($form['tid']['#value'], 'term'), 168 '#size' => 60, 169 '#maxlength' => 255, 170 '#weight' => -20, 171 ); 172 } 173 } 174 // Check we're editing a user profile and also check that the user settings's have 'show field' enabled 175 elseif ($form_id == 'user_profile_form') { 176 if (variable_get('page_title_user_showfield', 0)) { 177 $form['account']['page_title'] = array( 178 '#type' => 'textfield', 179 '#title' => t('Page title'), 180 '#description' => t('Provide a description of this user to appear in the <title> tag which search engines can use in search result listings (optional). It is generally accepted this field should be less than 70 characters.'), 181 '#default_value' => page_title_load_title($form['_account']['#value']->uid, 'user'), 182 '#size' => 60, 183 '#maxlength' => 255, 184 '#weight' => 20, 185 ); 186 } 187 } 188 elseif ($form_id == 'node_type_form') { 189 $form['page_title'] = array( 190 '#type' => 'fieldset', 191 '#title' => t('Page Title Settings'), 192 '#collapsible' => TRUE, 193 '#collapsed' => TRUE, 194 '#tree' => TRUE, 195 ); 196 197 $form['page_title']['show_field'] = array( 198 '#type' => 'checkboxes', 199 '#title' => t('Page Title Field'), 200 '#description' => t('If checked, the <em>Page Title</em> field will appear on the node edit form for those who have permission to set the title.'), 201 '#options' => array( 202 'show_field' => t('Show field'), 203 ), 204 '#default_value' => variable_get('page_title_type_'. $form['#node_type']->type .'_showfield', 0) ? array('show_field') : array(), 205 ); 206 207 $form['page_title']['pattern'] = array( 208 '#type' => 'textfield', 209 '#title' => t('Page Title Pattern'), 210 '#default_value' => variable_get('page_title_type_'. $form['#node_type']->type, ''), 211 '#description' => t('Enter the <em>Page Title</em> pattern you want to use for this node type. For more information, please use the !link settings page', array('!link' => l('Page Title', 'admin/content/page_title'))), 212 ); 213 214 $form['#submit'][] = 'page_title_node_type_form_submit'; 215 } 216 elseif ($form_id == 'forum_admin_settings') { 217 $form['page_title_forum_root_title'] = array( 218 '#type' => 'textfield', 219 '#title' => t('Page title'), 220 '#description' => t('Provide a description for this forum overview page to appear in the <title> tag which search engines can use in search result listings (optional). It is generally accepted this field should be less than 70 characters.'), 221 '#default_value' => variable_get('page_title_forum_root_title', ''), 222 '#size' => 60, 223 '#maxlength' => 255, 224 '#weight' => -1, 225 ); 226 } 227 } 228 229 230 /** 231 * Submit handler for the node_type_form element added in the hook_form_alter() above. 232 */ 233 function page_title_node_type_form_submit($form, &$form_state) { 234 $show_field = $form_state['values']['page_title']['show_field']['show_field'] ? 1 : 0; 235 variable_set('page_title_type_'. $form_state['values']['type'] .'_showfield', $show_field); 236 variable_set('page_title_type_'. $form_state['values']['type'], $form_state['values']['page_title']['pattern']); 237 238 // For some reason the node module adds the fieldset as a separate entry in the variables table... we dont want this! 239 variable_del('page_title_'. $form_state['values']['type']); 240 } 241 242 243 /** 244 * Implementation of hook_nodeapi(). 245 */ 246 function page_title_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) { 247 switch ($op) { 248 case 'update': 249 if (user_access('set page title')) { 250 db_query('DELETE FROM {page_title} WHERE type = "node" AND id = %d', $node->nid); 251 } 252 // fallthrough to insert intentional! 253 case 'insert': 254 if (isset($node->page_title) && drupal_strlen(trim($node->page_title)) > 0 && user_access('set page title')) { 255 db_query('INSERT INTO {page_title} VALUES ("node", %d, "%s")', $node->nid, $node->page_title); 256 } 257 break; 258 259 case 'delete': 260 db_query('DELETE FROM {page_title} WHERE type = "node" AND id = %d', $node->nid); 261 break; 262 263 case 'load': 264 return array('page_title' => page_title_load_title($node->nid, 'node')); 265 } 266 } 267 268 269 /** 270 * Implementation of hook_taxonomy(). 271 */ 272 function page_title_taxonomy($op, $type, $edit) { 273 if ($type == 'vocabulary') return; 274 275 switch ($op) { 276 case 'update': 277 if (user_access('set page title')) { 278 db_query('DELETE FROM {page_title} WHERE type = "term" AND id = %d', $edit['tid']); 279 } 280 // Fallthrough to insert is intentional! 281 case 'insert': 282 if (isset($edit['page_title']) && drupal_strlen(trim($edit['page_title'])) > 0 && user_access('set page title')) { 283 db_query('INSERT INTO {page_title} VALUES("term", %d, "%s")', $edit['tid'], $edit['page_title']); 284 } 285 break; 286 287 case 'delete': 288 db_query('DELETE FROM {page_title} WHERE type = "term" AND id = %d', $edit['tid']); 289 break; 290 } 291 } 292 293 294 /** 295 * Implementation of hook_user(). 296 */ 297 function page_title_user($op, &$edit, &$account) { 298 switch ($op) { 299 case 'update': 300 if (user_access('set page title')) { 301 db_query('DELETE FROM {page_title} WHERE type = "user" AND id = %d', $account->uid); 302 } 303 // Fallthrough to insert is intentional! 304 case 'insert': 305 if (isset($edit['page_title']) && drupal_strlen(trim($edit['page_title'])) > 0 && user_access('set page title')) { 306 db_query('INSERT INTO {page_title} VALUES("user", %d, "%s")', $account->uid, $edit['page_title']); 307 } 308 break; 309 310 case 'delete': 311 db_query('DELETE FROM {page_title} WHERE type = "user" AND id = %d', $account->uid); 312 break; 313 } 314 } 315 316 317 /** 318 * Simple wrapper function to get the currently set title for a page 319 * 320 * @return 321 * string the title for the current page 322 */ 323 function page_title_get_title() { 324 // If we're looking at a node or a comment on a node, get the node object from the menu system. 325 if ((arg(0) == 'node' && is_numeric(arg(1))) || (arg(0) == 'comment' && arg(1) == 'reply' && is_numeric(arg(2))) && module_exists('comment')) { 326 $node = menu_get_object(); 327 // If the node has a custom page title and the node type is configured to have a custom page title (ie, it's not a leftover from a previous setting), then use it. 328 if (!empty($node->page_title) && variable_get('page_title_type_'. $node->type .'_showfield', 0)) { 329 $title = $node->page_title; 330 } 331 } 332 // If we're looking at either a user profile page or a users blog page, get the user title 333 elseif ((arg(0) == 'user' || arg(0) == 'blog') && is_numeric(arg(1))) { 334 if (variable_get('page_title_user_showfield', 0) && ($user_title = page_title_load_title(arg(1), 'user'))) { 335 $title = $user_title; 336 } 337 } 338 // If we're looking at a taxonomy term page, get the term title 339 elseif (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2)) && module_exists('taxonomy')) { 340 $term = taxonomy_get_term(arg(2)); 341 if (variable_get('page_title_vocab_'. $term->vid .'_showfield', 0) && ($term_title = page_title_load_title($term->tid, 'term'))) { 342 $title = $term_title; 343 } 344 } 345 // If we're looking at a forum page determin if is a container/forum or the forum root 346 elseif (arg(0) == 'forum' && module_exists('forum')) { 347 // If there is a number then its a container or forum 348 if (is_numeric(arg(1))) { 349 $term = taxonomy_get_term(arg(1)); 350 if (variable_get('page_title_vocab_'. $term->vid .'_showfield', 0) && ($term_title = page_title_load_title($term->tid, 'term'))) { 351 $title = $term_title; 352 } 353 } 354 // If not then it's the forum root. 355 else { 356 $title = variable_get('page_title_forum_root_title', ''); 357 } 358 } 359 360 // If nothing above set a title, give the legacy function a chance to act 361 if (empty($title)) { 362 $title = page_title_set_title(); 363 } 364 365 // If we still have no title, fall back to the title provided by Drupal Core 366 if (empty($title)) { 367 $title = drupal_get_title(); 368 } 369 370 // Give other modules the oppertunity to use hook_page_title_alter(). 371 drupal_alter('page_title', $title); 372 373 // Return the title in a safe form (any tags removed (such as emphasised or strong tags) and eny entiied encoded) 374 return filter_xss($title, array()); 375 } 376 377 378 /** 379 * Gets the page title for a type & id. 380 * 381 * @param $id 382 * int The objects id. 383 * @param $type 384 * string What is the scope (usually 'node', 'term' or 'user'). 385 * 386 * @return 387 * string the page title for the given type & id. 388 */ 389 function page_title_load_title($id, $type) { 390 return db_result(db_query('SELECT page_title FROM {page_title} WHERE type = "%s" AND id = %d', $type, $id)); 391 } 392 393 394 /** 395 * Wrapper for old function... 396 * NOTE: This has been depricated in favor of page_title_load_title(). 397 */ 398 function page_title_node_get_title($nid) { 399 return page_title_load_title($nid, 'node'); 400 } 401 402 403 /** 404 * Legacy page title setting function... 405 * NOTE: This has been depreicated in favour of hook_page_title_alter(). 406 */ 407 function page_title_set_title($title = NULL) { 408 static $stored_title; 409 410 if (isset($title)) { 411 $stored_title = $title; 412 } 413 return $stored_title; 414 } 415 416 417 /** 418 * Determines what title should be sent to the page template. 419 * 420 * Call this function from the page hook of function _phptemplate_variables in 421 * template.php. 422 * 423 * @return 424 * string The page's title. 425 */ 426 function page_title_page_get_title() { 427 static $title = NULL; 428 429 if (is_null($title)) { 430 // If frontpage, then use the frontpage pattern and set the title. 431 if (drupal_is_front_page()) { 432 // Get the frontpage pattern 433 $page_title_pattern = variable_get('page_title_front', '[site-name] | [site-slogan]'); 434 435 // If the frontpage pattern is empty, fallback to the default. 436 if (empty($page_title_pattern)) { 437 $page_title_pattern = variable_get('page_title_default', '[page-title] | [site-slogan]'); 438 } 439 440 // Append the pattern for pages with a pager on them 441 $page_title_pattern .= isset($_REQUEST['page']) ? variable_get('page_title_pager_pattern', '') : ''; 442 443 // 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. 444 drupal_alter('page_title_pattern', $page_title_pattern); 445 446 // Apply the token patterns using the one-level replacer (frontpage is only "global" scope). Need to flush the token cache first. 447 token_get_values('global', NULL, TRUE); 448 $title = token_replace($page_title_pattern); 449 } 450 // Otherwise this is a non-frontpage page title. 451 else { 452 // Initialize some variables we need 453 $page_title_pattern = ''; 454 $types = array('global' => NULL); 455 456 // Determine scope 457 // Node (either node or comment reply) 458 if ((arg(0) == 'node' && is_numeric(arg(1))) || (arg(0) == 'comment' && arg(1) == 'reply' && is_numeric(arg(2)) && module_exists('comment')) ) { 459 $types['node'] = menu_get_object(); 460 // If the node has any taxonomy, grab the first time and pass it over to be passed as a token. 461 // TODO: Handle multiple terms? Only pass specific terms per content type? 462 if (!empty($types['node']->taxonomy)) { 463 $types['taxonomy'] = current($types['node']->taxonomy); 464 } 465 $page_title_pattern = variable_get('page_title_type_'. $types['node']->type, ''); 466 } 467 // Term 468 elseif (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2)) && module_exists('taxonomy')) { 469 $types['taxonomy'] = taxonomy_get_term(arg(2)); 470 $page_title_pattern = variable_get('page_title_vocab_'. $types['taxonomy']->vid, ''); 471 } 472 // Forum 473 elseif (arg(0) == 'forum' && module_exists('forum')) { 474 if (is_numeric(arg(1))) { 475 $types['taxonomy'] = taxonomy_get_term(arg(1)); 476 } 477 $forum_vid = variable_get('forum_nav_vocabulary', ''); 478 $page_title_pattern = variable_get('page_title_vocab_'. $forum_vid, ''); 479 } 480 // User 481 elseif (arg(0) == 'user' && is_numeric(arg(1))) { 482 $types['user'] = user_load(array('uid' => arg(1))); 483 $page_title_pattern = variable_get('page_title_user', ''); 484 } 485 // Blog 486 elseif (arg(0) == 'blog' && is_numeric(arg(1))) { 487 $types['user'] = user_load(array('uid' => arg(1))); 488 $page_title_pattern = variable_get('page_title_blog', ''); 489 } 490 491 // If pattern is emtpy (either if the type is not overridable or simply not set) fallback to the default pattern) 492 if (empty($page_title_pattern)) { 493 $page_title_pattern = variable_get('page_title_default', '[page-title] | [site-name]'); 494 } 495 496 // Append the pattern for pages with a pager on them 497 $page_title_pattern .= isset($_REQUEST['page']) ? variable_get('page_title_pager_pattern', '') : ''; 498 499 // 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. 500 $data = array(&$page_title_pattern, &$types); 501 foreach (module_implements('page_title_pattern_alter') as $module) { 502 $function = $module .'_page_title_pattern_alter'; 503 call_user_func_array($function, $data); 504 } 505 506 // Apply token patterns by resetting the token cache first and then using token_replace_multiple to insert token values 507 token_get_values('global', NULL, TRUE); 508 $title = token_replace_multiple($page_title_pattern, $types); 509 } 510 } 511 512 // Use filter_xss to remove any tags and to entity encode content. 513 return filter_xss($title, array()); 514 } 515 516 517 /** 518 * Implementation of hook_token_values(). 519 * 520 * @param 521 * string The type of token being generated 522 * 523 * @return 524 * array An array of Token ID and Token Value pairs 525 */ 526 function page_title_token_values($type) { 527 $values = array(); 528 529 if ($type == 'global') { 530 $values['page-title'] = page_title_get_title(); 531 } 532 533 return $values; 534 } 535 536 537 /** 538 * Implementation of hook_token_list(). 539 * 540 * @param 541 * string Which type of token list are we generating? 542 * 543 * @return 544 * array Nested array of Token ID and Token Name pairs. 545 */ 546 function page_title_token_list($type = 'all') { 547 $tokens = array(); 548 549 if ($type == 'global' || $type == 'all') { 550 $tokens['global']['page-title'] = t("The page title."); 551 } 552 553 return $tokens; 554 } 555 556 557 /** 558 * Implementation of hook_preprocess_page(). 559 */ 560 function page_title_preprocess_page(&$vars) { 561 $vars['head_title'] = page_title_page_get_title(); 562 } 563 564 565 /** 566 * Implementation of hook_content_extra_fields(). 567 * 568 * This allows CCK to control the weight of the Page Title element as a "non-cck field" 569 */ 570 function page_title_content_extra_fields($type_name) { 571 $extra = array(); 572 573 if (variable_get('page_title_type_'. $type_name .'_showfield', 0)) { 574 $extra['page_title'] = array( 575 'label' => t('Page Title'), 576 'description' => t('Page Title form.'), 577 'weight' => -4 578 ); 579 } 580 581 return $extra; 582 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Mar 24 11:18:33 2011 | Cross-referenced by PHPXref 0.7 |