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

Henrik Opel
  • 19,138
  • 1
  • 45
  • 61
anru
  • 1,233
  • 3
  • 16
  • 29

6 Answers6

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.

Jeremy French
  • 10,555
  • 4
  • 43
  • 68
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;
}
hugronaphor
  • 835
  • 7
  • 23
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
 }
Jeremy French
  • 10,555
  • 4
  • 43
  • 68
Henrik Opel
  • 19,138
  • 1
  • 45
  • 61
  • 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. – Jeremy French Oct 29 '09 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 '12 at 04:12
  • misspelling, should be `taxonomy_vocabulary_save` – Sswater Shi Feb 08 '14 at 03:14
  • @SswaterShi: This answer and code where aimed at Drupal 6, where it was [taxonomy_save_vocabulary](https://api.drupal.org/api/drupal/modules!taxonomy!taxonomy.module/function/taxonomy_save_vocabulary/6). – Henrik Opel Feb 10 '14 at 18:02
3

Should help.

function _my_module_vid($name) {
  $names = taxonomy_vocabulary_get_names();
  return $names[$name]->vid;
}
  • This should work, but what about an isset check and returning FALSE if there is no vocab with that name ? – AKS Oct 26 '12 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!

Bhavin Joshi
  • 504
  • 3
  • 12
0

In Drupal 7 you could use:

$vocab_object = taxonomy_vocabulary_machine_name_load('vocabulary_name');
$my_vid = $vocab_object->vid;
digitaldonkey
  • 2,341
  • 1
  • 18
  • 15