【问题标题】:Drupal 7, default_value for a file field in a custom formDrupal 7,自定义表单中文件字段的 default_value
【发布时间】:2013-10-28 10:00:36
【问题描述】:

我创建了一个公式,用户可以在其中使用文件字段上传图像。在另一个公式中,用户可以更改他之前上传的图像。

到目前为止,我能够让用户上传新图片或让该字段为空并保留现有图片。我的问题是,我如何以一种很好的方式向用户展示他之前使用该字段上传的文件?设置#default_value(下面的@code)对我没有任何作用。

//$smallimage = fid of image
$form['smallimage'] = array(
  '#type' => 'file',
  '#title' => t('Image'),
  '#default_value' => $smallimage,
  '#description' => t('Upload a file, allowed extensions: jpg, jpeg, png, gif'),
);

编辑: 切换回托管文件。我没有将我的文件/图像绑定到的类型,因此我将它们绑定到第一个节点和第一个用户。不是一个干净的解决方案,但它有效。

有没有人不切换到 managed_file 的解决方案/解决方法?

提前谢谢你

【问题讨论】:

    标签: drupal-7 drupal-fapi


    【解决方案1】:

    在 Drupal FAPI 上,'#default_value' 不是类型“文件”的有效属性,如果您想在这种情况下使用属性 #default_value,您可以使用

    '#type'         => 'managed_file'
    

    你的代码应该是这样的,

    $form['file'] = array( 
        '#title'        => t('Upload image'),
        '#type'         => 'managed_file',
        '#description'  => t('Images must be one of jpg, bmp, gif or png formats.'),
        '#default_value' => $fid,  //here you need to provide the file id (get it from database or $file_obj->fid).
        '#upload_location' => 'public://'
    );
    

    【讨论】:

      猜你喜欢
      • 2011-05-20
      • 1970-01-01
      • 2020-06-21
      • 2012-10-02
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多