You are here

function _itweak_upload_add_progressbar in iTweak Upload 6.2

1 call to _itweak_upload_add_progressbar()
_itweak_upload_process in ./itweak_upload.module
Process upload form and add a progress bar. Code idea from [#389150]. Except: Using hooking mechanism that is a hack - we want to come in before #process on 'attach', but our _form_alter() is way too early - the upload modules have higher…

File

./itweak_upload.module, line 1458
iTweakUpload - Tweak attachments display and file upload forms.

Code

function _itweak_upload_add_progressbar($form, $form_state) {

  // Add progress bar support to the upload if possible.
  $progress_indicator = variable_get('itweak_upload_progress_indicator', 'bar');
  if ($progress_indicator != 'throbber' && ($implementation = itweak_upload_progress_implementation())) {
    switch ($implementation) {
      case 'uploadprogress':
        $name = 'UPLOAD_IDENTIFIER';
        break;
      case 'apc':
        $name = 'APC_UPLOAD_PROGRESS';
        break;
      default:
        $name = '';
        break;
    }
    if ($name) {
      $upload_progress_key = !empty($form['#post']) ? $form['#post'][$name] : (isset($form[$name]) ? $form[$name]['#value'] : md5(mt_rand()));

      // This hidden field tells the server unique id of the upload, which is used for the progress tracking.
      // The field should be before the file upload button, therefore we render it into #prefix.
      // We also use the field to keep the key between sequential uploads. Otherwise we need some js code to copy the key every time.
      $insert = array(
        '#name' => $name,
        '#type' => 'hidden',
        '#value' => $upload_progress_key,
        '#attributes' => array(
          'class' => 'itweak_upload-progress',
        ),
      );
      $form_id = !empty($form['#post']) && !empty($form['#post']['form_id']) ? $form['#post']['form_id'] : $form['form_id'];
      $insert = form_builder($form_id, $insert, $form_state);
      $insert = drupal_render($insert);
      $form['new']['upload']['#prefix'] = empty($form['new']['upload']['#prefix']) ? $insert : $form['new']['upload']['#prefix'] . $insert;
      $form['new']['attach']['#ahah']['progress']['path'] = 'ajax/itu/progress/' . $upload_progress_key;
    }

    // Add the upload progress callback.
    $form['new']['attach']['#ahah']['method'] = 'replace';
    $form['new']['attach']['#ahah']['effect'] = 'fade';
    $form['new']['attach']['#ahah']['progress']['type'] = $progress_indicator;
  }
  return $form;
}