5

Here is the scenario.

I have a taxonomy term reference field, which is referenced to a taxonomy vocabulary, for example it is associated with category vocabulary. Now I need to fetch the vid of the category vocabulary which is associated to that particular field.

or is there a way to fetch the field settings in drupal 7?

3
  • you have tid and want to get vid
    – Ahmed
    Commented May 15, 2012 at 11:07
  • nope. thats not the case. Commented May 15, 2012 at 11:19
  • you have node that contains a taxonomy ref. and you need to get vid
    – Ahmed
    Commented May 15, 2012 at 11:22

1 Answer 1

13

You can use a combination of field API and taxonomy API functions to get this info:

$field_name = 'field_FIELD_NAME';

// Get the field info
$info = field_info_field($field_name);

// The link to the vocab is stored as it's machine name
$vocab_machine_name = $info['settings']['allowed_values'][0]['vocabulary'];

// Load the vocab by machine name
$vocab = taxonomy_vocabulary_machine_name_load($vocab_machine_name);

// Finally, get the vid
$vid = $vocab->vid;
1
  • 1
    its really awesome. Clive rock!!! Commented May 15, 2012 at 11:59

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.