Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Drupal 如何在声明自定义字段的模块的hook_install()中创建并实例化新的自定义字段?_Drupal_Drupal 7 - Fatal编程技术网

Drupal 如何在声明自定义字段的模块的hook_install()中创建并实例化新的自定义字段?

Drupal 如何在声明自定义字段的模块的hook_install()中创建并实例化新的自定义字段?,drupal,drupal-7,Drupal,Drupal 7,我的模块使用hook\u field\u info()定义自定义字段类型。在本模块的hook\u install()中,我试图创建此自定义字段类型的新字段和实例: function my_module_install() { if (!field_info_field('my_field')) { $field = array( 'field_name' => 'my_field', 'type' => 'custom_field_type',

我的模块使用
hook\u field\u info()
定义自定义字段类型。在本模块的
hook\u install()
中,我试图创建此自定义字段类型的新字段和实例:

function my_module_install() {

  if (!field_info_field('my_field')) {
    $field = array(
      'field_name' => 'my_field',
      'type' => 'custom_field_type',
      'cardinality' => 1
    );
    field_create_field($field);
  }
}
代码在
字段\u创建\u字段($field)
处崩溃:

WD php:FieldException:尝试创建未知类型的字段custom\u field\u type。在字段中创建字段()
/path/to/modules/field/field.crud.inc)。
无法修改标头信息-标头已由(输出从/path/to/drush/includes/output.inc:37开始)bootstrap.inc:1255发送[警告]
FieldException:尝试创建未知类型的字段custom\u field\u type。在字段中创建字段()(第110行/path/to/modules/field/field.crud.inc)。

怎么了?

您试图启用一个定义字段类型的模块,并试图在启用之前在其
hook\u install()
中使用这些相同的字段类型。Drupal的字段信息缓存在运行
hook\u install()
之前不会重建,因此当您尝试创建字段时,Drupal不知道模块中的字段类型

要解决此问题,请在
field\u create\u field($field)
之前调用
field\u info\u cache\u clear()
手动重建字段信息缓存:

WD php: FieldException: Attempt to create a field of unknown type custom_field_type. in field_create_field() (line 110 of                                            [error]
/path/to/modules/field/field.crud.inc).
Cannot modify header information - headers already sent by (output started at /path/to/drush/includes/output.inc:37) bootstrap.inc:1255                             [warning]
FieldException: Attempt to create a field of unknown type <em class="placeholder">custom_field_type</em>. in field_create_field() (line 110 of /path/to/modules/field/field.crud.inc).
if (!field_info_field('my_field')) {
  field_info_cache_clear();

  $field = array(
    'field_name' => 'my_field',
    'type' => 'custom_field_type',
    'cardinality' => 1
  );
  field_create_field($field);
}