我在managed_file类型方面遇到一些问题。每当我尝试编辑并保存其中包含Managed_file元素的表单时,都会发生错误:可能不会引用“图片”字段中使用的文件。
这是我正在使用的代码:
function foo_form ($form, &$form_state, $foo) {
...
$form['file'] = array(
'#type' => 'managed_file',
'#title' => t('Picture'),
'#default_value' => (isset($foo->file->fid) ? $banner->foo->fid : ''),
'#upload_location' => variable_get('foo_upload_location'),
);
if (isset($foo->file)) {
$form['current_file'] = array(
'#type' => 'hidden',
'#value' => $foo->file->fid,
);
}
...
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
function foo_form_submit ($form, &$form_state) {
$foo = (object)$form_state['values'];
$current_file_set = isset($form_state['values']['current_file']);
if ($form_state['values']['file'] != 0 && !$current_file_set) {
// Load the file uploaded in the form.
$file = file_load($form_state['values']['file']);
$file->status = FILE_STATUS_PERMANENT;
file_save($file);
$foo->file = $file->fid;
} else if ($form_state['values']['file'] != 0 && $current_file_set) {
// If we are uploading a different picture, delete the old one and save the
// new one. If not, don't do anything.
if ($form_state['values']['current_file'] != $form_state['values']['file']) {
file_delete(file_load($form_state['values']['current_file']));
// Load the file uploaded in the form.
$file = file_load($form_state['values']['file']);
$file->status = FILE_STATUS_PERMANENT;
file_save($file);
$foo->file = $file->fid;
}
} else {
file_delete(file_load($form_state['values']['current_file']));
$foo->file = null;
}
...
}
我将错误追溯到modules / file / file.module中的file_managed_file_validate函数,但对文件引用一无所知。