home | career | drupal | java | mac | mysql | perl | php | scala | uml | unix

Drupal example source code file (page_title.module)

This example Drupal source code file (page_title.module) is included in the DevDaily.com "Drupal Source Code Warehouse" project. The intent of this project is to help you "Learn Drupal by Example".

PHP - Drupal tags/keywords

array, form, function, if, isset, node, page, php, return, the, title, to, type, variable

The page_title.module Drupal example source code

<?php
// $Id: page_title.module,v 1.21.4.14 2010/11/02 00:04:24 njt1982 Exp $

/**
 * @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/config/search/page-title' :
      $output  = '<p>'. t('Page Title provides control over the &lt;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&hellip;</em></strong> link below if you need further assistance.') .'</p>';
      $output .= '<p>'. l(t('More Help...'), 'admin/help/page_title') .'</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>[current-page:page-title]</samp> placeholder however if it is left blank the <samp>[current-page:page-title]</samp> token will inherit the item\'s own title.') .'</p>';
      $output .= '<p>'. t('The <samp>[current: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/config/search/page-title'] = array(
    'title' => 'Page titles',
    'description' => 'Configure the page titles for your site (the title in the &lt;head&gt; tag).',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('page_title_admin_settings'),
    'access arguments' => array('administer page titles'),
    'type' => MENU_NORMAL_ITEM,
    '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_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().
 * (cant use hook_form_FORM_ID_alter(). here as the form ID changes from node to node)
 */
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;

  // If we're editing a node...
  if (!empty($form['#node_edit_form'])) {
    // ... and the show field is enabled for this node type
    if (variable_get('page_title_type_'. $form['type']['#value'] .'_showfield', 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('Provide a description of this node to appear in the &lt;title&gt; tag which search engines can use in search result listings (optional). It is generally accepted this field should be less than 70 characters.'),
        '#default_value' => $page_title,
        '#size' => 60,
        '#maxlength' => 255,
      );
    }
  }
}


/**
 * Implement hook_form_FORM_ID_alter().
 */
function page_title_form_user_profile_form_alter(&$form, $form_state) {
  // 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 the user profile form has the show field enabled
  if (variable_get('page_title_user_showfield', 0)) {
    $form['account']['page_title'] = array(
      '#type' => 'textfield',
      '#title' => t('Page title'),
      '#description' => t('Provide a description of this user to appear in the &lt;title&gt; tag which search engines can use in search result listings (optional). It is generally accepted this field should be less than 70 characters.'),
      '#default_value' => page_title_load_title($form['#user']->uid, 'user'),
      '#size' => 60,
      '#maxlength' => 255,
      '#weight' => 20,
    );
  }
}


/**
 * Implement hook_form_FORM_ID_alter().
 */
function page_title_form_taxonomy_form_term_alter(&$form, $form_state) {
  // If we dont have permission to set the title then we need to abort this alter now!
  if (!user_access('set page title')) return;

  // For some reason, the term delete confirm form is run through the taxonomy_form_term form - skip if this is case!
  //if ($form['delete']['#value'] === TRUE) return;

  // Check the term's vocab has the show field enabled
  if (variable_get('page_title_vocab_'. $form['#vocabulary']->vid .'_showfield', 0)) {
    $form['advanced']['page_title'] = array(
      '#type' => 'textfield',
      '#title' => t('Page title'),
      '#description' => t('Provide a description of this term to appear in the &lt;title&gt; tag which search engines can use in search result listings (optional). It is generally accepted this field should be less than 70 characters.'),
      '#default_value' => isset($form['tid']) ? page_title_load_title($form['tid']['#value'], 'term') : '',
      '#size' => 60,
      '#maxlength' => 255,
      '#weight' => -20,
    );
  }
}


/**
 * Implement hook_form_FORM_ID_alter().
 */
function page_title_form_forum_form_container_alter(&$form, $form_state) {
  $forum_vid = variable_get('forum_nav_vocabulary', 0);

  // Check the forum vocab has the show field enabled
  if (variable_get('page_title_vocab_'. $forum_vid .'_showfield', 0)) {
    $form['page_title'] = array(
      '#type' => 'textfield',
      '#title' => t('Page title'),
      '#description' => t('Provide a description of this forum to appear in the &lt;title&gt; tag which search engines can use in search result listings (optional). It is generally accepted this field should be less than 70 characters.'),
      '#default_value' => isset($form['tid']) ? page_title_load_title($form['tid']['#value'], 'term') : '',
      '#size' => 60,
      '#maxlength' => 255,
      '#weight' => -20,
    );
  }
}


