You are here

page_title.module in Page Title 5

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.module
View 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.
 */

/**
 * Implementation of hook_help().
 */
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;
    case 'admin/settings/page_title':
      $output = t("<p>The default page title follows one of two patterns:</p>\n        <ol><li><em>page title</em> | <em>site name</em></li>\n            <li><em>site name</em> | <em>site slogan</em></li></ol>\n        <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>\n        <p>The page_title.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 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>");
      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) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/settings/page_title',
      'title' => t('Page title'),
      'description' => t('Enhanced control over the page title (in the &lt;head&gt; tag).'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'page_title_admin_settings',
      ),
      'access' => user_access('administer site configuration'),
      'type' => MENU_NORMAL_ITEM,
    );
  }
  return $items;
}

/**
 * Displays the form for the standard settings tab.
 *
 * @return
 *   array A structured array for use with Forms API.
 */
function page_title_admin_settings() {
  $form['page_title'] = array(
    '#type' => 'fieldset',
    '#title' => t('Page title patterns'),
  );
  $form['page_title']['page_title_individual'] = array(
    '#type' => 'textfield',
    '#title' => t('Pattern for individual pages'),
    '#default_value' => variable_get('page_title_individual', '!page_title | !site_name'),
    '#maxlength' => 128,
    '#description' => t('Available placeholders: <code>!page_title</code>, <code>!site_name</code>, <code>!site_slogan</code>.'),
  );
  $form['page_title']['page_title_front'] = array(
    '#type' => 'textfield',
    '#title' => t('Pattern for front page'),
    '#default_value' => variable_get('page_title_front', '!site_name | !site_slogan'),
    '#maxlength' => 128,
    '#description' => t('Available placeholders: <code>!site_name</code>, <code>!site_slogan</code>.'),
  );
  return system_settings_form($form);
}

/**
 * Implementation of hook_form_alter().
 */
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'),
      '#description' => t('Optionally specify a different title to appear in the &lt;title&gt; tag of the page.'),
      '#default_value' => $form['#node']->page_title,
      '#size' => 60,
      '#maxlength' => 255,
      '#weight' => -4,
    );
  }
}

/**
 * Implementation of hook_nodeapi().
 */
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;
  }
}

/**
 * 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.
 */
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 $nid
 *   int The node's id.
 * @return
 *   string 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;
}

/**
 * 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.
 */
function page_title_page_get_title() {
  static $title = NULL;
  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');

    // this is the normal title. For node pages it is $node->title
    $drupal_title = drupal_get_title();

    // 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);
    $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,
      ));
    }
  }
  return $title;
}

Functions

Namesort descending Description
page_title_admin_settings Displays the form for the standard settings tab.
page_title_form_alter Implementation of hook_form_alter().
page_title_help Implementation of hook_help().
page_title_menu Implementation of hook_menu().
page_title_nodeapi Implementation of hook_nodeapi().
page_title_node_get_title Gets the page title for a node id.
page_title_page_get_title Determines what title should be sent to the page template.
page_title_perm Implementation of hook_perm().
page_title_set_title Sets or retrieves the page title of the current page.