You are here

taxonomy_title.module in Taxonomy Title 6

Same filename and directory in other branches
  1. 5 taxonomy_title.module
  2. 7 taxonomy_title.module

Enhanced control over the heading tag for the taxonomy term list pages.

This module gives you control over the page heading. It gives you the chance to provide custom titles for all of your taxonomy term list pages, and tokens for use with the Page title module.

File

taxonomy_title.module
View source
<?php

/**
 * @file
 * Enhanced control over the heading tag for the taxonomy term list pages.
 *
 * This module gives you control over the page heading. It gives you the 
 * chance to provide custom titles for all of your taxonomy term list
 * pages, and tokens for use with the Page title module.
 */

/**
 * Implementation of hook_menu().
 */
function taxonomy_title_menu() {
  $items['admin/settings/taxonomy_title'] = array(
    'title' => 'Taxonomy title',
    'description' => 'Settings for the taxonomy title module.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'taxonomy_title_admin_settings',
    ),
    'access callback' => 'user_access',
    'access arguments' => array(
      'administer taxonomy',
    ),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'taxonomy_title.admin.inc',
  );
  return $items;
}

/**
 * Implementation of hook_form_FORM_ID_alter().
 */
function taxonomy_title_form_taxonomy_form_term_alter(&$form, &$form_state) {
  if (!(isset($_POST['op']) && $_POST['op'] == t('Delete')) || isset($_POST['confirm'])) {
    $title = _taxonomy_title_get($form['tid']['#value']);
    $form['identification']['taxonomy_title'] = array(
      '#type' => 'textfield',
      '#title' => t('Title (page heading)'),
      '#default_value' => $title,
      '#description' => t('This is the title you will see in the heading tag on your taxonomy term page. If left blank, the term name will be used.'),
      '#weight' => 0,
    );
    if ($title !== FALSE) {
      $form['identification']['taxonomy_title_set'] = array(
        '#type' => 'value',
        '#value' => TRUE,
      );
    }
  }
}

/**
 * Implementation of hook_taxonomy().
 */
function taxonomy_title_taxonomy($op, $type, $array = NULL) {
  if ($type == 'term') {
    switch ($op) {
      case 'delete':
        _taxonomy_title_delete($array['tid']);
        break;
      case 'update':
        if (!empty($array['taxonomy_title'])) {
          _taxonomy_title_update($array['tid'], $array['taxonomy_title']);
        }
        if ($array['taxonomy_title_set'] === TRUE && empty($array['taxonomy_title'])) {
          _taxonomy_title_delete($array['tid']);
        }
        break;
      case 'insert':
        if (!empty($array['taxonomy_title'])) {
          _taxonomy_title_insert($array['tid'], $array['taxonomy_title']);
        }
        break;
    }
  }
}

/**
 * Implementation of hook_preprocess_page().
 *
 * Overrides title and head_title variables sent to template_preprocess.
 */
function taxonomy_title_preprocess_page(&$variables) {

  // Get the tid depending on the path.
  if (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2)) && arg(2) > 0) {
    $tid = arg(2);
  }
  elseif (module_exists('uc_catalog') && arg(0) == 'catalog') {
    $tids = explode(' ', arg(1));
    if (is_numeric($tids[0]) && $tids[0] > 0) {
      $tid = $tids[0];
    }
  }
  if (!empty($tid)) {

    // Retrieve the title based on tid.
    $title = _taxonomy_title_get($tid);
    if (!empty($title)) {
      drupal_set_title($title);
      $term = taxonomy_get_term($tid);
      $settings = taxonomy_title_get_settings();
      $heading_settings = $settings['taxonomy_title_headings'];
      $page_title_settings = $settings['taxonomy_title_page_titles'];
      if ($new_title = drupal_get_title()) {

        // Set the heading.
        if ($heading_settings[$term->vid] != 0) {
          $variables['title'] = $new_title;
        }

        // Set the title.
        if ($page_title_settings[$term->vid] != 0 && !module_exists('page_title')) {
          $head_title = array(
            strip_tags($new_title),
            variable_get('site_name', 'Drupal'),
          );
          $variables['head_title'] = implode(' | ', $head_title);
        }
      }
    }
  }
}

/**
 * Retrieves the term title.
 *
 * @param $tid
 *   The taxonomy term id of the term to delete.
 * @return
 *   The taxonomy term title for the term.
 */
function _taxonomy_title_get($tid) {
  $title = db_result(db_query("SELECT title FROM {taxonomy_title} WHERE tid = %d", $tid));
  return taxonomy_title_tt("taxonomy_title:term:{$tid}:title", $title);
}

/**
 * Inserts the term title.
 *
 * @param $tid
 *   The taxonomy term id of the term.
 * @param $title
 *   The taxonomy term title to use for this term.
 */
function _taxonomy_title_insert($tid, $title) {
  if (!empty($title)) {
    db_query("INSERT INTO {taxonomy_title} (tid, title) VALUES (%d, '%s')", $tid, $title);
    if (function_exists('i18nstrings_update')) {
      i18nstrings_update("taxonomy_title:term:{$tid}:title", $title);
    }
  }
}

/**
 * Updates the term title.
 *
 * @param $tid
 *   The taxonomy term id of the term.
 * @param $title
 *   The taxonomy term title to use for this term.
 */
