Saya mengalami beberapa masalah dengan tipe managed_file . Setiap kali saya mencoba mengedit dan menyimpan formulir yang memiliki elemen managed_file di dalamnya terjadi kesalahan: File yang digunakan di bidang Picture mungkin tidak dirujuk.
Ini adalah kode yang saya gunakan:
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;
}
...
}
Aku menelusuri kembali kesalahan ke file_managed_file_validate fungsi dalam modul / file / file.module tetapi tidak tahu apa-apa tentang referensi file.