【问题标题】:Drupal 7, hook_file_presave display error message and stop uploading fileDrupal 7, hook_file_presave 显示错误信息并停止上传文件
【发布时间】:2021-05-09 22:09:35
【问题描述】:

我是 drupal 7 的新手,我正在尝试创建 hook_file_presave / hook_file_validate / hook_field_validate 函数,用于检查网站上传的 pdf 文件是否没有密码保护。

如果文件受密码保护,我可以使用 php 轻松检查。但是当我显示错误消息时,它只显示错误消息也会上传文件。我想我没有使用右钩子。

function simpletest_file_presave($destination){
    // here is my logic

    drupal_set_message(t('file is encrypted >>>>>>>> '. $filename), 'error');
    return;
}

Here you can see file shouldn't be uploaded buit its there with remove button.

【问题讨论】:

    标签: drupal drupal-7


    【解决方案1】:

    我已经有一段时间没有接触 Drupal,但我首先想到的是hook_file_validate()

    但作为explained here,您可以实现hook_form_FORM_ID_alter(),以便添加您自己的文件上传验证器,然后返回要显示的错误数组。

    重要提示:当您使用t() 函数翻译您的消息时,不要将文件名附加到您的字符串,因为这会创建多个翻译字符串,每个上传的文件一个,所以它永远不会被翻译,因为它总是会有所不同。为避免这种情况,请使用占位符并将文件名作为字符串参数传递,如下所示:

    $error_message = t(
      'The file "@filename" is encrypted! Please upload a PDF without password protection.',
      ['@filename' => $filename_without_path]
    );
    

    API documentation for the t() and format_string() functions

    【讨论】:

      猜你喜欢
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-20
      • 1970-01-01
      • 2017-06-27
      • 1970-01-01
      • 2016-05-13
      相关资源
      最近更新 更多