You are here

function taxonomy_image_link_alter in Taxonomy Image 5

Same name and namespace in other branches
  1. 6 contributed/taxonomy_image_link_alter/taxonomy_image_link_alter.module \taxonomy_image_link_alter()

Implement hook_link_alter to picturize the taxonomy term links

2 string references to 'taxonomy_image_link_alter'
taxonomy_image_help in ./taxonomy_image.module
Implementation of hook_help.
taxonomy_image_views_tables_alter in ./taxonomy_image.module
This module is a small helper module to fix an issue with a view.

File

contributed/taxonomy_image_link_alter/taxonomy_image_link_alter.module, line 10
Change taxonomy links to show term images.

Code

function taxonomy_image_link_alter(&$node, &$links) {

  // We make these static so we only have to get them once per page view.
  static $type_enabled, $show_name, $preset;
  if (!isset($type_enabled)) {
    $type_enabled = variable_get('taxonomy_image_link_types', array());
    $show_name = variable_get('taxonomy_image_link_show_name', 0);
    $preset = variable_get('taxonomy_image_link_preset', 'ORIGINAL');
  }

  // Is the type one we want to change?
  $type_enabled = variable_get('taxonomy_image_link_types', array());
  if (!$type_enabled[$node->type]) {
    return;
  }
  drupal_add_css(drupal_get_path('module', 'taxonomy_image') . '/taxonomy_image.css');
  $tid_list = array();
  foreach ($links as $module => $link) {
    if (strstr($module, 'taxonomy_term')) {

      // Link back to my display and not the taxonomy term page
      $tid_list[] = $tid = substr($module, 14);
      $term = taxonomy_image_get_object($tid);
      $tname = $show_name ? '<br />' . $term->name : NULL;

      // See if we have a taxonomy image associated with this term
      $taxo_image = taxonomy_image_display($term->tid, NULL, $preset, array(
        'wrapper' => FALSE,
      ));
      if ($taxo_image) {
        if (isset($links[$module]['attributes']['class'])) {
          $links[$module]['attributes']['class'] .= ' taxonomy-image-link-alter';
        }
        else {
          $links[$module]['attributes']['class'] = 'taxonomy-image-link-alter';
        }
        $links[$module]['title'] = $taxo_image . $tname;
        $links[$module]['html'] = TRUE;

        /* if we don't do this, it will come out as text */
      }
    }
  }
  $sort_order = variable_get('taxonomy_image_link_sort_links', 0);

  // If we don't want to sort, go back now.
  if ($sort_order == 0) {
    return;
  }
  switch ($sort_order) {
    case 1:

      // Alphabetically
      $sort_fields = array(
        'v.weight',
        'v.name',
        't.weight',
        't.name',
      );
      break;
    case 2:

      // By ID
      $sort_fields = array(
        'v.weight',
        'v.vid',
        't.weight',
        't.tid',
      );
      break;
    default:

      // Should never get here.
      drupal_set_message(t('Unknown sort order (@order) configured.', array(
        '@order' => $sort_order,
      )), 'error');
      return;
  }
  $fields = array();
  foreach ($sort_fields as $index => $field_name) {
    $fields[] = $field_name . ' AS field' . $index;
  }

  // Save the taxo link data into a temporary table.
  // We do this because PHP can't properly sort anything but English.
  $tquery = 'SELECT ' . implode(', ', $fields) . ', t.tid ' . "FROM `term_data` t LEFT JOIN `vocabulary` v USING (vid) WHERE t.tid IN ('" . implode("', '", $tid_list) . "')";
  $result = db_query_temporary($tquery, 'ti_link_alter');

  // Now read them back in the proper order.
  $result = db_query("SELECT * FROM {ti_link_alter} ORDER BY field0, field1, field2, field3");
  $new_links = array();
  while ($row = db_fetch_object($result)) {
    $module = 'taxonomy_term_' . $row->tid;

    // Save the link, then delete the original.
    $new_links[$module] = $links[$module];
    unset($links[$module]);
  }

  // Now put the taxo links back in the new order.
  $links = array_merge($links, $new_links);

  // We need to destroy the table in case there are more nodes on the page.
  db_query("DROP TABLE {ti_link_alter}");
}