/**
 * Implement hook_form_FORM_ID_alter().
 * (We can re-use the above function)
 */
function page_title_form_forum_form_forum_alter(&$form, $form_state) {
  page_title_form_forum_form_container_alter($form, $form_state);
}


/**
 * Implement hook_form_FORM_ID_alter().
 */
function page_title_form_node_type_form_alter(&$form, $form_state) {
  // If we dont have permission to administer the title then we need to abort this alter now!
  if (!user_access('administer page title')) return;

  // Add the node-type specific page title settings to the additional settings section
  $form['page_title'] = array(
    '#type' => 'fieldset',
    '#title' => t('Page Title Settings'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#tree' => TRUE,
    '#group' => 'additional_settings',
  );

  $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' => variable_get('page_title_type_'. $form['#node_type']->type .'_showfield', 0) ? array('show_field') : array(),
  );

  $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';
}


/**
 * Submit handler for the node_type_form element added in the hook_form_FORM_ID_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
 *
 * @param $raw
 *   Optionally set the function to return a result not cleaned by filter_xss. This should be used with caution.
 *
 * @return
 *   string the title for the current page
 */
function page_title_get_title($raw = FALSE, $flush = FALSE) {
  $title = &drupal_static(__FUNCTION__);

  if ($flush || is_null($title)) {
   // Give other modules the oppertunity to use hook_page_title_alter().
    drupal_alter('page_title', $title);
  }

  // Return the title
  return $raw ? $title : filter_xss($title, array());
}


/**
 * 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) {
  $stored_title = &drupal_static(__FUNCTION__);

  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
 *
 * @param $raw
 *   Optionally get the result without cleaning it with filter_xss. This should be used with caution.
 *
 * @return
 *   string The page's title.
 */
function page_title_page_get_title($raw = FALSE) {
  $title = &drupal_static(__FUNCTION__);

  if (is_null($title)) {
    $types = array('global' => NULL);

    // Allow hook_page_title_pattern_alter() to modify the pattern and tokens
    drupal_alter('page_title_pattern', $page_title_pattern, $types);

    // If pattern is empty (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', '[current-page: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);
  }

  // Trim trailing whitespace from the title
  $title = trim($title);

  return $raw ? $title : filter_xss($title);
}


/**
 * Implement hook_preprocess_html().
 */
function page_title_preprocess_html(&$vars) {
  $vars['head_title'] = page_title_page_get_title();
}


/**
 * Implement hook_init().
 */
function page_title_init() {
  // Include relevant page_title.inc's
  foreach (page_title_get_module_apis() as $module => $info) {
    if (file_exists("./{$info['path']}/{$module}.page_title.inc")) {
      include "./{$info['path']}/{$module}.page_title.inc";
    }
  }
}


/**
 * Get a list of modules that support the current Page Title API.
 */
function page_title_get_module_apis() {
  $cache = &drupal_static(__FUNCTION__);

  if (!isset($cache)) {
    $cache = array();
    foreach (module_implements('page_title_api') as $module) {
      $function = $module . '_page_title_api';
      $info = $function();
      if (isset($info['api']) && $info['api'] == 1.000) {
        if (!isset($info['path'])) {
          $info['path'] = drupal_get_path('module', $module);
        }
        $cache[$module] = $info;
      }
    }
  }

  return $cache;
}


/**
 * Implementation of hook_page_title_api().
 */
function page_title_page_title_api() {
  return array(
    'api' => 1,
    'path' => drupal_get_path('module', 'page_title') .'/modules',
  );
}

/**
 * Core implementations of hook_page_title_api().
 */
function taxonomy_page_title_api() { return page_title_page_title_api(); }
function node_page_title_api() { return page_title_page_title_api(); }
function comment_page_title_api() { return page_title_page_title_api(); }
function forum_page_title_api() { return page_title_page_title_api(); }
function user_page_title_api() { return page_title_page_title_api(); }
function views_page_title_api() { return page_title_page_title_api(); }

Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal page_title.module source code file:

new blog posts

"Drupal" is a registered trademark of Dries Buytaert.

my drupal tutorials and examples  

Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

Beginning in 2016, a portion of the proceeds from pages under the '/drupal-code-examples/' URI will be donated to charity.