簡體   English   中英

如何在 drupal8 中為“managed_file”類型設置默認值

[英]How to set default value for 'managed_file' type in drupal8

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM