【问题标题】:How to pass dynamic file location in Form API?如何在表单 API 中传递动态文件位置?
【发布时间】:2016-07-28 05:25:51
【问题描述】:

我的要求是将文件上传到特定文件夹。我怎样才能通过使用表单 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);
}

【问题讨论】:

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


    【解决方案1】:

    #submit property 不能在 managed_file 表单对象上声明...

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

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

    如果您不想修改表单的提交方法,您也可以简单地添加一个验证器(使用#validate property),女巫将根据文件夹名称值修改文档的“#upload_location”属性.

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

    【讨论】:

      【解决方案2】:
      <?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;
      }
      

      【讨论】:

        猜你喜欢
        • 2013-06-15
        • 1970-01-01
        • 1970-01-01
        • 2020-05-21
        • 1970-01-01
        • 2020-11-04
        • 1970-01-01
        • 2021-05-18
        • 2021-03-25
        相关资源
        最近更新 更多