Add Attachment to Taxonomy

sest - April 30, 2008 - 19:41

Hello,
Is it possible to add attachments to a taxonomy term? I search for a while and the only module I found was the http://drupal.org/project/taxonomy_image module. With this Module I can add images to a taxonomy term, but I want to add any type of file to a taxonomy term (like disknode or webfm attachments).
If there is no such module, what is the fastest and best way to get this working? Is it better to use http://drupal.org/project/taxonomy_enhancer to hook the load or view or is it better to copy and refactor taxonomy_image.

Stefan

Generally files attach to

nevets - April 30, 2008 - 20:59

Generally files attach to nodes (content) so I would use a content type that I use taxonomy to classify and attach files to that content.

I'm looking to do something

tcindie - May 7, 2008 - 03:00

I'm looking to do something similar.

In my situation I have some taxonomy terms established that define several properties (business locations, not variables). I'm building a job listing site for the various companies tied to the parent company I work for, so all openings can be published in one place. There are two different types of job application for each property, just a standard application and one that also includes supplemental information that's generally used for more physically demanding jobs (bending, lifting, etc).

I have a custom cck field established so that upon entry of a job listing you can select which property it is applicable for, and which application type is associated (just a 2 choice drop down list).

My aim is to have a link to the correct downloadable job application (pdf) propagate to a block when a node is displayed. That way I can style it however I like and place it pretty much anywhere on the page as well. Ultimately this will also provide a link to the correct online application form, based on which application type is associated with a listing, so that users can fill out a form to apply online.

But the best way I can think of to handle this is to associate a file with a particular taxonomy term, though I guess I haven't fully explored the option of creating a separate node for each application type and pulling the info for the link to the file from that based on association. I suppose if those nodes had their own content type I could create something to just retrieve the information I need.

Messy, but it works

tcindie - May 8, 2008 - 18:26

Well, I've got my particular goal mostly working. It's probably not the cleanest solution, but it's getting there.

Here's what I did:

1. create a new content type for the attachment(s) (In my case this is called downloadable_job_application)
2. Add content of the new type containing the attachments you want associated with a particular term, and assign this to that term -- give it whatever title you want, it won't be shown.
3. Save the new content, and set it as Unpublished (otherwise it will turn up in search results, and you probably don't want that)

4. Create a custom node template for each node type you want these files to be attached to. (In my case this is just those that are of content type job_listing so node-job_listing.tpl.php)

5. Add the following code to the end of your new template file:

<?php
    reset
($node->taxonomy);
   
$obj = current($node->taxonomy);
   
$thistid = (!empty($obj)) ? $obj->tid : 0;

   
$node_type = "";

   
$result = db_query("SELECT n.nid FROM {node} as n, {term_node} as tn where tn.tid=" . $thistid . " and n.nid=tn.nid and n.type=\"" . $node_type . "\"");
   
$results = db_fetch_array($result);

   
$this_node_attachments = node_load($results);
   
$attachments = node_build_content($this_node_attachments, FALSE, FALSE);
   
$content = drupal_render($attachments->content);
    print
$content;
?>

Change $node_type = ""; to contain the machine readable name of the attachment content type you created, so for me that is $node_type = "downloadable_job_application";

If your attachments node has nothing for the body this should just add the Attachments table to the node. As written it will only work with one taxonomy term assigned to the node, which is all I needed in my scenario, but it should get you heading in the right direction.

In my scenario I am using terms to determine which property location a particular job listing is at, and which type of application is required (since there are two types for each property), so I had to also include the custom cck field that determines which application type to use, and I also included location information in the job application nodes that contains address and such for the property...

My sql query looks like this:
$result = db_query("SELECT n.nid FROM {node} as n, {term_node} as tn, {content_field_application} as c where tn.tid=" . $thistid . " and n.nid=tn.nid and n.type=\"" . $node_type . "\" and c.field_application_value=" . $node->field_application[0]['value'] . " and c.nid=n.nid");

As I said, it's messy, but it works.. the issue I'm dealing with now is preventing the attached information from being displayed when it's not the only node being displayed.

 
 

Drupal is a registered trademark of Dries Buytaert.