You are here

taxonomy_title.module in Taxonomy Title 7

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

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

File

taxonomy_title.module
View source
<?php

/**
 * @file
 * Enhanced control over the heading tag for the taxonomy term list pages.
 */

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

/**
 * Implements hook_theme().
 */
function taxonomy_title_theme($existing, $type, $theme, $path) {
  $theme = array(
    'taxonomy_title_admin_settings' => array(
      'render element' => 'form',
      'file' => 'taxonomy_title.theme.inc',
    ),
  );
  return $theme;
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function taxonomy_title_form_taxonomy_form_term_alter(&$form, &$form_state) {
  $title = taxonomy_title_get($form['tid']['#value'], FALSE);
  $alt = 'the term name';
  if ($global_default = variable_get('taxonomy_title_default_' . $form['#term']['vid'], FALSE)) {
    $alt = 'the global default pattern of <em>' . token_replace($global_default, array(
      'term' => $form_state['term'],
    )) . '</em>';
  }
  $form['taxonomy_title'] = array(
    '#type' => 'textfield',
    '#title' => t('Term page heading'),
    '#default_value' => $title,
    '#description' => t('If left blank !alt will be used.', array(
      '!alt' => $alt,
    )),
    '#weight' => -4,
  );
}

/**
 * Implements hook_preprocess_page().
 *
 * Overrides variables sent to template_preprocess.
 */
function taxonomy_title_process_page(&$variables) {
  if (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2)) && arg(2) > 0 && !arg(3)) {
    $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)) {
      $term = taxonomy_term_load($tid);
      $heading_settings = variable_get('taxonomy_title_headings', array());
      $page_title_settings = variable_get('taxonomy_title_page_titles', array());

      // If there no setting, assume setting is ON.
      $affect_heading = !isset($heading_settings[$term->vid]) || $heading_settings[$term->vid] != 0;
      $affect_title = !isset($page_title_settings[$term->vid]) || $page_title_settings[$term->vid] != 0;

      // Set the page heading.
      if ($affect_heading) {
        $variables['title'] = $title;
      }

      // Set the HTML title tag.
      $site_name = variable_get('site_name', 'Drupal');
      if ($affect_title && !module_exists('page_title') && !module_exists('metatag')) {
        $head_title = array(
          $title,
          $site_name,
        );
        $variables['head_title'] = implode(' | ', $head_title);
      }
      else {

        // Unset things, just to be safe.
        $title = check_plain($term->name);
        $head_title = array(
          $title,
          $site_name,
        );
        $variables['head_title'] = implode(' | ', $head_title);
      }
      drupal_set_title($title);
    }
  }
}

/**
 * Implements hook_taxonomy_term_insert().
 */
function taxonomy_title_taxonomy_term_insert($term) {
  if (isset($term->taxonomy_title)) {
    taxonomy_title_insert($term->tid, $term->taxonomy_title);
  }
}

/**
 * Implements hook_taxonomy_term_update().
 */
function taxonomy_title_taxonomy_term_update($term) {
  if (isset($term->taxonomy_title)) {
    taxonomy_title_update($term->tid, $term->taxonomy_title);
  }
  else {
    taxonomy_title_delete($term->tid);
  }
}

/**
 * Implements hook_taxonomy_term_delete().
 */
function taxonomy_title_taxonomy_term_delete($term) {
  taxonomy_title_delete($term->tid);
}

/******************************************************************************
 * CRUD functions
 ******************************************************************************/

/**
 * Retrieves the term title.
 *
 * @param (int) $tid
 *   The taxonomy term id of the term to delete.
 * @param (bool) $default
 *   Whether to return a global default value if no specific value is provided.
 *
 * @return (string)
 *   The taxonomy term title for the term.
 */
function taxonomy_title_get($tid, $default = TRUE) {
  $title = db_query("SELECT title FROM {taxonomy_title} WHERE tid = :tid", array(
    ':tid' => $tid,
  ))
    ->fetchField();
  if ($title == FALSE && $default == TRUE) {

    // Check for global default value.
    $term = taxonomy_term_load($tid);
    $default = variable_get('taxonomy_title_default_' . $term->vid, '');
    $title = token_replace($default, array(
      'term' => $term,
    ));
  }
  if (function_exists('i18n_string_translate')) {
    $title = i18n_string_translate(array(
      'taxonomy_title',
      'term',
      $tid,
      'title',
    ), $title, array(
      'sanitize' => FALSE,
    ));
  }
  return $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)) {
    $id = db_insert('taxonomy_title')
      ->fields(array(
      'tid' => $tid,
      'title' => $title,
    ))
      ->execute();
  }
}

/**
 * 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) {
  db_merge('taxonomy_title')
    ->key(array(
    'tid' => $tid,
  ))
    ->fields(array(
    'title' => $title,
  ))
    ->execute();

  // Add Suppot for i18nstrings.
  if (function_exists('i18n_string_update')) {
    i18n_string_update(array(
      '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_delete('taxonomy_title')
    ->condition('tid', $tid)
    ->execute();
}

/******************************************************************************
 * Support for translations
 ******************************************************************************/

/**
 * Implements hook_i18n_string_info()
 */
function taxonomy_title_i18n_string_info() {
  $groups['taxonomy_title'] = array(
    'title' => t('Taxonomy title'),
    'description' => t('Translatable custom page titles for taxonomy terms.'),
    'format' => FALSE,
    // This group doesn't have strings with format
    'list' => FALSE,
    // This group cannot list all strings
    'refresh callback' => 'taxonomy_title_locale_refresh',
  );
  return $groups;
}