Any one who uses Taxonomy Image may feel free to submit their application here. Please contact NancyW.

Some of the add-on modules that are distributed with Taxonomy Image began as these custom applications. Please check them out before modifying or writing code.

Many people have asked how to do this, so here's how I do it. As always, YMMV.

One of the groups that I maintain the site for wanted a specialized newsletter. The articles are classified with a vocabulary and they asked for picturized links long ago. I modified the module for them by adding a hook_link_alter.

function pinkslip_link_alter(&$node, &$links) {
  if ($node->type != 'pinkslip') { return; } /* only do ours */
  foreach ($links AS $module => $link) {
    if (strstr($module, 'taxonomy_term')) {
      if (variable_get('pinkslip_display_term_list', FALSE)) {  /* are we showing the terms? */
        // Link back to my display and not the taxonomy term page
        $tid = substr($module,14);
        $term = taxonomy_get_term($tid);
        $tname = $term->name;
        $links[$module]['href'] = "pinkslip/term/$tid";
        $links[$module]['attributes']['class'] = 'pinkslip_terms';
        // See if we have a taxonomy image associated with this term
        $taxo_image = taxonomy_image_display($term->tid);
        if ($taxo_image) {
          $links[$module]['title'] = $taxo_image;
          $links[$module]['html'] = TRUE;   /* if we don't do this, it will come out as text */
        } /* end image insertion */
      } /* end if display_term */
      else { /* we don't want terms shown */
        $links[$module]['title'] = "";  /* we'll just blank it out */
      } /* end else */
    } /* end if taxonomy_term */
  } /* end foreach */
}

In this example, we check to see if the link is from the taxonomy module. If so, then we change the link to point back to the module's processing functions. There are several pieces to doing this as you can see. This code can probably be generalized for an entire site without much difficulty.

There may be a better way to do this; I wrote it a long time ago when I was just beginning to write modules.

Now available! As I was writing up this example, it struck me that a lot of people might want something like this. So I sat down and generalized this code a bit and made it available as a contributed add-on feature for Taxonomy Image. When you download the TI module now, this code will be available for you to enable and select which types of nodes it should be used for. Enjoy!

Linking to Full Sized Image

In the Glossary module, I wanted the image to link to a full-sized image from the regular thumbnail, so I used the following code (simplified a bit).

  if (module_exists('taxonomy_image') && $show_desc) {
    $img = taxonomy_image_display($term->tid);
    if ($img) {
      $obj = taxonomy_image_get_object($term->tid);
      $img = '<a href="'. $obj->url .'" target="_blank">'. $img .'</a>';
    }
  }

A module usage matrix

I maintain somewhere around 16 sites, some live, some test sites. Each site has its own set of contributed modules. During this time of transition from 5.x to 6.x, I needed some help keeping track of which modules have already been converted to 6.x so I know whether the site can be converted.

I struggled for a while about how to organize and present the matrix. I played with the idea of using a vocabulary to define either the sites or the modules, but how to show whether the module was in use on a site, and if so, did it have an upgrade available yet. Suddenly it dawned on me that if I used Taxonomy Image's recursive image display I could create three main terms ("Available," "Unavailable," and "Unknown") and then place each module under one of those (single hierarchy). As the module's status changed, it would be a simple matter of changing its parent term. The recursive display feature allows me to define a single image for the parent terms and then each module under it would use that image.

This idea greatly simplified the whole process to the point that I could create the entire matrix in a PHP page rather than create a module. (Okay, for those militant no-PHP-code-in-pages people, yes, I have the Util module and will probably go ahead and move the code there; but this is a completely isolated site just for me.)

I created a new content type called "site" in which I could use the title for the site name and the body for a brief description of the site. The vocabulary attaches itself by saying that it pertains to the "site" type.

There was one small flaw though: I don't use the recursive feature on the site. I also didn't want the wrapper division attached, which I do use on the site. So I did what any self-respecting module developer would do: I changed Taxonomy Image to allow this. (Do I hear "Kobayashi Maru?"). While I was at it, I wanted to reduce the space needed, so I added a new feature to the Helpers module too.

<?php
  $vid = 10;  //  <<---- the "modules" vocabulary.
  $sites = array();
  $sites = db_result_array(db_query("SELECT n.nid, n.title FROM {node} n WHERE n.type='site' and n.status=1"));

  $terms = array();
  $header = array();
  $cols = db_result_array(db_query("SELECT tid, name FROM {term_data} WHERE vid=%d AND name<>'available' AND name<>'unavailable' AND name<>'unknown' ORDER BY name ASC", $vid));
  $cols[0] = ' Site Name';
  asort($cols);
  foreach($cols as $tid => $name) {
    $terms[] = $name;
    if ($name == ' Site Name') {
      $header[] = array('data' => '<strong>Site Name</strong>', 'align' => 'center');
    }
    else {
      $header[] = array('data' => l(theme('vertical', $name), 'taxonomy/term/'. $tid, array(), null, null, false, true),
        'valign' => 'top', 'align' => 'center', 'width' => '30px', 'class' => 'monospace vertical');
    }
  }

  $rows = array($header);
  foreach ($sites as $sitenid => $sitename) {
    $row = array();
    $cols = array_fill(0, count($terms), null);
    $node = node_load($sitenid);
    $desc = $node->body;
    $cols[0] = l($sitename, 'node/'. $sitenid) .'<br/><small>'. $desc .'</small>';
    
    $using = taxonomy_node_get_terms($sitenid);
    foreach($using as $tid => $term) {
      $i = array_search($term->name, $terms);
      $img = taxonomy_image_display($tid, null, null, array('wrapper' => false,'recursive' => true));
      $cols[$i] = array('data' => $img, 'align' => 'center');
    }
    $rows[] = $cols;
  }
  echo theme('table', null, $rows, array('border' => '1'));

?>

I think it came out pretty slick. (Click on the picture to see it full size.) With Taxonomy Image inserting the term description into the "title" attribute, hovering over a mark tells me the status of the module. Clicking on a module name at the top shows me all the sites using that module. Only local images are allowed.

Adding the image to the title of a taxonomy/term/xx page

This was asked for as a support request. The user wanted to add the taxonomy image to the top of a taxonomy/term/xx page. This requires a bit of theme fiddling.

For information on changing your themes, I suggest the handbook section: PHPTemplate theme engine

  1. Create a copy of your page.tpl.php and name it page-taxonomy-term.tpl.php.
  2. Locate where the title is being formatted (usually with an <h1> tag).
  3. For example, Bluemarine has: <h1 class="title"><?php print $title ?></h1>

  4. Replace this line with this code:
  5.         <?php if (module_exists('taxonomy_image')) {
                    $img = taxonomy_image_display(arg(2));
                  }
                  else {
                    $img = null;
                  }  ?>
            <h1 class="title"><?php print $img . $title ?></h1>
            <div class="clear-block"></div>
    
  6. Save the code.

You should be in business now.

 

Comments

Sinan Erdem’s picture

About the part: "Adding the image to the title of a taxonomy/term/xx page", is it possible to put the image on a sidebar instead of the main content area?

NancyDru’s picture

There is an add-on module for TI that will show the terms of a node in a block.

Sinan Erdem’s picture

I think that add-on module just shows the image on a node page, but I wanted to show it also on a term(category) page. I just did it with Views. If anyone is interested, I just added a Term ID argument to a view, provided default argument, php code:

return array(arg(2));

and as a field I choose Taxonomy: term image

NancyDru’s picture

Well, then I can recommend Taxonomy Context or Taxonomy List