Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get vocabulary id by name

I can retrieve a vocabulary id directly from DB, but is there a built in function for this?

for example:

i have a vocabulary called "listing", i need that built in function takes "listing" as function argument, and return a vid.

i am using drupal 6

like image 581
anru Avatar asked Oct 28 '09 20:10

anru


1 Answers

I have a function for this, well almost..

 /**
 * This function will return a vocabulary object which matches the
 * given name. Will return null if no such vocabulary exists.
 *
 * @param String $vocabulary_name
 *   This is the name of the section which is required
 * @return Object
 *   This is the vocabulary object with the name
 *   or null if no such vocabulary exists
 */
function mymodule_get_vocabulary_by_name($vocabulary_name) {
  $vocabs = taxonomy_get_vocabularies(NULL);
  foreach ($vocabs as $vocab_object) {
    if ($vocab_object->name == $vocabulary_name) {
      return $vocab_object;
    }
  }
  return NULL;
}

If you want the vid just get the vid property of the returned object and.

$vocab_object = mymodule_get_vocabulary_by_name("listing");
$my_vid = $vocab_object->vid;

Henriks point about storing it in a variable is very valid as the above code you won't want to be running on every request.

Edit

Also worth noting that in Drupal 7 you can use taxonomy_vocabulary_get_names() which makes this a little easier.

like image 52
Jeremy French Avatar answered Sep 25 '22 23:09

Jeremy French