檔案上傳 managed_file Event 觸發

1 篇文章 / 0 new
author
檔案上傳 managed_file Event 觸發
三個 callback 參數可使用
function managed_file_Event($form, &$form_state) {
    //drupal_add_js(drupal_get_path('module', 'formexample') . '/auto_upload.js'); //不用再按下上傳
    $element_info = element_info('managed_file');
    $form['use_managed_file'] = array(
        '#title' => t('圖片上傳'),
        '#type' => 'managed_file',
        '#description' => t('許可的檔案類型: <em>gif, png, jpg, jpeg.</em>'),
        //'#default_value' => , //會自動判定 上傳/移除 按鈕
        '#upload_validators' => array(
            'file_validate_extensions' => array('gif png jpg jpeg'),
            // array key is the callback function with value as an array of arguments to pass. file object is automatically added as first argument.
            'CALL_onValidate' => array('參數資料內容'),
        ),
        '#upload_location' => 'public://',
        // this will help to add onRemove event
        '#file_value_callbacks' => array('CALL_onRemove'),
        // this will help to add onAdd/onRemove event
        '#process' => array_merge($element_info['#process'], array('CALL_onProcess')),
        /* 預設值
        '#value_callback' => 'file_managed_file_value', //不可替代否則上傳失效
        '#element_validate' => array('file_managed_file_validate'),
        '#pre_render' => array('file_managed_file_pre_render'),
        '#theme' => 'file_managed_file',
        '#theme_wrappers' => array('form_element'),
        '#progress_indicator' => 'throbber',*/
    );
}
 
function CALL_onProcess($element, &$form_state, $form) {
    if (isset($element['filename'])) {
        // add JS on ajax file add
        // to get to current window, use 'parent.' (like 'parent.jQuery(".body").addClass("mf_added")')
        $js = arg(0) == 'file' && arg(1) == 'ajax' ? '<script>alert("added");</script>' : '';
        // here I also modify markup, displayed at the left of the button
        $element['filename']['#markup'] = $js .
            theme('image', array('path' => $element['#file']->uri, 'width' => 100, 'height'=> 100, ) );
    }
    return $element;
}
 
function CALL_onRemove($element, $input, $form_state) {
    if (empty($input)) {
        // in my case it also modifies current page, accessing it through 'parent.'
        // add JS only on ajax remove
        print '<script>parent.jQuery("#use_managed_file").slideUp("slow");alert("removed");</script>';
    }
}
//資料驗證處理
function CALL_onValidate($file, $myARGS) {
    //form_set_error()
    return array(); //若無錯誤則傳回空陣列
}
► 省去按下 上傳鈕 的方式, 選取檔案後立即上傳
程式中加入 drupal_add_js(drupal_get_path('module', 'formexample') . '/auto_upload.js'); 或增加元素屬性值
'#attached' => array( 'js' => array(
    drupal_get_path('module', 'file') . '/file.js', //原基本值
    drupal_get_path('module', 'formexample') . '/auto_upload.js'), //自動上傳
),
► auto_upload.js 
(function ($) {
  Drupal.behaviors.autoUpload = {
    attach: function (context, settings) {
      $('form', context).delegate('input.form-file', 'change', function() {  
        $(this).next('input[type="submit"]').mousedown();
      });
    }
  };
})(jQuery);
Free Web Hosting