如何在Form API中传递动态文件位置?

时间:2016-07-28 05:25:52

标签: drupal-7 drupal-modules drupal-forms

我的要求是将文件上传到特定文件夹。如何通过使用表单api实现此目的。 如何修改下面的代码,使upload_location应该是动态的。上传的文件应保存到用户提供的文件夹名称中。
#submit元素未调用custom_document_submit函数。

 $form['folder_name'] = array(
      '#type' => 'textfield',
      '#title' => t('Folder Name'),
  );
  $form['document'] = array(
    '#type' => 'managed_file',
    '#upload_validators' => array('file_validate_extensions' => array('xml')),
    '#upload_location' => 'public://',
    '#submit' => array('custom_document_submit'),
    );
function custom_document_submit($form, &$form_state){
  $destination = $form_state['values']['folder_name'];
  $validators = array();
  $file = file_save_upload('document', $validators, 'public://'.$destination);
}

2 个答案:

答案 0 :(得分:0)

#submit property无法在managed_file表单对象上声明...

相反,您必须在表单(或按钮)上添加或修改提交操作。

$form['#submit'][] = 'custom_document_submit';

如果您不想修改表单的提交方法,您也可以简单地添加一个验证器(使用#validate property),将修改文档的'#upload_location'属性,具体取决于folder_name值。

两者都必须将#submit和#validate属性添加到表单本身。

答案 1 :(得分:0)

<?php

define('IMPORT_DIRECTORY_PATH', 'public://import');

$form['folder_name'] = array(
      '#type' => 'textfield',
      '#title' => t('Folder Name'),
  );


  form['document'] = array(
    '#title' => t('Upload .xml'),
    '#type' => 'managed_file',
    '#upload_validators' => array(
      'file_validate_extensions' => array('xml'),
    ),
    '#process' => array('import_document_element_process'),
  );

$form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Upload'),
    '#submit' => array('custom_document_submit'),
  );

function custom_document_submit($form, &$form_state){

  // Validate extensions.
    $validators = array(
      'file_validate_extensions' => array('xml'),
    );
    $file = file_save_upload('document', $validators, FALSE, FILE_EXISTS_RENAME);

    // If the file passed validation.
    if ($file) {
      // Rename uploaded file to prevent cache from remembering name file.
      $directory = SCHEDULES_IMPORT_DIRECTORY_PATH;
      if (file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
        $uri = $directory . '/xml_' . $file->uid . '_' . $file->timestamp . '.xml';
        if ($file = file_move($file, $uri)) {
          $form_state['values']['document'] = $file;
        }
        else {
          form_set_error('document', t('The file could not be uploaded.'));
        }
      }
      else {
        form_set_error('document', t('The directory is not writable.'));
      }
    }
    else {
      form_set_error('document', t('The file extension is not correct.'));
    }
    // dpm($form_state['values']['document']);
    // var_dump( $form_state['values']['document']);
}


/**
 * Removing the upload button in managed files.
 */
function import_document_element_process($element, &$form_state, $form) {
  $element = file_managed_file_process($element, $form_state, $form);
  $element['upload_button']['#access'] = FALSE;

  return $element;
}