function _taxonomy_title_update($tid, $title) {
  if (db_result(db_query("SELECT title FROM {taxonomy_title} WHERE tid = %d", $tid))) {
    db_query("UPDATE {taxonomy_title} SET title = '%s' WHERE tid = %d", $title, $tid);
  }
  else {
    db_query("INSERT INTO {taxonomy_title} (tid, title) VALUES (%d, '%s')", $tid, $title);
  }

  // Add Suppot for i18nstrings.
  if (function_exists('i18nstrings_update')) {
    i18nstrings_update("taxonomy_title:term:{$tid}:title", $title);
  }
}

/**
 * Deletes the term title.
 *
 * @param $tid
 *   The taxonomy term id of the term to delete.
 */
function _taxonomy_title_delete($tid) {
  db_query("DELETE FROM {taxonomy_title} WHERE tid = %d", $tid);

  // Add Suppot for i18nstrings.
  if (function_exists('i18nstrings_remove')) {
    i18nstrings_remove($name);
  }
}

/**
 * Helper function: sets all default usage to ON.
 */
function taxonomy_title_get_settings() {
  $vocabs = taxonomy_get_vocabularies();
  $heading_defaults = variable_get('taxonomy_title_headings', array());
  $page_title_defaults = variable_get('taxonomy_title_page_titles', array());
  foreach ($vocabs as $vid => $vocab) {
    if (!isset($heading_defaults[$vid])) {
      $heading_defaults[$vid] = $vid;
    }
    if (!isset($page_title_defaults[$vid])) {
      $page_title_defaults[$vid] = $vid;
    }
  }
  $settings = array(
    'taxonomy_title_headings' => $heading_defaults,
    'taxonomy_title_page_titles' => $page_title_defaults,
  );
  return $settings;
}

/**
 * Translate user defined string. Wrapper function for tt() if i18nstrings enabled.
 * 
 * @param $name
 *   String id in the form taxonomy_title:term:[tid]:title
 */
function taxonomy_title_tt($name, $string, $langcode = NULL) {
  return function_exists('i18nstrings') ? i18nstrings($name, $string, $langcode) : $string;
}

/**
 * Implementation of hook_locale().
 */
function taxonomy_title_locale($op = 'groups', $group = NULL) {
  switch ($op) {
    case 'groups':
      return array(
        'taxonomy_title' => t('Taxonomy title'),
      );
    case 'info':
      $info['taxonomy_title']['refresh callback'] = 'taxonomy_title_locale_refresh';
      return $info;
  }
}

/**
 * Refresh callback for locale strings.
 */
function taxonomy_title_locale_refresh() {
  $results = db_query("SELECT tid, title FROM {taxonomy_title}");
  while ($row = db_fetch_object($results)) {
    i18nstrings_update("taxonomy_title:term:{$row->tid}:title", $row->title);
  }

  // Meaning it completed with no issues. @see i18nmenu_locale_refresh().
  return TRUE;
}

/**
 * Implementation of hook_token_list().
 */
function taxonomy_title_token_list($type = 'all') {
  if ($type == 'taxonomy' || $type == 'all') {
    $tokens['taxonomy']['term-title'] = t("The term's title, defaults to term name (same as [cat]) if no title provided.");
    $tokens['taxonomy']['term-title-raw'] = t("The raw component of the term's title, WARNING - raw user input.");
    return $tokens;
  }
}

/**
 * Implementation of hook_token_values().
 */
function taxonomy_title_token_values($type, $object = NULL, $options = array()) {
  $values = array();
  if ($type == 'taxonomy') {
    if (!empty($object)) {
      $category = $object;

      // Use taxonomy title if it exists, else the category name.
      $title = _taxonomy_title_get($category->tid);
      $values['term-title'] = $title ? check_plain($title) : check_plain($category->name);
      $values['term-title-raw'] = $title ? $title : $category->name;
    }
  }
  return $values;
}

/**
 * Implements hook_theme().
 */
function taxonomy_title_theme() {
  $theme = array(
    'taxonomy_title_admin_settings' => array(
      'arguments' => array(
        'form' => NULL,
      ),
      'file' => 'taxonomy_title.admin.inc',
    ),
  );
  return $theme;
}

Functions

Namesort descending Description
taxonomy_title_form_taxonomy_form_term_alter Implementation of hook_form_FORM_ID_alter().
taxonomy_title_get_settings Helper function: sets all default usage to ON.
taxonomy_title_locale Implementation of hook_locale().
taxonomy_title_locale_refresh Refresh callback for locale strings.
taxonomy_title_menu Implementation of hook_menu().
taxonomy_title_preprocess_page Implementation of hook_preprocess_page().
taxonomy_title_taxonomy Implementation of hook_taxonomy().
taxonomy_title_theme Implements hook_theme().
taxonomy_title_token_list Implementation of hook_token_list().
taxonomy_title_token_values Implementation of hook_token_values().
taxonomy_title_tt Translate user defined string. Wrapper function for tt() if i18nstrings enabled.
_taxonomy_title_delete Deletes the term title.
_taxonomy_title_get Retrieves the term title.
_taxonomy_title_insert Inserts the term title.
_taxonomy_title_update Updates the term title.