我的模块定义了一个自定义字段类型hook_field_info().在hook_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);
}
}
Run Code Online (Sandbox Code Playgroud)
代码崩溃在field_create_field($field):
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).
Run Code Online (Sandbox Code Playgroud)
怎么了?
您正在尝试启用定义字段类型的模块,并在hook_install()启用之前尝试在其中使用这些字段类型.Drupal的字段信息缓存在hook_install()运行之前不会重建,因此当您尝试创建字段时,Drupal不知道模块中的字段类型.
要解决此问题,请通过以下方式field_info_cache_clear()之前调用来手动重建字段信息缓存field_create_field($field):
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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2414 次 |
| 最近记录: |