background image

Content tagged with: title

Eric's picture

If someone clicks on a taxonomy term they land on a page showing all the content tagged with that term. The title on those pages is simply the taxonomy term. I thought it would be more usable if I changed the verbiage of the title to let the user know what they are viewing. This code snippet shows how you can change the title for the taxonomy term landing pages:

<?php
function MYTHEME_preprocess_page(&$variables) {
  if (
arg(0)=='taxonomy' && arg(1)=='term') {
   
$variables['title'] = "Content tagged with: " . $variables['title'];
  }
}
?>

Eric's picture

In a recent project I used the content profile module to improve flexibility in creating user profiles. This module replaces the regular profile creation with a CCK node type. One of the requested profile fields was job title, which is confusing since title is already a required field of a node. In most cases, I would use first and last name as the title, but in this case those fields were required to be separate. Since the node title did not fit into the list of requested fields, I decided to remove it from the form and dynamically generate a node title from the user's first and last name. Here's how this can be accomplished:

<?php
// create a form_alter hook to remove the field from the form:
function MYMODULE_form_alter(&$form, $form_state, $form_id) {

  if (
$form_id == 'profile_node_form') {
   
// remove title from view
   
$form['title']['#access'] = false;
  }
   
}

// create a nodeapi function to create the node title when the node is created
function MYMODULE_nodeapi(&$node, $op, $a3=NULL, $a4=NULL) {
  if (
$node->type =='profile' && $op=='insert') {
   
$node->title = $node->field_profile_name_first[0]['value'] . " " . $node->field_profile_name_last[0]['value'];
   
node_save($node);      
  }
}
?>

Eric's picture

If your site uses the admin_menu & menu_breadcrumb modules, you might have noticed something wrong with your page titles on the /user page, similar to this:

# / # <img src="/sites/all/modules/admin_menu/images/icon_users.png" width="16" height="15" alt="Current anonymous / authenticated users" title="Current anonymous / authenticated users" />

The incorrect HTML is being output from the admin_menu in admin_menu.module on line 286, but it's caused from the menu_breadcrumb module.

As a quick solution, I added some code to my theme template.php file in a preprocess_page function to replace the titles.

<?php
function MYTHEME_preprocess_page(&$variables) {
  if (
stripos($variables['head_title'],'icon_users.png')) {
   
$variables['head_title'] = 'User account | ' . variable_get('site_name', '');
  }
  if (
stripos($variables['title'],'icon_users.png')) {
   
$variables['title'] = 'User account';
  }
}
?>

Eric's picture

In this code snippet I'll explain how to create a menu item that has a dynamic title.

<?php
// define hook_menu to create menu item
function MYMODULE_menu() {
 
$items = array();
   
 
$items['MY/PAGE/URL/%'] = array(
   
'page callback' => 'MY_PAGE_CALLBACK_FUNCTION',
   
'title callback' => 'MY_PAGE_CALLBACK_TITLE_FUNCTION',
   
'title arguments' => array(3),
   
'type' => MENU_CALLBACK,
  );

  return
$items;
}

// define title callback function
function MY_PAGE_CALLBACK_TITLE_FUNCTION($arg) {
  return
"My dynamic title: " . $arg;
}
?>

In the above example, if you browsed to the URL: MY/PAGE/URL/blah-blah-blah, you'd have a page title of: My dynamic title blah-blah-blah. In a more applicable example, you might need to pass the node id to the callback function and then return: $node->title;

Check out the documentation on wildcard loader arguments for more advanced options.

Eric's picture

Here's a code snippet that ensures the submitted title is unique across all nodes.

<?php
function MYMODULE_form_alter($form_id, $form) {
  if (
substr($form_id, -10)=='_node_form') {
   
// add custom form validation function
   
$form['#validate'] = array_merge(array('_MYMODULE_helper_validate' => array()), $form['#validate']);
  }
}

function
_MYMODULE_helper_validate($form_id, $form_values, $form) {
 
// ensure title is unique
 
if (strlen($form_values['title'])) {
   
// check for unique title
   
$sql = "select distinct title from {node} where 1=1 ";
   
// don't want to include current title
   
if ($form_values['nid']) $sql .= "and nid != '" . db_escape_string($form_values['nid']) . "'";
       
   
$resource = db_query($sql);
   
$titles = array();
    while(
$result = db_fetch_array($resource)) $titles[] = $result['title'];
   
    if (
in_array($form_values['title'],$titles))
     
form_set_error('title', $form['title']['#title'] . ' has already been submitted.');
  }
}
?>