drupal - 按名称获取词汇 ID

标签 drupal drupal-6

我可以直接从数据库检索词汇表ID, 但是有没有内置的函数可以做到这一点?

例如:

我有一个词汇叫做“listing”, 我需要内置函数将“列表”作为函数参数,然后返回 一个视频。

我正在使用 drupal 6

最佳答案

我有一个函数,几乎......

 /**
 * 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;
}

如果您想要 vid,只需获取返回对象的 vid 属性即可。

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

Henrik关于将其存储在变量中的观点非常有效,因为您不希望在每个请求上运行上述代码。

编辑

还值得注意的是,在 Drupal 7 中您可以使用 taxonomy_vocabulary_get_names()这使得这变得更容易一些。

关于drupal - 按名称获取词汇 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1639932/

相关文章:

php - 是否可以通过javascript调用自定义模块中的drupal函数?

php - Drupal - 使用 Bootstrap 检查 Drupal 之外的登录用户不工作

drupal - 用于过滤结果的搜索 Hook ?

php - Drupal 表单 API 和 $form_state ['storage' ] 在页面刷新时被销毁

drupal - 更改 Drupal 中的节点创建标题?

drupal - View 中打开 2 个寻呼机 - Drupal 6

Drupal:显示成功/错误消息

drupal - 如何将 lm_paypal 订阅添加到注册页面?

php - 最大函数嵌套级别在 Drupal 中达到错误

drupal-7 - 将 HTML 添加到 Drupal 闭包?