You are here

function file_entity_add_upload_step_upload in File Entity (fieldable files) 7.3

Same name and namespace in other branches
  1. 7.2 file_entity.pages.inc \file_entity_add_upload_step_upload()

Generate form fields for the first step in the add file wizard.

1 call to file_entity_add_upload_step_upload()
file_entity_add_upload in ./file_entity.pages.inc
Form callback for adding a file via an upload form.

File

./file_entity.pages.inc, line 124
Supports file operations including View, Edit, and Delete.

Code

function file_entity_add_upload_step_upload($form, &$form_state, array $options = array()) {
  $upload_validators = file_entity_get_upload_validators($options);
  $form['upload'] = array(
    '#type' => 'managed_file',
    '#title' => t('Upload a new file'),
    '#upload_location' => file_entity_upload_destination_uri($options),
    '#upload_validators' => $upload_validators,
    '#progress_indicator' => 'bar',
    '#required' => TRUE,
    '#pre_render' => array(
      'file_managed_file_pre_render',
      'file_entity_upload_validators_pre_render',
    ),
    '#default_value' => isset($form_state['storage']['upload']) ? $form_state['storage']['upload'] : NULL,
  );
  $form['upload']['#description'] = t('Files must be less than !size.', array(
    '!size' => '<strong>' . format_size($form['upload']['#upload_validators']['file_entity_validate_size_extensions'][0]) . '</strong>',
  ));

  // Get list of extensions.
  $extensions = explode("\n", variable_get('file_entity_max_filesize_extensions'));
  if (!empty($extensions)) {
    $limits = array();

    // Limit extensions displayed to those in upload validator.
    $valid_extensions = array();
    if (!empty($upload_validators)) {
      foreach ($upload_validators['file_validate_extensions'] as $key => $values) {
        $valid_extensions[$key] = explode(' ', strtolower($values));
      }
    }

    // If there are valid extensions, merge sub-arrays into a single array.
    if (!empty($valid_extensions)) {
      $valid_extensions = call_user_func_array('array_merge', $valid_extensions);
    }
    foreach ($extensions as $position => $text) {
      $matches = array();
      preg_match('/(.*)\\|(.*)/', $text, $matches);
      if (is_array($matches) && count($matches) == 3) {
        $extension = $matches[1];
        $filesize = $matches[2];
        if (empty($valid_extensions) || in_array(strtolower($extension), $valid_extensions)) {
          $limits[] = trim($extension) . ': <strong>' . trim($filesize) . '</strong>';
        }
      }
    }
    if (!empty($limits)) {

      // If less than or equal to 15 items to display, show as unordered list.
      // Otherwise, implode into a single list item.
      $list = array(
        '#theme' => 'item_list',
        '#list_type' => 'ul',
        '#items' => count($limits) <= 15 ? $limits : array(
          implode(', ', $limits),
        ),
      );
      if (!empty($valid_extensions) && count($limits) == count($valid_extensions)) {
        $form['upload']['#description'] = t('Files must be less than these sizes: !list', array(
          '!list' => render($list),
        ));
      }
      else {
        $form['upload']['#description'] = t('Files must be less than these sizes: !list The default maximum size is !size.', array(
          '!list' => render($list),
          '!size' => '<strong>' . format_size($form['upload']['#upload_validators']['file_entity_validate_size_extensions'][0]) . '</strong>',
        ));
      }
    }
  }
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['next'] = array(
    '#type' => 'submit',
    '#value' => t('Next'),
  );
  form_load_include($form_state, 'inc', 'file_entity', 'file_entity.pages');
  return $form;
}