Adding fields to Taxonomy?
SteveTurnbull - April 20, 2008 - 22:07
hook_taxonomy allows interception of insert, update and deletion of terms and vocabularies. But to smoothly integrate additional fields for a taxonomy term, say an enable/disable field a term, I can't see any way to intercept a "load" of a term or vocabulary.
I've done lots of research and reading but I can't see any way of doing it.
Any ideas?

use ajax
use ajax
---------------------------------------------------------------------------
Women Lose Weight-->I would love to lose weight!
That's stupid. You should
That's stupid. You should not rely on something unsure as AJAX to build your module on. I'd rather not implement anything than having to rely on AJAX. Javascript is meant to provide additional features that are not necessary for the module to be fully functional. This is because Javascript is only enabled at 80 to 90 per cent of all computers.
@Hellon: what's the spam link doing there?
Ajax is inappropriate
Ajax is inappropriate, that would be a client/server thing; what I want isn't. Obviously I didn't explain myself well enough.
Bit of background: There are some sophisticated taxonomy systems out in the real world, I want to set up a module to import and maintain one of these. As a result the ability to enable and disable a particular taxonomy term, and also store some additional data for each term would be very useful.
I have no problem creating a new table to store this extra information, nor adding stuff to forms and saving new data because hook_taxonomy provides interception of insert, update and delete.
But hook_taxonomy doesn't provide interception of 'load'. Which is very annoying. I can't even use db_rewrite_sql because you can't add to the fields that get loaded.
The only solution I can think of is intercepting hook_nodeapi and check the node's taxonomy and add the data there, seems a bit late in the day.
Does anyone have any better ideas?
subscribing, 'cause 'd like
subscribing, 'cause 'd like to know too!
The solution
There's no solution.
Actually there are 4 ways of adding to {term_data} but all but one of them require hacking of core code:
(a) Use hook_nodeapi, see which terms have been added and then fetch the additional data, no hacking required but only applies to terms loaded into nodes, doesn't apply to terms loaded for any other purpose. Keep additional data in an external file.
(b) Modify db_rewrite_sql to allow the addition of fields to a query. Then you can intercept any query that involves 'tid' and enter the filter and add additional data. I have tried this, it's not hard, and made it work. Of course it potentially influences every query that goes through db_rewrite_sql.
Keep additional data in an external file and when intercepting add the fields we want to load, and add the filter to remove disabled terms.
BUT the most important query that loads terms does NOT go through db_rewrite_sql. So that requires a change in taxonomy.module.
(c) Add a new 'load' op to hook_taxonomy. I didn't try this and it would have to be inserted in several places (about 3) in taxonomy.module. No changes required to db_rewrite_sql, but it would tend to slow down term access quite a lot, and that's one of the most common accesses on a Drupal site.
(d) (My final choice) Add any additional data fields to the {term_data} table itself. Writing data is not a problem and all the SELECT commands in taxonomy use SELECT * so every field will be loaded. BUT in order to implement the disabled filter we still need to modify the taxonomy.module to add a db_rewrite_sql to the one SELECT query that doesn't have it.
So there is no useful option that does not involve a core hack. Option (d) is probably the best, although option (c) would be preferable.
By the way the line you need to change is in function taxonomy_get_term and it should look like this:
<?php$terms[$tid] = db_fetch_object(db_query(db_rewrite_sql('SELECT t.* FROM {term_data} t WHERE t.tid = %d','t','tid'), $tid));
?>
If you just want to add additional data to {term_data} and not do any filtering, then it's easy enough and no hacks are required. It would probably be worth looking at the module taxonomy_access http://drupal.org/project/taxonomy_access .
subscribing
subscribing
Can you edit the data ?
Hi, interesting write up. I see we are going different directions. Can you edit the additional taxonomy data on the site with your solutions ?
Yes, by intercepting
Yes, by intercepting hook_form_alter and hook_taxonomy you can edit these directly. However I must admit I'm now into version 2 of this work and I have simplified the whole thing considerably. But the eseentials remain the same.
subscribing
It's silly that I need to spam the comments to "subscribe", but here we go.
-- NO, don't read this signature!