page_title.module 11.4 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
20
function page_title_help($section) {
  switch ($section) {
    case 'admin/help#page_title':
21
22
23
      $output  = t('<p>Provides control over the &lt;title> element on a page using token patterns and an optional textfield to override the node title.</p>');
      $output .= t('<p>Below are all the tokens you can use with <strong><em>Page Title</em></strong>:</p>');
      $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 = t("<p>To display a 'Page Title' field on a node (just below the 'Title' 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
function page_title_menu() {
  $items['admin/content/page_title'] = array(
    'title' => t('Page titles'),
    'description' => t('Enhanced control over the page titles (in the &lt;head&gt; tag).'),
    '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,
  );
  $items['admin/content/page_title/settings'] = array(
    'title' => t('Default settings'),
    'description' => t('Control the page title settings.'),
    'access callback' => 'user_access',
    'access arguments' => array('administer page titles'),
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
  $items['admin/content/page_title/types'] = array(
    'title' => t('Content type settings'),
    'description' => t('Control the display of the Page title field.'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('page_title_admin_types'),
    'access callback' => 'user_access',
    'access arguments' => array('administer page titles'),
    'type' => MENU_LOCAL_TASK,
  );

81
82
83
  return $items;
}

84
85
86
87
88
89
90

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


98
/**
99
 * Displays the form for the standard settings tab.
100
101
102
103
 *
 * @return
 *   array A structured array for use with Forms API.
 */
104
function page_title_admin_settings() {
105
  //Define the titles
106
107
  $form['title']['page_title_default'] = array('#type' => 'markup', '#value' => 'Default Pattern');
  $form['title']['page_title_front']   = array('#type' => 'markup', '#value' => 'Frontpage Pattern');
108
109
  
  //Define the 'default' token patterns
110
111
112
  $form['pattern']['page_title_default'] = array('#type' => 'textfield', '#default_value' => variable_get('page_title_default', '[page-title] | [site-name]'), '#maxlength' => 128, '#description' => t('This is the pattern used in a situation where a pattern is not defined specifically for a content type below.'));
  $form['pattern']['page_title_front']   = array('#type' => 'textfield', '#default_value' => variable_get('page_title_front', '[site-name] | [site-slogan]'),  '#maxlength' => 128, '#description' => t('This is the frontpage pattern.'));
    
113
  //Definate the patterns per-node-type
114
115
116
117
118
119
120
  $types = node_get_types();
  foreach ($types as $type) {
    $key = 'page_title_type_' . $type->type;
    $form['title'][$key]   = array('#type' => 'markup', '#value' => t('Pattern for %type', array('%type' => $type->name)), );
    $form['pattern'][$key] = array('#type' => 'textfield', '#default_value' => variable_get($key, ''), '#maxlength' => 128, '#description' => t('If left blank, will inherit from default settings.'));
  }
  
121
122
  //Add the system buttons to the form
  $form = system_settings_form($form);
123
  
124
125
126
  //Overide the theme function back to our own one
  $form['#theme'] = 'page_title_admin_settings';
  return $form;
127
128
129
}


130
131
132
133
134
135
136
/**
 * Displays the form for the "Content creation types" tab.
 *
 * @return
 *   array A structured form array for use with Forms API.
 */
function page_title_admin_types() {
137
  $form['page_title_fieldset'] = array(
138
139
    '#type' => 'fieldset',
    '#title' => t('Content creation forms'),
140
141
142
143
144
145
146
    '#description' => t('Display a "Page title" field for these content types.'),
  );

  $form['page_title_fieldset']['page_title_display'] = array(
    '#type'          => 'checkboxes',
    '#default_value' => variable_get('page_title_display', array()),
    '#options'       => node_get_types('names'),
147
148
149
150
151
152
  );

  // Add save button, etc to the form.
  return system_settings_form($form);
}

153

154
155
156
157
158
159
/**
 * Implementation of hook_node_type().
 *
 * Updates settings after a node type change.
 */
function page_title_node_type($op, $info) {
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
  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());
 
    //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;
    }
    
    //Unset the old type
    unset($display[$info->old_type]);
    
    //Save the settings
    variable_set('page_title_display', $display);
    
    //Get the old pattern, if set
    $old_pattern = variable_get('page_title_type_' . $info->old_type, '');
    
    //If it was set then set the new one to whatever the old pattern was
    if (!empty($old_pattern)) {
      variable_set('page_title_type_' . $info->type, $old_pattern);
    }
    
    //Delete the old pattern
    variable_del('page_title_type_' . $info->old_type);
189
190
191
  }
}

192

193
194
195
/**
 * Implementation of hook_form_alter().
 */
196
197
198
199
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;
  
200
201
  $display = variable_get('page_title_display', array());
  
202
203
  //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']]) {
204
205
206
    $form['page_title'] = array(
      '#type' => 'textfield',
      '#title' => t('Page title'),
207
      '#description' => t('Optionally specify a different title to appear in the &lt;title&gt; tag of the page.'),
208
209
      '#default_value' => $form['#node']->page_title,
      '#size' => 60,
210
      '#maxlength' => 255,
211
212
213
214
215
      '#weight' => -4,
    );
  }
}

