page_title.module 8.67 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
/**
 * Implementation of hook_help().
 */
17
18
19
20
21
22
function page_title_help($section) {
  switch ($section) {
    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;
23
    case 'admin/content/page_title':
24
      $output = t("<p>Drupal’s default page title follows one of two patterns:</p>
25
        <ol><li><em>page title</em> | <em>site name</em></li>
26
        <li><em>site name</em> | <em>site slogan</em></li></ol>
27
        <p>#1 is the common case where the visitor is viewing a specific page whereas #2 is used for the default front page.</p>
JohnAlbin's avatar
JohnAlbin committed
28
        <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 be the node’s title, as is now the case.</p>");
29
      break;
30
31
32
    case 'admin/content/page_title/types':
      $output = t("<p>By default, a “Page title” field will be displayed (just below the “Title” field) on every content type’s creation form. You may hide this field for each content type below.</p>");
      break;
33
34
35
36
37
38
39
40
  }
  return $output;
}

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

/**
 * Implementation of hook_menu().
 */
function page_title_menu($may_cache) {
48
  $items = array();
49
50
  if ($may_cache) {
    $items[] = array(
51
52
53
      'path' => 'admin/content/page_title',
      'title' => t('Page titles'),
      'description' => t('Enhanced control over the page titles (in the &lt;head&gt; tag).'),
54
      'callback' => 'drupal_get_form',
55
56
      'callback arguments' => array('page_title_admin_settings'),
      'access' => user_access('administer page titles'),
57
58
      'type' => MENU_NORMAL_ITEM,
    );
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
    $items[] = array(
      'path' => 'admin/content/page_title/settings',
      'title' => t('Default settings'),
      'description' => t('Control the page title settings.'),
      'access' => user_access('administer page titles'),
      'type' => MENU_DEFAULT_LOCAL_TASK,
      'weight' => -10,
    );
    $items[] = array(
      'path' => 'admin/content/page_title/types',
      'title' => t('Content type settings'),
      'description' => t('Control the display of the Page title field.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array('page_title_admin_types'),
      'access' => user_access('administer page titles'),
      'type' => MENU_LOCAL_TASK,
    );
76
77
78
79
  }
  return $items;
}

80
/**
81
 * Displays the form for the standard settings tab.
82
83
84
85
 *
 * @return
 *   array A structured array for use with Forms API.
 */
86
87
88
function page_title_admin_settings() {
  $form['page_title'] = array(
    '#type' => 'fieldset',
89
    '#title' => t('Page title patterns'),
90
91
92
93
  );
  $form['page_title']['page_title_individual'] = array(
    '#type' => 'textfield',
    '#title' => t('Pattern for individual pages'),
JohnAlbin's avatar
JohnAlbin committed
94
    '#default_value' => variable_get('page_title_individual', '!page_title | !site_name'),
95
    '#maxlength' => 128,
JohnAlbin's avatar
JohnAlbin committed
96
    '#description' => t('Available placeholders: <code>!page_title</code>, <code>!site_name</code>, <code>!site_slogan</code>.'),
97
98
99
100
  );
  $form['page_title']['page_title_front'] = array(
    '#type' => 'textfield',
    '#title' => t('Pattern for front page'),
JohnAlbin's avatar
JohnAlbin committed
101
    '#default_value' => variable_get('page_title_front', '!site_name | !site_slogan'),
102
    '#maxlength' => 128,
JohnAlbin's avatar
JohnAlbin committed
103
    '#description' => t('Available placeholders: <code>!site_name</code>, <code>!site_slogan</code>.'),
104
105
106
107
  );
  return system_settings_form($form);
}

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
/**
 * 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() {
  $form['page_title_types'] = array(
    '#type' => 'fieldset',
    '#title' => t('Content creation forms'),
    '#description' => t('Display a “Page title” field for these content types.'),
  );
  foreach (node_get_types('names') AS $type => $name) {
    $form['page_title_types']["page_title_display_$type"] = array(
      '#type'          => 'checkbox',
      '#title'         => $name,
      '#default_value' => variable_get("page_title_display_$type", 1),
    );
  }

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

/**
 * Implementation of hook_node_type().
 *
 * Updates settings after a node type change.
 */
function page_title_node_type($op, $info) {
  if ($op == 'update' and !empty($info->old_type) and $info->type != $info->old_type) {
    $old = 'page_title_display_'. $info->old_type;
    $new = 'page_title_display_'. $info->type;

    variable_set($new, variable_get($old, 1));
    variable_del($old);
  }
}

147
148
149
/**
 * Implementation of hook_form_alter().
 */
150
function page_title_form_alter($form_id, &$form) {
151
  if ($form['#id'] == 'node-form' and user_access('set page title') and variable_get('page_title_display_'.$form['type']['#value'], 1)) {
152
153
154
    $form['page_title'] = array(
      '#type' => 'textfield',
      '#title' => t('Page title'),
155
      '#description' => t('Optionally specify a different title to appear in the &lt;title&gt; tag of the page.'),
156
157
      '#default_value' => $form['#node']->page_title,
      '#size' => 60,
158
      '#maxlength' => 255,
159
160
161
162
163
      '#weight' => -4,
    );
  }
}

164
165
166
/**
 * Implementation of hook_nodeapi().
 */
167
168
169
function page_title_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  switch ($op) {
    case 'update':
JohnAlbin's avatar
JohnAlbin committed
170
171
172
173
174
175
176
177
      if (trim($node->page_title)) {
        db_query("UPDATE {page_title} SET page_title = '%s' WHERE nid = %d", trim($node->page_title), $node->nid);
      }
      else {
        db_query("DELETE FROM {page_title} WHERE nid = %d", $node->nid);
      }
      break;

178
    case 'insert':
JohnAlbin's avatar
JohnAlbin committed
179
180
      if (trim($node->page_title)) {
        db_query("INSERT INTO {page_title} VALUES (%d, '%s')", $node->nid, trim($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

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

/**
200
201
202
203
204
205
 * Sets or retrieves the page title of the current page.
 *
 * @param $title
 *   string The page title to set.
 * @return
 *   string The current page's title.
206
207
208
209
210
211
212
213
214
215
216
217
218
 */
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 =)

/**
219
 * Gets the page title for a node id.
220
 *
221
222
223
224
 * @param $nid
 *   int The node's id.
 * @return
 *   string The node's page title.
225
226
 */
function page_title_node_get_title($nid) {
JohnAlbin's avatar
JohnAlbin committed
227
  return trim(db_result(db_query('SELECT page_title FROM {page_title} WHERE nid = %d', $nid)));
228
229
230
}

/**
231
232
233
234
235
236
237
 * 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.
238
239
 */
function page_title_page_get_title() {
JohnAlbin's avatar
JohnAlbin committed
240
  static $title = NULL;
241

JohnAlbin's avatar
JohnAlbin committed
242
243
244
  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');
245

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

JohnAlbin's avatar
JohnAlbin committed
249
250
251
    // 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);
252

JohnAlbin's avatar
JohnAlbin committed
253
254
255
256
257
258
259
260
261
    $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));
    }
262
263
264
  }
  return $title;
}