You are here

function filefield_sources_plupload_source_process in FileField Sources Plupload 7

A #process callback to extend the filefield_widget element type.

1 string reference to 'filefield_sources_plupload_source_process'
filefield_sources_plupload_filefield_sources_info in ./filefield_sources_plupload.module
Implements hook_filefield_sources_info().

File

./filefield_sources_plupload.module, line 27
A File field extension to allow multi uploads using Plupload.

Code

function filefield_sources_plupload_source_process($element, $form_state, $form) {
  $field = field_widget_field($element, $form_state);
  $instance = field_widget_instance($element, $form_state);

  // Get the upload size. Use PHP limit if not defined. This can be specified
  // larger than PHP limit, since Plupload transfers using 1mb chunks.
  $max_filesize = !empty($instance['settings']['max_filesize']) ? parse_size($instance['settings']['max_filesize']) : parse_size(file_upload_max_size());
  $element['filefield_plupload'] = array(
    '#weight' => 100.5,
    // Required for proper theming.
    '#filefield_source' => TRUE,
    '#prefix' => '<div class="filefield-source filefield-source-plupload clearfix">',
    '#suffix' => '</div>',
  );
  $element['filefield_plupload']['pud'] = array(
    '#type' => 'plupload',
    '#title' => t('Select one or more files to upload'),
    // Even though filefield does validation on submit, this is required for
    // client side validation as well as proper file munging during upload.
    '#upload_validators' => $element['#upload_validators'],
    '#plupload_settings' => array(
      'cardinality' => $field['cardinality'],
      'max_file_size' => $max_filesize,
      'chunk_size' => '1mb',
      'runtimes' => 'html5,flash,silverlight,html4',
    ),
    // We need our own value callback as we need access to $form_state.
    '#value_callback' => 'filefield_sources_plupload_element_value',
    '#process' => array(
      'filefield_sources_plupload_element_process',
    ),
  );
  if ($field['cardinality'] == 1) {
    $element['filefield_plupload']['pud']['#plupload_settings'] += array(
      'multi_selection' => FALSE,
      'multiple_queues' => FALSE,
    );
    $element['filefield_plupload']['pud']['#title'] = t("Select one file to upload");
  }
  elseif ($field['cardinality'] != -1) {
    $element['filefield_plupload']['pud']['#title'] = t("Select up to @cardinality files to upload", array(
      '@cardinality' => $field['cardinality'],
    ));
  }
  $element['filefield_plupload']['upload_button'] = array(
    '#name' => implode('_', $element['#array_parents']) . '_transfer',
    '#type' => 'submit',
    '#value' => t('Start upload'),
    '#validate' => array(),
    '#submit' => array(
      'filefield_sources_field_submit',
    ),
    '#limit_validation_errors' => array(
      $element['#parents'],
    ),
    '#ajax' => array(
      'path' => 'file/ajax/' . implode('/', $element['#array_parents']) . '/' . $form['form_build_id']['#value'],
      'wrapper' => $element['#id'] . '-ajax-wrapper',
      'method' => 'replace',
      'effect' => 'fade',
      'event' => 'pud_update',
    ),
  );
  return $element;
}