2016-07-28 4 views
0

私の要件は、特定のフォルダにファイルをアップロードすることです。フォームapiを使ってこれをどうすれば実現できますか? upload_locationを動的にするようなコードを変更するにはどうすればよいですか。アップロードされたファイルは、ユーザーが提供するフォルダ名に保存する必要があります。
#サブミット要素がcustom_document_submit関数を呼び出していません。Form APIで動的ファイルの場所を渡すにはどうすればよいですか?

$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); 
} 

答えて

0

#submit property

は代わりに、あなたが追加したり、フォーム(またはボタン)に提出するアクションを変更する必要が... managed_fileフォームオブジェクトに宣言することはできません。

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

あなたは、フォームの提出方法を変更したくない場合は、単に(#validate propertyで)バリデータを追加することができ、魔女に応じて、ドキュメントの「#upload_location」プロパティを変更しますfolder_name値。

フォームに#submitプロパティと#validateプロパティの両方を追加する必要があります。

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; 
} 
関連する問題