Drupal node field 文本字段设置长度不足时如何修改
在Drupal 中设置了新的字段长度当设置不足长度时,就无法在管理页面进行修改,需要修改数据库了,为了方便更好的修改,可以使用下面的模块对其修改:
function warehouse_fabric_print_menu(){
$items=array();
$items['warehouse/modfiyfieldlenght']=array(
'title'=>'修改字段的长度',
'description'=>'修改字段的长度',
'page callback'=>'drupal_get_form',
'page arguments'=>array('warehouse_fabric_print_modfiy_form'),
//页面回调的function
//页面调用的用户权限
'access callback'=>TRUE,
);
return $items;
}
function warehouse_fabric_print_modfiy_form($form, &$form_state){
$form=array();
$form['select']=array(
'#type' => 'fieldset',
'#title' => t('Warehouse Fabric Modfiy'),
);
$form['select']['field']= array(
'#type'=>'textfield',
'#title'=>'修改字段的名字',
'#default_value'=>1,
'#size'=>16,
'#required' => TRUE,
);
$form['select']['submit']= array(
'#type' => 'submit',
'#value' => t('提交修改'),
);
return $form;
}
function warehouse_fabric_print_modfiy_form_submit($form, &$form_state){
mymodule_change_text_field_max_length($form_state['values']['field'],255);
}
function mymodule_change_text_field_max_length($field_name, $new_length) {
$field_table = 'field_data_' . $field_name;
$field_revision_table = 'field_revision_' . $field_name;
$field_column = $field_name . '_value';
// Alter value field length in fields table.
db_query("UPDATE `{$field_table}` SET `{$field_column}`=SUBSTR(`{$field_column}`, 0, {$new_length})");
db_query("ALTER TABLE `{$field_table}` CHANGE `{$field_column}` `{$field_column}` VARCHAR( {$new_length} )");
// Alter value field length in fields revision table.
db_query("UPDATE `{$field_revision_table}` SET `{$field_column}`=SUBSTR(`{$field_column}`, 0, {$new_length})");
db_query("ALTER TABLE `{$field_revision_table}` CHANGE `{$field_column}` `{$field_column}` VARCHAR( {$new_length} )");
// Update field config with new max length.
$result = db_query("SELECT CAST(`data` AS CHAR(10000) CHARACTER SET utf8) FROM `field_config` WHERE field_name = '{$field_name}'");
$config = $result->fetchField();
$config_array = unserialize($config);
$config_array['settings']['max_length'] = $new_length;
$config = serialize($config_array);
db_update('field_config')
->fields(array('data' => $config))
->condition('field_name', $field_name)
->execute();
}
参考外文翻译和修改
http://drupal.stackexchange.com/questions/8829/how-to-change-the-length-of-field-settings
function mymodule_change_text_field_max_length($field_name, $new_length) {
$field_table = 'field_data_' . $field_name;
$field_revision_table = 'field_revision_' . $field_name;
$field_column = $field_name . '_value';
// Alter value field length in fields table.
db_query("UPDATE `{$field_table}` SET `{$field_column}`=SUBSTR(`{$field_column}`, 0, {$new_length})");
db_query("ALTER TABLE `{$field_table}` CHANGE `{$field_column}` `{$field_column}` VARCHAR( {$new_length} )");
// Alter value field length in fields revision table.
db_query("UPDATE `{$field_revision_table}` SET `{$field_column}`=SUBSTR(`{$field_column}`, 0, {$new_length})");
db_query("ALTER TABLE `{$field_revision_table}` CHANGE `{$field_column}` `{$field_column}` VARCHAR( {$new_length} )");
// Update field config with new max length.
$result = db_query("SELECT CAST(`data` AS CHAR(10000) CHARACTER SET utf8) FROM `field_config` WHERE field_name = '{$field_name}'");
$config = $result->fetchField();
$config_array = unserialize($config);
$config_array['settings']['max_length'] = $new_length;
$config = serialize($config_array);
db_update('field_config')
->fields(array('data' => $config))
->condition('field_name', $field_name)
->execute();
}
转载自:https://blog.csdn.net/e_zhiwen/article/details/53333708