【问题标题】:Error in Programmatically creating Taxonomy in drupal在 drupal 中以编程方式创建分类法时出错
【发布时间】:2014-10-29 05:40:05
【问题描述】:

我在使用以下代码构建分类词汇和术语时遇到以下错误

FieldException:尝试创建不存在或当前处于非活动状态的字段 field_my_custom_vocab002 的实例。在 field_create_instance() 中(C:\wamp\www\pur_theme\modules\field\field.crud.inc 的第 476 行)。

我检查了词汇并创建了它,问题仅在于术语创建

代码

<?php

$new_vocab = (object) array(
    'name' => 'My custom vocabulary002',
    'description' => 'Test',
    'machine_name' => 'my_custom_vocab002',
  );
taxonomy_vocabulary_save($new_vocab);

$vocab = taxonomy_vocabulary_machine_name_load('my_custom_vocab002');

$term1 = (object) array(
   'name' => 'Term 1',
    'description' => 'This is term 1',
   'vid' => $vocab->vid,
);

taxonomy_term_save($term1);

我在哪里/哪里做错了?

【问题讨论】:

    标签: php drupal drupal-7 drupal-taxonomy


    【解决方案1】:

    我认为这是因为您尝试创建一个字段比 2 个基本字段(名称和 vid)更多的术语。您还有一个“描述”字段。

    试试这个代码:

    $term = new stdClass();
    $term->name = 'Term 1';
    $term->vid = $vocab->vid; 
    $term->field_description[LANGUAGE_NONE][0]['value'] = 'This is term 1'; 
    taxonomy_term_save($term);
    

    来源:http://www.lightrains.com/blog/programmatically-create-taxonomy-term-drupal

    【讨论】:

    • $term-&gt;field_description[LANGUAGE_NONE][0]['value'] 不是 taxonomy_term 描述字段的情况。它应该是$term-&gt;description,因为描述列存在于taxonomy_term_data db表中。
    • $term-&gt;field_description[LANGUAGE_NONE][0]['value'] = 'This is term 1'; 用于新字段。
    【解决方案2】:

    我发现唯一的解决方案是在创建词汇表之前创建它正在寻找的字段。就我个人而言,我使用自定义函数做到了这一点:

    function MY_MODULE_create_new_taxonomy($taxonomy_name, $taxonomy_machine_name, $taxonomy_description){
    
      //Add field you know is going to cause trouble
      $field = array(
        'field_name' => 'field_'.$taxonomy_machine_name,
        'type' => 'text',
        'label' => 'Label'
      );
    
      field_create_field($field);
    
      //create the vocab
      $new_vocabulary = new stdClass();
      $new_vocabulary->name = $taxonomy_name;
      $new_vocabulary->machine_name = $taxonomy_machine_name;
      $new_vocabulary->description = t($taxonomy_description);
      $new_vocabulary->module = 'taxonomy';
    
      taxonomy_vocabulary_save($new_vocabulary);
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-27
      • 2016-03-19
      • 2014-11-17
      • 1970-01-01
      相关资源
      最近更新 更多