page_title.module 6.1 KB
Newer Older
1
<?php
JohnAlbin's avatar
JohnAlbin committed
2
// $Id$
3
4

/**
JohnAlbin's avatar
JohnAlbin committed
5
6
7
 * @file
 * Enhanced control over the page title (in the head tag).
 *
8
9
10
11
12
13
14
15
16
 * This module gives you control over the page title. It gives you the chance
 * to provide templates for how the title should be structured, and on node
 * pages, gives you the chance to specify the page title rather than defaulting
 * to the node title.
 */

function page_title_help($section) {
  switch ($section) {
    case 'admin/modules#description':
JohnAlbin's avatar
JohnAlbin committed
17
      $output = t('Enhanced control over the page title (in the &lt;head&gt; tag).');
18
19
20
21
22
23
24
25
26
27
      break;
    case 'admin/help#page_title':
      $output = t('<p>Adds an extra form element to node creation forms that allows you to specify the page title.</p>
        <p>Allows for better global configuration of the page title.</p>');
      break;
    case 'admin/settings/page_title':
      $output = t("<p>The default page title follows one of two patterns:
        <ol><li><em>page title</em> | <em>site name</em></li>
            <li><em>site name</em> | <em>site slogan</em></li></ol>
        <p>#1 is the common case where the visitor is viewing a specific page whereas #2 is used at least for the default front page (<em>node</em>).</p>
JohnAlbin's avatar
JohnAlbin committed
28
        <p>The page_title.module lets you change these defaults in two ways. First, you can adjust the templates below using the placeholders given. This will change the way the default page titles are created. Second, on every node creation form, you can have the option of specifying a title that is different than the title of the node. If a value is provided, this will be used to generate the <code>!page_title</code> placeholder. If left blank, <code>!page_title</code> will be the node's title, as is now the case.</p>");
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
      break;
  }
  return $output;
}

/**
 * Implementation of hook_perm().
 */
function page_title_perm() {
  return array('set page title');
}

/**
 * Implementation of hook_menu().
 */
function page_title_menu($may_cache) {
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/settings/page_title',
      'title' => t('Page title'),
JohnAlbin's avatar
JohnAlbin committed
49
      'description' => t('Enhanced control over the page title (in the &lt;head&gt; tag).'),
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
      'callback' => 'drupal_get_form',
      'callback arguments' => 'page_title_admin_settings',
      'access' => user_access('administer site configuration'),
      'type' => MENU_NORMAL_ITEM,
    );
  }
  return $items;
}

function page_title_admin_settings() {
  $form['page_title'] = array(
    '#type' => 'fieldset',
    '#title' => t('Page title templates'),
  );
  $form['page_title']['page_title_individual'] = array(
    '#type' => 'textfield',
    '#title' => t('Pattern for individual pages'),
JohnAlbin's avatar
JohnAlbin committed
67
    '#default_value' => variable_get('page_title_individual', '!page_title | !site_name'),
68
    '#maxlength' => 128,
JohnAlbin's avatar
JohnAlbin committed
69
    '#description' => t('Available placeholders: <code>!page_title</code>, <code>!site_name</code>, <code>!site_slogan</code>.'),
70
71
72
73
  );
  $form['page_title']['page_title_front'] = array(
    '#type' => 'textfield',
    '#title' => t('Pattern for front page'),
JohnAlbin's avatar
JohnAlbin committed
74
    '#default_value' => variable_get('page_title_front', '!site_name | !site_slogan'),
75
    '#maxlength' => 128,
JohnAlbin's avatar
JohnAlbin committed
76
    '#description' => t('Available placeholders: <code>!site_name</code>, <code>!site_slogan</code>.'),
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
  );
  return system_settings_form($form);
}

function page_title_form_alter($form_id, &$form) {
  if ($form['#id'] == 'node-form' && user_access('set page title')) {
    $form['page_title'] = array(
      '#type' => 'textfield',
      '#title' => t('Page title'),
      '#default_value' => $form['#node']->page_title,
      '#size' => 60,
      '#maxlength' => 127,
      '#weight' => -4,
    );
  }
}

function page_title_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  switch ($op) {
    case 'update':
      db_query("DELETE FROM {page_title} WHERE nid = %d", $node->nid);
      // fallthrough to insert intentional!
    case 'insert':
      if (strlen(trim($node->page_title)) > 0) {
        db_query("INSERT INTO {page_title} VALUES (%d, '%s')", $node->nid, $node->page_title);
      }
      break;

    case 'delete':
      db_query('DELETE FROM {page_title} WHERE nid = %d', $node->nid);
      break;

    case 'load':
      $object = new stdClass();
      $object->page_title = page_title_node_get_title($node->nid);
      return array('page_title' => $object->page_title);

    case 'view':
      if ($page) {
        page_title_set_title($node->page_title);
      }
      break;
  }
}


/**
 * Set the page title of the current page, for display on the page and in the title bar.
 */
function page_title_set_title($title = NULL) {
  static $stored_title;

  if (isset($title)) {
    $stored_title = $title;
  }
  return $stored_title;
}

// Public API (every module's gotta have one =)

/**
 * Gets the page title for a node id
 *
 * @param the node's id
 * @return the node's page title.
 */
function page_title_node_get_title($nid) {
  $row = db_fetch_object(db_query('SELECT page_title FROM {page_title} WHERE nid = %d', $nid));
  return $row->page_title;
}

/**
 * Does the logic to see what title should be send to the page template. Call this function from
 * the page hook of function _phptemplate_variables in template.php.
 */
function page_title_page_get_title() {
JohnAlbin's avatar
JohnAlbin committed
153
  static $title = NULL;
154

JohnAlbin's avatar
JohnAlbin committed
155
156
157
  if (is_null($title)) {
    $page_title_individual = variable_get('page_title_individual', '!page_title | !site_name');
    $page_title_front = variable_get('page_title_front', '!site_name | !site_slogan');
158

JohnAlbin's avatar
JohnAlbin committed
159
160
    // this is the normal title. For node pages it is $node->title
    $drupal_title = drupal_get_title();
161

JohnAlbin's avatar
JohnAlbin committed
162
163
164
    // this is the custom page title. For node pages it is $node->page_title.
    $page_title = (page_title_set_title()) ? page_title_set_title() : $drupal_title;
    $page_title = strip_tags($page_title);
165

JohnAlbin's avatar
JohnAlbin committed
166
167
168
169
170
171
172
173
174
    $site_name = variable_get('site_name', 'drupal');
    $site_slogan = variable_get('site_slogan', '');

    if (drupal_is_front_page() || !$drupal_title) {
      $title = t($page_title_front, array('!site_name' => $site_name, '!site_slogan' => $site_slogan));
    }
    else {
      $title = t($page_title_individual, array('!page_title' => $page_title, '!site_name' => $site_name, '!site_slogan' => $site_slogan));
    }
175
176
177
  }
  return $title;
}