page_title.module 9.51 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
 * 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
11
12
13
 * pages, gives you the chance to specify the page title rather than defaulting
 * to the node title.
 */

14

15
16
17
/**
 * Implementation of hook_help().
 */
18
19
function page_title_help($path, $arg) {
  switch ($path) {
20
    case 'admin/help#page_title':
21
22
      $output  = '<p>'. t('Provides control over the &lt;title> element on a page using token patterns and an optional textfield to override the node title.') .'</p>';
      $output .= '<p>'. t('Below are all the tokens you can use with <strong><em>Page Title</em></strong>:') .'</p>';
23
      $output .= theme('token_help');
24
      break;
25
    case 'admin/content/page_title':
26
27
28
29
30
31
32
      $output = t("<p>Drupal's default page title follows one of two patterns:</p>
        <ol>
          <li><strong>Default Page:</strong> <em>page title</em> | <em>site name</em></li>
          <li><strong>Default Frontpage:</strong> <em>site name</em> | <em>site slogan</em></li>
        </ol>
        <p>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 every content 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 inherit the node's title.</p>
        <p><code>[page-title]</code> will default to the value returned from <code>drupal_get_title</code> if there is no overriden page title.</p>");
33
      break;
34
    case 'admin/content/page_title/types':
35
      $output = '<p>'. t('To display a <em>Page Title</em> field on a node (just below the <em>Title</em> field), you will need to enable it for the appropriate content types below.') .'</p>';
36
      break;
37
38
39
40
  }
  return $output;
}

41

42
43
44
45
/**
 * Implementation of hook_perm().
 */
function page_title_perm() {
46
  return array('set page title', 'administer page titles');
47
48
}

49

50
51
52
/**
 * Implementation of hook_menu().
 */
53
function page_title_menu() {
54
55
  $items = array();

56
  $items['admin/content/page_title'] = array(
57
58
    'title' => 'Page titles',
    'description' => 'Enhanced control over the page titles (in the &lt;head&gt; tag).',
59
60
61
62
63
    'page callback' => 'drupal_get_form',
    'page arguments' => array('page_title_admin_settings'),
    'access callback' => 'user_access',
    'access arguments' => array('administer page titles'),
    'type' => MENU_NORMAL_ITEM,
64
    'file' => 'page_title.admin.inc',
65
66
  );
  $items['admin/content/page_title/settings'] = array(
67
68
    'title' => 'Default settings',
    'description' => 'Control the page title settings.',
69
70
71
    'access callback' => 'user_access',
    'access arguments' => array('administer page titles'),
    'type' => MENU_DEFAULT_LOCAL_TASK,
72
    'file' => 'page_title.admin.inc',
73
74
75
    'weight' => -10,
  );
  $items['admin/content/page_title/types'] = array(
76
77
    'title' => 'Content type settings',
    'description' => 'Control the display of the Page title field.',
78
79
80
81
    'page callback' => 'drupal_get_form',
    'page arguments' => array('page_title_admin_types'),
    'access callback' => 'user_access',
    'access arguments' => array('administer page titles'),
82
    'file' => 'page_title.admin.inc',
83
84
85
    'type' => MENU_LOCAL_TASK,
  );

86
87
88
  return $items;
}

89
90

/**
91
 * Implementation of hook_theme().
92
93
94
95
 */
function page_title_theme() {
  return array(
    'page_title_admin_settings' => array(
96
      'template' => 'page_title-admin-settings-form',
97
98
      'arguments' => array('form' => NULL),
    ),
99
100
101
102

    'page_title_preprocess_page' => array(
      'arguments' => array('vars' => NULL),
    ),
103
104
105
106
  );
}


107
108
109
110
111
112
/**
 * Implementation of hook_node_type().
 *
 * Updates settings after a node type change.
 */
function page_title_node_type($op, $info) {
113
114
115
  if ($op == 'update' && !empty($info->old_type) and $info->type != $info->old_type) {
    //Get the display settings for node types
    $display = variable_get('page_title_display', array());
116

117
118
119
120
121
122
123
124
    //If the old one is set, set the new one
    if ($display[$info->old_type]) {
      $display[$info->type] = $info->type;
    }
    //Otherwise the new type is disabled like the old one was
    else {
      $display[$info->type] = 0;
    }
125

126
127
    //Unset the old type
    unset($display[$info->old_type]);
128

129
130
    //Save the settings
    variable_set('page_title_display', $display);
131

132
    //Get the old pattern, if set
133
134
    $old_pattern = variable_get('page_title_type_'. $info->old_type, '');

135
136
    //If it was set then set the new one to whatever the old pattern was
    if (!empty($old_pattern)) {
137
      variable_set('page_title_type_'. $info->type, $old_pattern);
138
    }
139

140
    //Delete the old pattern
141
    variable_del('page_title_type_'. $info->old_type);
142
143
144
  }
}

145

146
147
148
/**
 * Implementation of hook_form_alter().
 */
149
150
151
function page_title_form_alter(&$form, $form_state, $form_id) {
  //If we dont have permission to set the title then we need to abort this alter now!
  if (!user_access('set page title')) return;
152

153
  $display = variable_get('page_title_display', array());
154

155
156
  //Check we're editing a node form and also check that the node type 'value' is enabled
  if ($form['#id'] == 'node-form' && $display[$form['type']['#value']]) {
157
158
159
    $form['page_title'] = array(
      '#type' => 'textfield',
      '#title' => t('Page title'),
160
      '#description' => t('Optionally specify a different title to appear in the &lt;title&gt; tag of the page.'),
161
162
      '#default_value' => $form['#node']->page_title,
      '#size' => 60,
163
      '#maxlength' => 255,
164
165
166
167
168
      '#weight' => -4,
    );
  }
}

169

170
171
172
/**
 * Implementation of hook_nodeapi().
 */
173
174
175
function page_title_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  switch ($op) {
    case 'update':
176
177
      db_query("DELETE FROM {page_title} WHERE nid = %d", $node->nid);
      // fallthrough to insert intentional!
178
    case 'insert':
179
      if (isset($node->page_title) && drupal_strlen(trim($node->page_title)) > 0) {
180
        db_query("INSERT INTO {page_title} VALUES (%d, '%s')", $node->nid, $node->page_title);
181
182
183
184
185
186
187
188
      }
      break;

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

    case 'load':
JohnAlbin's avatar
JohnAlbin committed
189
      return array('page_title' => page_title_node_get_title($node->nid));
190
191
192
193
  }
}


