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);
}