如何解决如何在drupal8中为'managed_file'类型设置默认值
在drupal 8中,我创建了一个自定义表单以上传.xlsx文件。当我上载.xlsx文件时,文件已成功保存,但是下次访问该页面时,我忘记了文件名。所以我想为该元素显示一个默认值。这些是我的代码,但是无效。请指教,谢谢。
$form['test_file'] = [
'#type' => 'managed_file','#title' => $this->t('Test File'),'#upload_location' => 'private://','#upload_validators' => [
'file_validate_extensions' => ['xls xlsx'],],'#description' => $this->t('Please upload test data excel file.'),'#default_value' => $config->get('test_file'),];
解决方法
$ element [#value] [fids]是managed_file字段处理期间使用的条件。 (请参阅core / module / file / src / Element / ManagedFile.php) 试试这个:
$fid = $config->get('test_file'); // the fid of the file you want to show in the field
$form['test_file'] = [
'#type' => 'managed_file','#title' => $this->t('Test File'),'#upload_location' => 'private://','#upload_validators' => [
'file_validate_extensions' => ['xls xlsx'],],'#description' => $this->t('Please upload test data excel file.'),'#default_value' => null,// don't need
];
// A condition for your test_file variable
if($fid != NULL){
$form['test_file']['#value']['fids'] = array($fid);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。