216

217
218
219
/**
 * Implementation of hook_nodeapi().
 */
220
221
222
function page_title_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  switch ($op) {
    case 'update':
223
224
      db_query("DELETE FROM {page_title} WHERE nid = %d", $node->nid);
      // fallthrough to insert intentional!
225
    case 'insert':
226
227
      if (isset($node->page_title) && strlen(trim($node->page_title)) > 0) {
        db_query("INSERT INTO {page_title} VALUES (%d, '%s')", $node->nid, $node->page_title);
228
229
230
231
232
233
234
235
      }
      break;

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

    case 'load':
JohnAlbin's avatar
JohnAlbin committed
236
      return array('page_title' => page_title_node_get_title($node->nid));
237
238
239
240
  }
}


241
242
243
244
245
246
/**
 * Simple wrapper function to get the currently set title for a page
 *
 * @return unknown
 */
function page_title_get_title() {
247
248
249
250
251
252
253
254
255
  $display_options = variable_get('page_title_display', array());
  $node = ((arg(0) == 'node') && (is_numeric(arg(1)))) ? node_load(arg(1)) : NULL;
  
  if ($display_options[$node->type] && !empty($node->page_title)) {
    return check_plain(strip_tags($node->page_title));
  }
  else {
    return strip_tags(drupal_get_title());
  }
256
257
258
}


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

/**
262
 * Gets the page title for a node id.
263
 *
264
265
266
267
 * @param $nid
 *   int The node's id.
 * @return
 *   string The node's page title.
268
269
 */
function page_title_node_get_title($nid) {
270
  return db_result(db_query('SELECT page_title FROM {page_title} WHERE nid = %d', $nid));
271
272
}

273

274
/**
275
276
277
278
279
280
281
 * 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.
282
283
 */
function page_title_page_get_title() {
JohnAlbin's avatar
JohnAlbin committed
284
  static $title = NULL;
285

JohnAlbin's avatar
JohnAlbin committed
286
  if (is_null($title)) {
287
288
    // If frontpage, then use the frontpage pattern and set the title.
    if (drupal_is_front_page()) {
289
      $page_title_pattern = variable_get('page_title_front', '[site-name] | [site-slogan]');
290
      $title = token_replace($page_title_pattern);
JohnAlbin's avatar
JohnAlbin committed
291
    }
292
    //Otherwise this is a non-frontpage page title.
JohnAlbin's avatar
JohnAlbin committed
293
    else {
294
295
296
297
298
      //Get the node for this page
      $node = ((arg(0) == 'node') && (is_numeric(arg(1)))) ? node_load(arg(1)) : NULL;

      //Get the pattern for the node type. If no node type available, assume blank
      $page_title_pattern = variable_get('page_title_type_' . (isset($node->type) ? $node->type : ''), '');
299
300
301
302
303
304
      
      //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]');
      }
      
305
306
307
308
309
310
311
      // 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
312
    }
313
  }
314
  
315
316
  return $title;
}
317

318

319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/**
 * Implementation of hook_token_values
 *
 * @param
 *   string The type of token being generated
 * 
 * @return
 *   array An array of Token ID and Token Value pairs
 */
function page_title_token_values($type) {
  $values = array();
  
  if ($type == 'global') {
    $values['page-title'] = page_title_get_title();
  }
  
  return $values;
}

338

339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/**
 * Implementation of hook_token_list
 *
 * @param
 *   string Which type of token list are we generating?
 * 
 * @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.");
  }
  
  return $tokens;
}