/[drupal]/contributions/modules/page_title/page_title.module
ViewVC logotype

Contents of /contributions/modules/page_title/page_title.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.21 - (show annotations) (download) (as text)
Sat Oct 31 14:02:28 2009 UTC (3 weeks, 3 days ago) by njt1982
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-7--1, DRUPAL-7--2
Changes since 1.20: +15 -4 lines
File MIME type: text/x-php
More work to make Page Title compatible with Drupal 7
1 <?php
2 // $Id: page_title.module,v 1.20 2009/10/29 18:11:19 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 * Implement 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 &lt;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&hellip;</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>[site:page-title]</samp> placeholder however if it is left blank the <samp>[site:page-title]</samp> token will inherit the item\'s own title.') .'</p>';
32 $output .= '<p>'. t('The <samp>[site: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 * Implement hook_perm().
42 */
43 function page_title_permission() {
44 return array(
45 'set page title' => array(
46 'title' => t('Set Page Title'),
47 'description' => t('Allow user to set or modify a page title'),
48 ),
49 'administer page titles' => array(
50 'title' => t('Administer Page Title'),
51 'description' => t('Perform administration tasks for Page Title'),
52 ),
53 );
54 }
55
56
57 /**
58 * Implement hook_menu().
59 */
60 function page_title_menu() {
61 $items = array();
62
63 $items['admin/content/page_title'] = array(
64 'title' => 'Page titles',
65 'description' => 'Enhanced control over the page titles (in the &lt;head&gt; tag).',
66 'page callback' => 'drupal_get_form',
67 'page arguments' => array('page_title_admin_settings'),
68 'access arguments' => array('administer page titles'),
69 'type' => MENU_LOCAL_TASK,
70 'file' => 'page_title.admin.inc',
71 );
72
73 return $items;
74 }
75
76
77 /**
78 * Implement hook_theme().
79 */
80 function page_title_theme() {
81 return array(
82 'page_title_admin_settings' => array(
83 'render element' => 'form',
84 'file' => 'page_title.admin.inc',
85 ),
86 'page_title_token_help' => array(
87 'file' => 'page_title.admin.inc',
88 ),
89 'page_title_preprocess_html' => array(
90 'arguments' => array('vars' => NULL),
91 ),
92 );
93 }
94
95
96 /**
97 * Implement hook_node_type().
98 *
99 * Updates settings after a node type change.
100 */
101 function page_title_node_type($op, $info) {
102 // Handle a content type rename
103 if ($op == 'update' && !empty($info->old_type) && $info->type != $info->old_type) {
104 // Load the old node type settings.
105 $temp = variable_get('page_title_type_'. $info->old_type, '');
106
107 // If the settings aren't empty, then save them into the new type
108 if (!empty($temp)) {
109 variable_set('page_title_type_'. $info->type, $temp);
110 }
111
112 // Delete the old setting
113 variable_del('page_title_type_'. $info->old_type);
114
115 // Essentially, do the same as above but with the _showfield suffix for the node type
116 $temp = variable_get('page_title_type_'. $info->old_type .'_showfield', 0);
117 if ($temp) {
118 variable_set('page_title_type_'. $info->type .'_showfield', $temp);
119 }
120 variable_del('page_title_type_'. $info->old_type .'_showfield');
121
122 }
123
124 // If deleted, remove the variables
125 if ($op == 'delete') {
126 variable_del('page_title_type_'. $info->type);
127 variable_del('page_title_type_'. $info->type .'_showfield');
128 }
129 }
130
131
132 /**
133 * Implement hook_form_alter().
134 */
135 function page_title_form_alter(&$form, $form_state, $form_id) {
136 // If we dont have permission to set the title then we need to abort this alter now!
137 if (!user_access('set page title')) return;
138
139 // Check we're editing a node and also check that the node type's 'show field' is enabled
140 if ($form['#id'] == 'node-form') {
141 $key = 'page_title_type_'. $form['type']['#value'] .'_showfield';
142 if (variable_get($key, 0)) {
143 $page_title = isset($form['#node']->page_title) ? $form['#node']->page_title : NULL;
144 $form['page_title'] = array(
145 '#type' => 'fieldset',
146 '#title' => t('Page title settings'),
147 '#collapsible' => TRUE,
148 '#collapsed' => empty($page_title),
149 '#group' => 'additional_settings',
150 '#weight' => 35,
151 '#attached' => array(
152 'js' => array(drupal_get_path('module', 'page_title') . '/page_title.js'),
153 ),
154 );
155 $form['page_title']['page_title'] = array(
156 '#type' => 'textfield',
157 '#title' => t('Page title'),
158 '#description' => t('Optionally specify a different title to appear in the &lt;title&gt; tag of the page.'),
159 '#default_value' => $page_title,
160 '#size' => 60,
161 '#maxlength' => 255,
162 );
163 }
164 }
165 // Check we're editing a taxonomy term and also check that the terms vocabulary's 'show field' is enabled
166 elseif ($form_id == 'taxonomy_form_term') {
167 $key = 'page_title_vocab_'. $form['vid']['#value'] .'_showfield';
168 if (variable_get($key, 0)) {
169 $form['advanced']['page_title'] = array(
170 '#type' => 'textfield',
171 '#title' => t('Page title'),
172 '#description' => t('Optionally specify a different title to appear in the &lt;title&gt; tag of the page.'),
173 '#default_value' => isset($form['tid']) ? page_title_load_title($form['tid']['#value'], 'term') : '',
174 '#size' => 60,
175 '#maxlength' => 255,
176 '#weight' => -20,
177 );
178 }
179 }
180 // Check we're editing a user profile and also check that the user settings's have 'show field' enabled
181 elseif ($form_id == 'user_profile_form') {
182 if (variable_get('page_title_user_showfield', 0)) {
183 $form['account']['page_title'] = array(
184 '#type' => 'textfield',
185 '#title' => t('Page title'),
186 '#description' => t('Optionally specify a different title to appear in the &lt;title&gt; tag of the page.'),
187 '#default_value' => page_title_load_title($form['_account']['#value']->uid, 'user'),
188 '#size' => 60,
189 '#maxlength' => 255,
190 '#weight' => 20,
191 );
192 }
193 }
194 elseif ($form_id == 'node_type_form') {
195 $form['page_title'] = array(
196 '#type' => 'fieldset',
197 '#title' => t('Page Title Settings'),
198 '#collapsible' => TRUE,
199 '#collapsed' => TRUE,
200 '#tree' => TRUE,
201 );
202
203 $form['page_title']['show_field'] = array(
204 '#type' => 'checkboxes',
205 '#title' => t('Page Title Field'),
206 '#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.'),
207 '#options' => array(
208 'show_field' => t('Show field'),
209 ),
210 '#default_value' => array('show_field' => variable_get('page_title_type_'. $form['#node_type']->type .'_showfield', 0) ? 'show_field' : 0),
211 );
212
213 $form['page_title']['pattern'] = array(
214 '#type' => 'textfield',
215 '#title' => t('Page Title Pattern'),
216 '#default_value' => variable_get('page_title_type_'. $form['#node_type']->type, ''),
217 '#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'))),
218 );
219
220 $form['#submit'][] = 'page_title_node_type_form_submit';
221 }
222 }
223
224
225 /**
226 * Submite handler for the node_type_form element added in the hook_form_alter() above.
227 */
228 function page_title_node_type_form_submit($form, &$form_state) {
229 $show_field = $form_state['values']['page_title']['show_field']['show_field'] ? 1 : 0;
230 variable_set('page_title_type_'. $form_state['values']['type'] .'_showfield', $show_field);
231 variable_set('page_title_type_'. $form_state['values']['type'], $form_state['values']['page_title']['pattern']);
232
233 // For some reason the node module adds the fieldset as a separate entry in the variables table... we dont want this!
234 variable_del('page_title_'. $form_state['values']['type']);
235 }
236
237
238 /**
239 * Implement hook_node_load().
240 */
241 function page_title_node_load($nodes) {
242 foreach ($nodes as $node) {
243 if ($page_title = page_title_load_title($node->nid, 'node')) {
244 $node->page_title = $page_title;
245 }
246 }
247 }
248
249
250 /**
251 * Implement hook_node_insert().
252 */
253 function page_title_node_insert($node) {
254 if (user_access('set page title') && isset($node->page_title) && drupal_strlen(trim($node->page_title)) > 0) {
255 db_insert('page_title')->fields(array('type' => 'node', 'id' => $node->nid, 'page_title' => $node->page_title))->execute();
256 }
257 }
258
259
260 /**
261 * Implement hook_node_update().
262 */
263 function page_title_node_update($node) {
264 if (user_access('set page title') && isset($node->page_title) && drupal_strlen(trim($node->page_title)) > 0) {
265 db_merge('page_title')->key(array('type' => 'node', 'id' => $node->nid))->fields(array('page_title' => $node->page_title))->execute();
266 }
267 }
268
269
270 /**
271 * Implement hook_node_delete().
272 */
273 function page_title_node_delete($node) {
274 db_delete('page_title')->condition('type', 'node')->condition('id', $node->nid)->execute();
275 }
276
277
278 /**
279 * Implement hook_taxonomy_term_update().
280 */
281 function page_title_taxonomy_term_update($term) {
282 if (user_access('set page title')) {
283 if (isset($term->page_title) && drupal_strlen(trim($term->page_title)) > 0) {
284 db_merge('page_title')->key(array('type' => 'term', 'id' => $term->tid))->fields(array('page_title' => $term->page_title))->execute();
285 }
286 else {
287 page_title_taxonomy_term_delete($term);
288 }
289 }
290 }
291
292
293 /**
294 * Implement hook_taxonomy_term_delete().
295 */
296 function page_title_taxonomy_term_delete($term) {
297 db_delete('page_title')->condition('type', 'term')->condition('id', $term->tid)->execute();
298 }
299
300
301 /**
302 * Implement hook_taxonomy_term_insert().
303 */
304 function page_title_taxonomy_term_insert($term) {
305 if (user_access('set page title') && isset($term->page_title) && drupal_strlen(trim($term->page_title)) > 0) {
306 db_insert('page_title')->fields(array('type' => 'term', 'id' => $term->tid, 'page_title' => $term->page_title))->execute();
307 }
308 }
309
310
311 /**
312 * Implement hook_user_insert().
313 */
314 function page_title_user_insert(&$edit, &$account, $category) {
315 if (user_access('set page title') && isset($edit['page_title']) && drupal_strlen(trim($edit['page_title'])) > 0) {
316 db_insert('page_title')->fields(array('type' => 'user', 'id' => $account->uid, 'page_title' => $edit['page_title']))->execute();
317 }
318 }
319
320
321 /**
322 * Implement hook_user_update().
323 */
324 function page_title_user_update(&$edit, &$account, $category) {
325 if (user_access('set page title')) {
326 if (isset($edit['page_title']) && drupal_strlen(trim($edit['page_title'])) > 0) {
327 db_merge('page_title')->key(array('type' => 'user', 'id' => $account->uid))->fields(array('page_title' => $edit['page_title']))->execute();
328 }
329 else {
330 db_delete('page_title')->condition('type', 'user')->condition('id', $account->uid)->execute();
331 }
332 }
333 }
334
335
336 /**
337 * Implement hook_user_cancel().
338 */
339 function page_title_user_cancel(&$edit, &$account, $method) {
340 switch ($method) {
341 case 'user_cancel_block_unpublish' : break;
342 case 'user_cancel_reassign' : break;
343 case 'user_cancel_delete' :
344 db_delete('page_title')->condition('type', 'user')->condition('id', $account->uid)->execute();
345 break;
346 }
347 }
348
349
350 /**
351 * Simple wrapper function to get the currently set title for a page
352 *
353 * @return
354 * string the title for the current page
355 */
356 function page_title_get_title() {
357 // If we're looking at a node or a comment on a node, get the node object from the menu system.
358 if ((arg(0) == 'node' && is_numeric(arg(1))) || (arg(0) == 'comment' && arg(1) == 'reply' && is_numeric(arg(2))) && module_exists('comment')) {
359 $node = menu_get_object();
360 // 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.
361 if (!empty($node->page_title) && variable_get('page_title_type_'. $node->type .'_showfield', 0)) {
362 $title = check_plain(strip_tags($node->page_title));
363 }
364 }
365 // If we're looking at either a user profile page or a users blog page, get the user title
366 elseif (($user = menu_get_object('user_uid_optional')) || ($user = menu_get_object('user'))) {
367 if (variable_get('page_title_user_showfield', 0) && ($user_title = page_title_load_title($user->uid, 'user'))) {
368 $title = check_plain(strip_tags($user_title));
369 }
370 }
371 // If we're looking at a taxonomy term page, get the term title
372 elseif (module_exists('taxonomy') && ($term = menu_get_object('taxonomy_term', 2))) {
373 if (variable_get('page_title_vocab_'. $term->vid .'_showfield', 0) && ($term_title = page_title_load_title($term->tid, 'term'))) {
374 $title = check_plain(strip_tags($term_title));
375 }
376 }
377
378 // If nothing above set a title, give the legacy function a chance to act
379 if (empty($title)) {
380 $title = check_plain(strip_tags(page_title_set_title()));
381 }
382
383 // If we still have no title, fall back to the title provided by Drupal Core
384 if (empty($title)) {
385 $title = drupal_get_title();
386 }
387
388 // Give other modules the oppertunity to use hook_page_title_alter().
389 drupal_alter('page_title', $title);
390
391 // Return the title
392 return $title;
393 }
394
395
396 /**
397 * Gets the page title for a type & id.
398 *
399 * @param $id
400 * int The objects id.
401 * @param $type
402 * string What is the scope (usually 'node', 'term' or 'user').
403 *
404 * @return
405 * string the page title for the given type & id.
406 */
407 function page_title_load_title($id, $type) {
408 return db_query('SELECT page_title FROM {page_title} WHERE type = :type AND id = :id', array(':type' => $type, ':id' => $id))->fetchField();
409 }
410
411
412 /**
413 * Wrapper for old function...
414 * NOTE: This has been depricated in favor of page_title_load_title().
415 */
416 function page_title_node_get_title($nid) {
417 return page_title_load_title($nid, 'node');
418 }
419
420
421 /**
422 * Legacy page title setting function...
423 * NOTE: This has been depreicated in favour of hook_page_title_alter().
424 */
425 function page_title_set_title($title = NULL) {
426 static $stored_title;
427
428 if (isset($title)) {
429 $stored_title = $title;
430 }
431 return $stored_title;
432 }
433
434
435 /**
436 * Determines what title should be sent to the page template.
437 *
438 * This function gets called from the implementation of hook_preprocess_html
439 *
440 * @return
441 * string The page's title.
442 */
443 function page_title_page_get_title() {
444 static $title = NULL;
445 $types = array('global' => NULL);
446
447 if (is_null($title)) {
448 // If frontpage, then use the frontpage pattern and set the title.
449 if (drupal_is_front_page()) {
450 // Get the frontpage pattern
451 $page_title_pattern = variable_get('page_title_front', '[site:name] | [site:slogan]');
452
453 // If the frontpage pattern is empty, fallback to the default.
454 if (empty($page_title_pattern)) {
455 $page_title_pattern = variable_get('page_title_default', '[site:page-title] | [site:slogan]');
456 }
457
458 // Append the pattern for pages with a pager on them
459 $page_title_pattern .= isset($_REQUEST['page']) ? variable_get('page_title_pager_pattern', '') : '';
460
461 // Apply the token patterns using the one-level replacer (frontpage is only "global" scope)
462 $title = token_replace($page_title_pattern);
463 }
464 // Otherwise this is a non-frontpage page title.
465 else {
466 // Initialize some variables we need
467 $page_title_pattern = '';
468
469 // Determine scope
470 // Node
471 if ($node = menu_get_object()) {
472 $types['node'] = $node;
473 // TODO: Figure out comment pages. The new Menu API buggers about the arg() and $_GET['q']...
474 $page_title_pattern = variable_get('page_title_type_'. $node->type, '');
475 }
476 // Term
477 elseif (module_exists('taxonomy') && $term = menu_get_object('taxonomy_term', 2)) {
478 $types['taxonomy'] = $term;
479 $page_title_pattern = variable_get('page_title_vocab_'. $term->vid, '');
480 }
481 // User-based page... User Profile / Blog
482 elseif ($user = menu_get_object('user_uid_optional') || $user = menu_get_object('user')) {
483 $types['user'] = $user;
484 switch (arg(0)) {
485 case 'blog' : $page_title_pattern = variable_get('page_title_blog', ''); break;
486 default : $page_title_pattern = variable_get('page_title_user', ''); break;
487 }
488 }
489
490 // If pattern is emtpy (either if the type is not overridable or simply not set) fallback to the default pattern
491 if (empty($page_title_pattern)) {
492 $page_title_pattern = variable_get('page_title_default', '[site:page-title] | [site:name]');
493 }
494
495 // Append the pattern for pages with a pager on them
496 $page_title_pattern .= isset($_REQUEST['page']) ? variable_get('page_title_pager_pattern', '') : '';
497
498 // Apply token patterns using token_replace
499 $title = token_replace($page_title_pattern, $types);
500 }
501 }
502
503 return strip_tags($title);
504 }
505
506
507 /**
508 * Implement hook_token_values().
509 *
510 * @param
511 * string The type of token being generated
512 *
513 * @return
514 * array An array of Token ID and Token Value pairs
515 */
516 function page_title_tokens($type, $tokens, $data, $language = NULL, $sanitize = TRUE) {
517 $replacements = array();
518 if ($type == 'site' && isset($tokens['page-title'])) {
519 $replacements[$tokens['page-title']] = page_title_get_title();
520 }
521
522 return $replacements;
523 }
524
525
526 /**
527 * Implement hook_token_info().
528 */
529 function page_title_token_info() {
530 $result = array();
531
532 $result['tokens']['site']['page-title'] = array(
533 'name' => t('Page Title'),
534 'description' => t('The Page Title is used in the head of the page'),
535 );
536
537 return $result;
538 }
539
540
541 /**
542 * Implement hook_preprocess_html().
543 */
544 function page_title_preprocess_html(&$vars) {
545 $vars['head_title'] = page_title_page_get_title();
546 }

  ViewVC Help
Powered by ViewVC 1.1.2