我如何通过node_type获取分类词汇

发布于 2024-12-22 13:35:46 字数 73 浏览 1 评论 0原文

我的节点类型很少,其中一些有一个与分类词汇相关的额外字段。如果我知道该字段存在的字段名称和节点类型,如何获取分类词汇 ID 或名称?

I have few node types and some of them have an extra field is linked to taxonomy vocabulary. How can i get taxonomy vocabulary id or name if i know field name and node type where this field there exists?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薄荷港 2024-12-29 13:35:46

在 Drupal 7 中,字段可以添加到任何实体/包中,并且对于术语参考字段,使用的词汇表是在字段级别设置的,而不是在实体/包级别上设置的。

因此,您不需要查询特定内容类型的字段设置,而只需查询字段本身的设置。词汇机名称存储在从 field_info_field()

$field_name = 'field_name_of_field';
$info = field_info_field($field_name);

$vocab_keys = array();
foreach ($info['settings']['allowed_values'] as $item) {
  $vocab_keys[] = $item['vocabulary'];
}

// $vocab_keys now contains an array of all vocabulary machine names allowed on this field

希望有帮助

In Drupal 7 fields can be added to any entity/bundle, and for a term reference field the vocabularies used are set on the field level, not on the entity/bundle level.

So you don't need to query the field settings for a particular content type, but simply the settings for the field itself. The vocabulary machine names are stored in the allowed_values key of the settings array returned from field_info_field():

$field_name = 'field_name_of_field';
$info = field_info_field($field_name);

$vocab_keys = array();
foreach ($info['settings']['allowed_values'] as $item) {
  $vocab_keys[] = $item['vocabulary'];
}

// $vocab_keys now contains an array of all vocabulary machine names allowed on this field

Hope that helps

仙气飘飘 2024-12-29 13:35:46

如果您在额外字段中保留术语,则此代码非常有用。

/**
 * Get vocabulary ID by term name applied to node
 */
$tid = $node->your_field[$node->language][0]['tid'];
$term = taxonomy_term_load($tid);

/* $term now is the following object
stdClass Object(
    [tid] => 1
    [vid] => 1
    [name] => Name of term
    [description] => Description of term
    [format] => full_html
    [weight] => 0
    [vocabulary_machine_name] => vocabulary
) */

/**
 * Loading vocabularies
 */
$vocabularies = taxonomy_get_vocabularies();

/* $vocabularies now is the following array
Array(
    [1] => stdClass Object(
        [vid] => 1
        [name] => Forums
        [machine_name] => forums
        [description] => Forum navigation vocabulary
        [hierarchy] => 1
        [module] => forum
        [weight] => -10
    )
    [2] => stdClass Object(
        [vid] => 2
        [name] => Category
        [machine_name] => category
        [description] => 
        [hierarchy] => 1
        [module] => taxonomy
        [weight] => -9

    )
) */

/**
 * Vocabulary searched by you
 */
$vocabulary = $vocabularies[$term->vid];

/* $vocabulary now is the following object
Array(
    [1] => stdClass Object(
        [vid] => 1
        [name] => Forums
        [machine_name] => forums
        [description] => Forum navigation vocabulary
        [hierarchy] => 1
        [module] => forum
        [weight] => -10
    )
) */

This code is useful if you keep term in your extra field.

/**
 * Get vocabulary ID by term name applied to node
 */
$tid = $node->your_field[$node->language][0]['tid'];
$term = taxonomy_term_load($tid);

/* $term now is the following object
stdClass Object(
    [tid] => 1
    [vid] => 1
    [name] => Name of term
    [description] => Description of term
    [format] => full_html
    [weight] => 0
    [vocabulary_machine_name] => vocabulary
) */

/**
 * Loading vocabularies
 */
$vocabularies = taxonomy_get_vocabularies();

/* $vocabularies now is the following array
Array(
    [1] => stdClass Object(
        [vid] => 1
        [name] => Forums
        [machine_name] => forums
        [description] => Forum navigation vocabulary
        [hierarchy] => 1
        [module] => forum
        [weight] => -10
    )
    [2] => stdClass Object(
        [vid] => 2
        [name] => Category
        [machine_name] => category
        [description] => 
        [hierarchy] => 1
        [module] => taxonomy
        [weight] => -9

    )
) */

/**
 * Vocabulary searched by you
 */
$vocabulary = $vocabularies[$term->vid];

/* $vocabulary now is the following object
Array(
    [1] => stdClass Object(
        [vid] => 1
        [name] => Forums
        [machine_name] => forums
        [description] => Forum navigation vocabulary
        [hierarchy] => 1
        [module] => forum
        [weight] => -10
    )
) */
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文