上传Drupal 8后如何获取文件名?

How to get filename after upload Drupal 8?

我正在构建一个表单,用户可以在其中上传文件。因为我使用的是 Drupal,所以我使用的是 managed_file。文件正在上传,但我似乎无法从表格中获取文件名...这是我的代码

构建形式:

        $form['formfile'] = array(
        '#type' => 'managed_file',
        '#name' => 'formfile',
        '#title' => t('File'),
        '#upload_validators' => $validators,
        '#upload_location' => 'public://trainingrequests/',
    );

提交

drupal_set_message($form_state->getValue('formfile'));

我真的什么都试过了。

首先需要获取File实体的实体ID,然后加载实体:

$formfile = $form_state->getValue('formfile');
if ($formfile) {
  $oNewFile = File::load(reset($formfile));
  $oNewFile->setPermanent();
  drupal_set_message('Filename: ' . $oNewFile->getFilename());
}

您可以在您的文件系统中浏览File实体的源代码:core/modules/file/src/Entity/File.php