2016-07-28 14 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'; 

, 당신은 또한 단순히 마녀에 따라 문서의 '#upload_location'속성을 수정합니다합니다 (#validate property 포함) 유효성 검사기를 추가 할 수 있습니다 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; 
}