194
195
196
197
198
199
/**
 * Simple wrapper function to get the currently set title for a page
 *
 * @return unknown
 */
function page_title_get_title() {
200
  $display_options = variable_get('page_title_display', array());
201
202
  $node = ((arg(0) == 'node') && (is_numeric(arg(1)))) ? menu_get_object() : NULL;

203
204
205
206
207
208
  if ($display_options[$node->type] && !empty($node->page_title)) {
    return check_plain(strip_tags($node->page_title));
  }
  else {
    return strip_tags(drupal_get_title());
  }
209
210
211
}


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

/**
215
 * Gets the page title for a node id.
216
 *
217
218
219
220
 * @param $nid
 *   int The node's id.
 * @return
 *   string The node's page title.
221
222
 */
function page_title_node_get_title($nid) {
223
  return db_result(db_query('SELECT page_title FROM {page_title} WHERE nid = %d', $nid));
224
225
}

226

227
/**
228
229
230
231
232
233
234
 * Determines what title should be sent to the page template.
 *
 * Call this function from the page hook of function _phptemplate_variables in
 * template.php.
 *
 * @return
 *   string The page's title.
235
236
 */
function page_title_page_get_title() {
JohnAlbin's avatar
JohnAlbin committed
237
  static $title = NULL;
238

JohnAlbin's avatar
JohnAlbin committed
239
  if (is_null($title)) {
240
241
    // If frontpage, then use the frontpage pattern and set the title.
    if (drupal_is_front_page()) {
242
      $page_title_pattern = variable_get('page_title_front', '[site-name] | [site-slogan]');
243
      $title = token_replace($page_title_pattern);
JohnAlbin's avatar
JohnAlbin committed
244
    }
245
    //Otherwise this is a non-frontpage page title.
JohnAlbin's avatar
JohnAlbin committed
246
    else {
247
      //Get the node for this page
248
      $node = ((arg(0) == 'node') && (is_numeric(arg(1)))) ? menu_get_object() : NULL;
249
250

      //Get the pattern for the node type. If no node type available, assume blank
251
252
      $page_title_pattern = variable_get('page_title_type_'. (isset($node->type) ? $node->type : ''), '');

253
254
255
256
      //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]');
      }
257

258
259
260
261
262
263
264
      // Return the title using the node scope if node is set, otherwise default to global scope.
      if (isset($node)) {
        $title = token_replace($page_title_pattern, 'node', $node);
      }
      else {
        $title = token_replace($page_title_pattern);
      }
JohnAlbin's avatar
JohnAlbin committed
265
    }
266
  }
267

268
269
  return $title;
}
270

271

272
/**
273
 * Implementation of hook_token_values().
274
275
276
 *
 * @param
 *   string The type of token being generated
277
 *
278
279
280
281
282
 * @return
 *   array An array of Token ID and Token Value pairs
 */
function page_title_token_values($type) {
  $values = array();
283

284
285
286
  if ($type == 'global') {
    $values['page-title'] = page_title_get_title();
  }
287

288
289
290
  return $values;
}

291

292
/**
293
 * Implementation of hook_token_list().
294
295
296
 *
 * @param
 *   string Which type of token list are we generating?
297
 *
298
299
300
301
302
303
304
305
306
 * @return
 *   array Nested array of Token ID and Token Name pairs.
 */
function page_title_token_list($type = 'all') {
  $tokens = array();

  if ($type == 'global' || $type == 'all') {
    $tokens['global']['page-title'] = t("The page title.");
  }
307

308
309
  return $tokens;
}
310
311
312


/**
313
 * Implementation of hook_preprocess_page().
314
315
316
317
 */
function page_title_preprocess_page(&$vars) {
  $vars['head_title'] = page_title_page_get_title();
}