page_title.module in Page Title 7
Same filename and directory in other branches
Enhanced control over the page title (in the head tag).
This module gives you control over the page title. It gives you the chance to provide patterns 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.
File
page_title.moduleView source
<?php
/**
* @file
* Enhanced control over the page title (in the head tag).
*
* This module gives you control over the page title. It gives you the chance
* to provide patterns 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.
*/
/**
* Implement hook_help().
*/
function page_title_help($path, $arg) {
$output = NULL;
switch ($path) {
case 'admin/content/page_title':
$output = '<p>' . t('Page Title provides control over the <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…</em></strong> link below if you need further assistance.') . '</p>';
break;
case 'admin/help#page_title':
$output = '<p>' . t('Drupal\'s default page title follows one of two patterns:') . '</p>';
$items = array(
t('<strong>Default Page</strong>: <samp><em>page title</em> | <em>site name</em></samp>'),
t('<strong>Default Frontpage</strong>: <samp><em>site name</em> | <em>site slogan</em></samp>'),
);
$output .= theme('item_list', $items, NULL, 'ol');
$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>';
$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>';
$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>';
break;
}
return $output;
}
/**
* Implement hook_perm().
*/
function page_title_permission() {
return array(
'set page title' => array(
'title' => t('Set Page Title'),
'description' => t('Allow user to set or modify a page title'),
),
'administer page titles' => array(
'title' => t('Administer Page Title'),
'description' => t('Perform administration tasks for Page Title'),
),
);
}
/**
* Implement hook_menu().
*/
function page_title_menu() {
$items = array();
$items['admin/content/page_title'] = array(
'title' => 'Page titles',
'description' => 'Enhanced control over the page titles (in the <head> tag).',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'page_title_admin_settings',
),
'access arguments' => array(
'administer page titles',
),
'type' => MENU_LOCAL_TASK,
'file' => 'page_title.admin.inc',
);
return $items;
}
/**
* Implement hook_theme().
*/
function page_title_theme() {
return array(
'page_title_admin_settings' => array(
'render element' => 'form',
'file' => 'page_title.admin.inc',
),
'page_title_token_help' => array(
'file' => 'page_title.admin.inc',
),
'page_title_preprocess_html' => array(
'arguments' => array(
'vars' => NULL,
),
),
);
}
/**
* Implement hook_node_type().
*
* Updates settings after a node type change.
*/
function page_title_node_type($op, $info) {
// Handle a content type rename
if ($op == 'update' && !empty($info->old_type) && $info->type != $info->old_type) {
// Load the old node type settings.
$temp = variable_get('page_title_type_' . $info->old_type, '');
// If the settings aren't empty, then save them into the new type
if (!empty($temp)) {
variable_set('page_title_type_' . $info->type, $temp);
}
// Delete the old setting
variable_del('page_title_type_' . $info->old_type);
// Essentially, do the same as above but with the _showfield suffix for the node type
$temp = variable_get('page_title_type_' . $info->old_type . '_showfield', 0);
if ($temp) {
variable_set('page_title_type_' . $info->type . '_showfield', $temp);
}
variable_del('page_title_type_' . $info->old_type . '_showfield');
}
// If deleted, remove the variables
if ($op == 'delete') {
variable_del('page_title_type_' . $info->type);
variable_del('page_title_type_' . $info->type . '_showfield');
}
}
/**
* Implement hook_form_alter().
*/
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;
}
// Check we're editing a node and also check that the node type's 'show field' is enabled
if ($form['#id'] == 'node-form') {
$key = 'page_title_type_' . $form['type']['#value'] . '_showfield';
if (variable_get($key, 0)) {
$page_title = isset($form['#node']->page_title) ? $form['#node']->page_title : NULL;
$form['page_title'] = array(
'#type' => 'fieldset',
'#title' => t('Page title settings'),
'#collapsible' => TRUE,
'#collapsed' => empty($page_title),
'#group' => 'additional_settings',
'#weight' => 35,
'#attached' => array(
'js' => array(
drupal_get_path('module', 'page_title') . '/page_title.js',
),
),
);
$form['page_title']['page_title'] = array(
'#type' => 'textfield',
'#title' => t('Page title'),
'#description' => t('Optionally specify a different title to appear in the <title> tag of the page.'),
'#default_value' => $page_title,
'#size' => 60,
'#maxlength' => 255,
);
}
}
elseif ($form_id == 'taxonomy_form_term') {
$key = 'page_title_vocab_' . $form['vid']['#value'] . '_showfield';
if (variable_get($key, 0)) {
$form['advanced']['page_title'] = array(
'#type' => 'textfield',
'#title' => t('Page title'),
'#description' => t('Optionally specify a different title to appear in the <title> tag of the page.'),
'#default_value' => isset($form['tid']) ? page_title_load_title($form['tid']['#value'], 'term') : '',
'#size' => 60,
'#maxlength' => 255,
'#weight' => -20,
);
}
}
elseif ($form_id == 'user_profile_form') {
if (variable_get('page_title_user_showfield', 0)) {
$form['account']['page_title'] = array(
'#type' => 'textfield',
'#title' => t('Page title'),
'#description' => t('Optionally specify a different title to appear in the <title> tag of the page.'),
'#default_value' => page_title_load_title($form['_account']['#value']->uid, 'user'),
'#size' => 60,
'#maxlength' => 255,
'#weight' => 20,
);
}
}
elseif ($form_id == 'node_type_form') {
$form['page_title'] = array(
'#type' => 'fieldset',
'#title' => t('Page Title Settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#tree' => TRUE,
);
$form['page_title']['show_field'] = array(
'#type' => 'checkboxes',
'#title' => t('Page Title Field'),
'#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.'),
'#options' => array(
'show_field' => t('Show field'),
),
'#default_value' => array(
'show_field' => variable_get('page_title_type_' . $form['#node_type']->type . '_showfield', 0) ? 'show_field' : 0,
),
);
$form['page_title']['pattern'] = array(
'#type' => 'textfield',
'#title' => t('Page Title Pattern'),
'#default_value' => variable_get('page_title_type_' . $form['#node_type']->type, ''),
'#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'),
)),
);
$form['#submit'][] = 'page_title_node_type_form_submit';
}
}
/**
* Submite handler for the node_type_form element added in the hook_form_alter() above.
*/
function page_title_node_type_form_submit($form, &$form_state) {
$show_field = $form_state['values']['page_title']['show_field']['show_field'] ? 1 : 0;
variable_set('page_title_type_' . $form_state['values']['type'] . '_showfield', $show_field);
variable_set('page_title_type_' . $form_state['values']['type'], $form_state['values']['page_title']['pattern']);
// For some reason the node module adds the fieldset as a separate entry in the variables table... we dont want this!
variable_del('page_title_' . $form_state['values']['type']);
}
/**
* Implement hook_node_load().
*/
function page_title_node_load($nodes) {
foreach ($nodes as $node) {
if ($page_title = page_title_load_title($node->nid, 'node')) {
$node->page_title = $page_title;
}
}
}
/**
* Implement hook_node_insert().
*/
function page_title_node_insert($node) {
if (user_access('set page title') && isset($node->page_title) && drupal_strlen(trim($node->page_title)) > 0) {
db_insert('page_title')
->fields(array(
'type' => 'node',
'id' => $node->nid,
'page_title' => $node->page_title,
))
->execute();
}
}
/**
* Implement hook_node_update().
*/
function page_title_node_update($node) {
if (user_access('set page title') && isset($node->page_title) && drupal_strlen(trim($node->page_title)) > 0) {
db_merge('page_title')
->key(array(
'type' => 'node',
'id' => $node->nid,
))
->fields(array(
'page_title' => $node->page_title,
))
->execute();
}
}
/**
* Implement hook_node_delete().
*/
function page_title_node_delete($node) {
db_delete('page_title')
->condition('type', 'node')
->condition('id', $node->nid)
->execute();
}
/**
* Implement hook_taxonomy_term_update().
*/
function page_title_taxonomy_term_update($term) {
if (user_access('set page title')) {
if (isset($term->page_title) && drupal_strlen(trim($term->page_title)) > 0) {
db_merge('page_title')
->key(array(
'type' => 'term',
'id' => $term->tid,
))
->fields(array(
'page_title' => $term->page_title,
))
->execute();
}
else {
page_title_taxonomy_term_delete($term);
}
}
}
/**
* Implement hook_taxonomy_term_delete().
*/
function page_title_taxonomy_term_delete($term) {
db_delete('page_title')
->condition('type', 'term')
->condition('id', $term->tid)
->execute();
}
/**
* Implement hook_taxonomy_term_insert().
*/
function page_title_taxonomy_term_insert($term) {
if (user_access('set page title') && isset($term->page_title) && drupal_strlen(trim($term->page_title)) > 0) {
db_insert('page_title')
->fields(array(
'type' => 'term',
'id' => $term->tid,
'page_title' => $term->page_title,
))
->execute();
}
}
/**
* Implement hook_user_insert().
*/
function page_title_user_insert(&$edit, &$account, $category) {
if (user_access('set page title') && isset($edit['page_title']) && drupal_strlen(trim($edit['page_title'])) > 0) {
db_insert('page_title')
->fields(array(
'type' => 'user',
'id' => $account->uid,
'page_title' => $edit['page_title'],
))
->execute();
}
}
/**
* Implement hook_user_update().
*/
function page_title_user_update(&$edit, &$account, $category) {
if (user_access('set page title')) {
if (isset($edit['page_title']) && drupal_strlen(trim($edit['page_title'])) > 0) {
db_merge('page_title')
->key(array(
'type' => 'user',
'id' => $account->uid,
))
->fields(array(
'page_title' => $edit['page_title'],
))
->execute();
}
else {
db_delete('page_title')
->condition('type', 'user')
->condition('id', $account->uid)
->execute();
}
}
}
/**
* Implement hook_user_cancel().
*/
function page_title_user_cancel(&$edit, &$account, $method) {
switch ($method) {
case 'user_cancel_block_unpublish':
break;
case 'user_cancel_reassign':
break;
case 'user_cancel_delete':
db_delete('page_title')
->condition('type', 'user')
->condition('id', $account->uid)
->execute();
break;
}
}
/**
* Simple wrapper function to get the currently set title for a page
*
* @return
* string the title for the current page
*/
function page_title_get_title() {
// If we're looking at a node or a comment on a node, get the node object from the menu system.
if (arg(0) == 'node' && is_numeric(arg(1)) || arg(0) == 'comment' && arg(1) == 'reply' && is_numeric(arg(2)) && module_exists('comment')) {
$node = menu_get_object();
// 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.
if (!empty($node->page_title) && variable_get('page_title_type_' . $node->type . '_showfield', 0)) {
$title = check_plain(strip_tags($node->page_title));
}
}
elseif (($user = menu_get_object('user_uid_optional')) || ($user = menu_get_object('user'))) {
if (variable_get('page_title_user_showfield', 0) && ($user_title = page_title_load_title($user->uid, 'user'))) {
$title = check_plain(strip_tags($user_title));
}
}
elseif (module_exists('taxonomy') && ($term = menu_get_object('taxonomy_term', 2))) {
if (variable_get('page_title_vocab_' . $term->vid . '_showfield', 0) && ($term_title = page_title_load_title($term->tid, 'term'))) {
$title = check_plain(strip_tags($term_title));
}
}
// If nothing above set a title, give the legacy function a chance to act
if (empty($title)) {
$title = check_plain(strip_tags(page_title_set_title()));
}
// If we still have no title, fall back to the title provided by Drupal Core
if (empty($title)) {
$title = drupal_get_title();
}
// Give other modules the oppertunity to use hook_page_title_alter().
drupal_alter('page_title', $title);
// Return the title
return $title;
}
/**
* Gets the page title for a type & id.
*
* @param $id
* int The objects id.
* @param $type
* string What is the scope (usually 'node', 'term' or 'user').
*
* @return
* string the page title for the given type & id.
*/
function page_title_load_title($id, $type) {
return db_query('SELECT page_title FROM {page_title} WHERE type = :type AND id = :id', array(
':type' => $type,
':id' => $id,
))
->fetchField();
}
/**
* Wrapper for old function...
* NOTE: This has been depricated in favor of page_title_load_title().
*/
function page_title_node_get_title($nid) {
return page_title_load_title($nid, 'node');
}
/**
* Legacy page title setting function...
* NOTE: This has been depreicated in favour of hook_page_title_alter().
*/
function page_title_set_title($title = NULL) {
static $stored_title;
if (isset($title)) {
$stored_title = $title;
}
return $stored_title;
}
/**
* Determines what title should be sent to the page template.
*
* This function gets called from the implementation of hook_preprocess_html
*
* @return
* string The page's title.
*/
function page_title_page_get_title() {
static $title = NULL;
$types = array(
'global' => NULL,
);
if (is_null($title)) {
// If frontpage, then use the frontpage pattern and set the title.
if (drupal_is_front_page()) {
// Get the frontpage pattern
$page_title_pattern = variable_get('page_title_front', '[site:name] | [site:slogan]');
// If the frontpage pattern is empty, fallback to the default.
if (empty($page_title_pattern)) {
$page_title_pattern = variable_get('page_title_default', '[site:page-title] | [site:slogan]');
}
// Append the pattern for pages with a pager on them
$page_title_pattern .= isset($_REQUEST['page']) ? variable_get('page_title_pager_pattern', '') : '';
// Apply the token patterns using the one-level replacer (frontpage is only "global" scope)
$title = token_replace($page_title_pattern);
}
else {
// Initialize some variables we need
$page_title_pattern = '';
// Determine scope
// Node
if ($node = menu_get_object()) {
$types['node'] = $node;
// TODO: Figure out comment pages. The new Menu API buggers about the arg() and $_GET['q']...
$page_title_pattern = variable_get('page_title_type_' . $node->type, '');
}
elseif (module_exists('taxonomy') && ($term = menu_get_object('taxonomy_term', 2))) {
$types['taxonomy'] = $term;
$page_title_pattern = variable_get('page_title_vocab_' . $term->vid, '');
}
elseif ($user = menu_get_object('user_uid_optional') || ($user = menu_get_object('user'))) {
$types['user'] = $user;
switch (arg(0)) {
case 'blog':
$page_title_pattern = variable_get('page_title_blog', '');
break;
default:
$page_title_pattern = variable_get('page_title_user', '');
break;
}
}
// 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', '[site:page-title] | [site:name]');
}
// Append the pattern for pages with a pager on them
$page_title_pattern .= isset($_REQUEST['page']) ? variable_get('page_title_pager_pattern', '') : '';
// Apply token patterns using token_replace
$title = token_replace($page_title_pattern, $types);
}
}
return strip_tags($title);
}
/**
* Implement 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_tokens($type, $tokens, $data, $language = NULL, $sanitize = TRUE) {
$replacements = array();
if ($type == 'site' && isset($tokens['page-title'])) {
$replacements[$tokens['page-title']] = page_title_get_title();
}
return $replacements;
}
/**
* Implement hook_token_info().
*/
function page_title_token_info() {
$result = array();
$result['tokens']['site']['page-title'] = array(
'name' => t('Page Title'),
'description' => t('The Page Title is used in the head of the page'),
);
return $result;
}
/**
* Implement hook_preprocess_html().
*/
function page_title_preprocess_html(&$vars) {
$vars['head_title'] = page_title_page_get_title();
}