16

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

6 Answers 6

17

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.

1
  • D7 users please notice: Use $vocab_object->machine_name instead of ->name !
    – mate64
    Aug 19, 2013 at 11:45
16

For Drupal 7 if you know the vocabulary machine name this is the way:

$vid = taxonomy_vocabulary_machine_name_load('your_vocabulary_name')->vid;

If you know only the Real name of vocabulary, you can use this function:

function _get_vocabulary_by_name($vocabulary_name) {
  // Get vocabulary by vocabulary name.
  $query = db_select('taxonomy_vocabulary', 'tv');
  $query->fields('tv', [
    'machine_name',
    'vid',
  ]);
  $query->condition('tv.name', $vocabulary_name, '=');
  $vocabulary = $query->execute()->fetchObject();
  return $vocabulary;
}
4

There is no built in function for this, afaik. You can roll your own by calling taxonomy_get_vocabularies() and search for your name in the resulting array, but this will do a database request on every call.

If you have a vocabulary that you often use from code, it might be easier/more effective to store the vid in a Drupal variable via variable_set() once and get it back via variable_get() (Many modules that create a vocabulary on install do it this way).

Edit: here is some sample code to do this on module install.

function mymodule_install() {
  $ret = array();
  $vocabulary = array(
      'name' => t('myvocab'),
      'multiple' => '1',
      'required' => '0',
      'hierarchy' => '1',
      'relations' => '0',
      'module' => 'mymodule',
      'nodes' => array('article' => 1), 
    );
  taxonomy_save_vocabulary($vocabulary);
  $vid = $vocabulary['vid'];
  variable_set('mymodule_myvocab', $vid);
  return $ret
 }
4
  • Henrik, I hope you don't mind, but as I had some code to hadn I put a sample in for storing a vocab on install. Oct 29, 2009 at 11:08
  • I was about to suggest taxonomy_get_vocabularies and saw your note regarding another DB call on every request... you're right.
    – Aditya M P
    Apr 13, 2012 at 4:12
  • misspelling, should be taxonomy_vocabulary_save Feb 8, 2014 at 3:14
  • @SswaterShi: This answer and code where aimed at Drupal 6, where it was taxonomy_save_vocabulary. Feb 10, 2014 at 18:02
3

Should help.

function _my_module_vid($name) {
  $names = taxonomy_vocabulary_get_names();
  return $names[$name]->vid;
}
1
  • This should work, but what about an isset check and returning FALSE if there is no vocab with that name ?
    – AKS
    Oct 26, 2012 at 19:12
0

You know the node type to which the vocaulbary is associated. So just use taxonomy_get_vocabularies() and pass the node type as argument and you will get the details you want!

0

In Drupal 7 you could use:

$vocab_object = taxonomy_vocabulary_machine_name_load('vocabulary_name');
$my_vid = $vocab_object->vid;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.