首页 / 帖子
用field_create_instance为article添加一个字段时,提示出错

用field_create_instance为article添加一个字段时,提示出错


提示错误信息如下:

Notice: Array to string conversion in eval() (line 10 ofF:\wamp\www\zy_test4\sites\all\modules\devel\devel.module(1415) : eval()'d code).

FieldException: Attempt to create an instance of field field_test_first on forbidden entity type node. in field_create_instance() (line 487 ofF:\wamp\www\zy_test4\modules\field\field.crud.inc).


代码如下:

$thefield = field_info_field('field_test_first');

if(empty($thefield)){

  $thefield = array(

    'entity_types' => array('node'),

    'field_name' => 'field_test_first',

    'type' => 'text',

);

  $thefield = field_create_field($thefield);

}

print_r('thefield' .$thefield);


$theinstance = field_info_instance('node' , 'field_test_first' , 'Article');

if(empty($theinstance)){

  $theinstance = array(

        'bundle' => 'article',

        'entity_type' => 'node',

'default_value' => NULL,

'deleted' => '0',

'field_name' => 'field_test_first',

'label' => 'the test first',

'required' => 0,

'title' => 'first title',

  );

  $theinstance = field_create_instance($theinstance);

}

print_r('theinstance' .$theinstance);


2个答案
YOYO
发布于:2014-11-02 17:59

这种问题,果断搜索吧。要么就自己跟踪源代码。

这种用法很少很少。。。


https://www.drupal.org/node/1404344

http://drupal.stackexchange.com/questions/36178/create-a-field-instance-programmatically-on-a-taxonomy-vocabulary

王斌
发布于:2014-11-03 06:52

entity_type是不是多了一个s


http://www.monarchdigital.com/blog/2013-08-09/programmatically-creating-fields-drupal-7 有一个例子