By Levure on
Hello !
Since I can't assume that every drupal site wich will install my module will have the same vocabulary id, but I can assume that they will have the same vocabulary name.
How can I get the vid (vocabulary id) of a taxonomy vocabulary, based on the vocabulary name ?
Many thanks in advance !
Comments
Vocabulary names are a shaky
Vocabulary names are a shaky foundation because the admin can edit them at any time in admin/content/taxonomy. Maybe that is why the API doesn't make it easy to use them in the way you want.
One idea is to do what the forum module does:
(a) when it creates its vocabulary during installation, it saves its vid in the variable table (forum_nav_vocabulary)
(b) It alters the vocabulary edit form and removes the "Delete" button (as well as some options), so that the admin can't delete it without uninstalling the module (people often deleted it and created a new one which didn't work because of the different vid).
Although I agree completely
Although I agree completely with cog.rusty, if you must get the vocabulary id from the name, you can do it with taxonomy_get_vocabularies(). This function will retrieve all of the vocabularies used on the site (or if you specify node-type, all the vocabularies of a particular node-type).
function
I like it, but I have a work around for the "shakiness"
There are a couple of comments on this point of "shakiness". It is true that the Administrator can overwrite the vocabulary, but the vid will not change (or at least I'm pretty sure that it won't)... I'm in the same situation that is described here. What to do think of this work around code? I tried it with a module that I'm developing which creates vocabulary items upon installation and so I needed to know the vid to do anything with the terms, etc. So far it seems to be working fine. The only issue may be that in storing the variable as a system variable in this fashion the vid changes from an integer/serial to string, but I tried it out with another function that had only the passed system variable as a reference to the vid number and that function worked properly. Granted, this solution might not be ideal for large numbers of vocabulary terms, but still I'd like to know if there are any reasons why this won't be a good practice.
function _get_vid($vocabulary_name) {
// replace dashes with spaces
$vocabulary_name = str_replace('-', ' ', $vocabulary_name);
// find the vid
$result = db_query("SELECT vid FROM {vocabulary} WHERE name = '%s'", $vocabulary_name);
$vid = db_fetch_object($result)->vid;
if($vid) {
//by storing the vid in the system variables table, we create a 'hard lock'
variable_set('myVocabularID',$vid);
return TRUE
}
else {
return FALSE;
}
}
not bad
I like your idea and I used something similar below:
The %s ($vocab_name) is passed in through arg(0) in the url...
$query = 'SELECT vocabulary.vid AS vocabulary_id
FROM vocabulary
WHERE vocabulary.name= "%s"
LIMIT 1';
$vocab_id = 0;
$result = db_query($query, $vocab_name);
while ($r = db_fetch_object($result)) {
//print $r->vocabulary_id. "
";
$vocab_id = $r->vocabulary_id;
}
print $vocab_id;
while this works, its shaky because in my scenario, there will only ever be one instance of a vocabulary name but what if there were two then my limit 1 would break and it would just return the first result it picks up...
i am open to ideas for better ways of doing this...if i find one, ill post it up
In Drupal 7, use
In Drupal 7, use taxonomy_vocabulary_machine_name_load().
-Wes
To just get the VID use this
In D7
If all you need is the vocabulary ID (vid) and you know the machine name, you can use:
Small performance increase: ~0.0036489963531494 seconds to ~0.00030779838562012 seconds.
This, of course, could be tailored as needed. Just change the condition to what you have.
If you're looking at
If you're looking at performance increases, then a straight-up db_query() will be the fastest:
Contact me to contract me for D7 -> D10/11 migrations.
In Drupal 7
It is possible to get the taxonomy vid by using the function: 'taxonomy_vocabulary_get_names'.
This function will provide you the array of objects of each taxonomy term used in your drupal 7 setup.
Drupal 7 : if you know the
Drupal 7 : if you know the vocabulary machine name Then this will works.
$vid = taxonomy_vocabulary_machine_name_load('your_vocabulary_name')->vid;
Thanks,
Rahul Baisane.
Drupal 7 : if you know the
Drupal 7 : if you know the vocabulary machine name Then this will works.
$vid = taxonomy_vocabulary_machine_name_load('your_vocabulary_name')->vid;
Thanks,
Rahul Baisane.