【问题标题】:Drupal 7 API + TaxonomyDrupal 7 API + 分类
【发布时间】:2011-11-03 20:40:00
【问题描述】:

我正在尝试使用 PHP 构建一个小工具,将内容从我当前的 CMS 导入 Drupal 7,因为我有大约 10k 多篇文章要引入。到目前为止,我已经获得了标题、摘要、正文、作者和发布日期到了,但是当涉及到类别(标签)时,我完全感到困惑。

我当前的每个类别/标签都存储在一个数据库表中,每个都有自己的 ID、名称和描述。我可以在每篇文章中提取它并按我喜欢的方式对其进行排序(字符串、数组等)。

在我的导入过程中,我猜我应该这样做:

$node->field_tags = array(
    'und' => array(
        array(
            'Update',
            'News',
            'Report'
        )
    )
);

我也试过了:

$node->field_tags = array(
    'Update',
    'News',
    'Report'
);

但是这些也不用逗号分隔的字符串是行不通的。 Drupal 7 API 文档似乎没有在我找到的任何地方解释这一点。

发送标签的格式是什么,或者我无法找到的文档页面是什么?提前谢谢!

【问题讨论】:

    标签: php api drupal tags drupal-7


    【解决方案1】:

    Drupal 7 中的术语字段与物理分类术语相关,因此您需要为每个类别创建一个术语,然后将该引用添加为字段的值。

    此代码可能会有所帮助:

    // Load the tags vocabulary
    $vocab = taxonomy_vocabulary_machine_name_load('tags');
    
    $term = new stdClass;
    $term->vid = $vocab->vid; // Attach the vocab id to the new term
    $term->name = 'Category Name'; // Give the term a name
    taxonomy_term_save($term); // Save it
    
    // Add the tags field
    $node->field_tags = array(
      LANGUAGE_NONE => array(
        'tid' => $term->tid // Relate the field to the new category term using it's tid (term id)
      )
    );
    

    【讨论】:

    • 感谢您的回复!如果我想添加多个标签怎么办?当然,它们不可能都有 'tid' 索引。
    • 是的,您添加的每个标签都是一个分类术语,每个分类术语都需要独立存在,否则 Drupal 与它没有任何关系。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多