按名称获取词汇表id

anr*_*ru 15 drupal drupal-6

我可以直接从DB检索词汇表id,但是有内置函数吗?

例如:

我有一个名为"listing"的词汇表,我需要内置函数将"listing"作为函数参数,并返回一个vid.

我正在使用drupal 6

Jer*_*nch 17

我有这个功能,差不多......

 /**
 * 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;
}
Run Code Online (Sandbox Code Playgroud)

如果你想让vid只获取返回对象的vid属性.

$vocab_object = mymodule_get_vocabulary_by_name("listing");
$my_vid = $vocab_object->vid;
Run Code Online (Sandbox Code Playgroud)

Henriks指出将其存储在变量中非常有效,因为您不希望在每个请求上运行上述代码.

编辑

另外值得注意的是,在Drupal 7中你可以使用taxonomy_vocabulary_get_names(),这使得它更容易一些.


hug*_*hor 15

对于Drupal 7,如果您知道词汇表机器名称,则这样:

$vid = taxonomy_vocabulary_machine_name_load('your_vocabulary_name')->vid;
Run Code Online (Sandbox Code Playgroud)

如果您只知道词汇的真实姓名,则可以使用此功能:

function _get_vocabulary_by_name($vocabulary_name) {
  // Get vocabulary by vocabulary name.
  $query = db_select('taxonomy_vocabulary', 'tv');
  $query->fields('tv', array(
    'machine_name',
    'vid',
  ));
  $query->condition('tv.name', $vocabulary_name, '=');
  $vocabulary = $query->execute()->fetchObject();
  return $vocabulary;
}
Run Code Online (Sandbox Code Playgroud)