You are here

function _webform_render_multifile in Webform Multiple File Upload 7

Same name and namespace in other branches
  1. 6 multifile.inc \_webform_render_multifile()

Implementation of _webform_render_component().

File

./multifile.inc, line 344
Webform module file component.

Code

function _webform_render_multifile($component, $value = NULL, $filter = TRUE) {
  $id = 'MultiFile-identifier-' . str_replace('_', '-', $component['form_key']);
  $component['extra']['attributes']['class'][] = 'form-item multi ' . $id;
  $node = node_load($component['nid']);
  $form_key = implode('_', webform_component_parent_keys($node, $component));
  $current_types = isset($component['extra']['filtering']['types']) ? $component['extra']['filtering']['types'] : array();
  $element[$form_key] = array(
    '#type' => 'file',
    '#translatable' => array(
      'title',
      'description',
    ),
    '#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'],
    '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
    //'#required' => $component['mandatory'], // Drupal core bug with required file uploads.
    '#description' => $filter ? _webform_filter_descriptions($component['extra']['description']) : $component['extra']['description'],
    '#attributes' => $component['extra']['attributes'],
    '#tree' => FALSE,
    // file_check_upload assumes a flat $_FILES structure.
    '#suffix' => "<div class='outer'><div class='inner'></div></div>",
    "#attached" => array(
      'js' => array(
        drupal_get_path('module', 'webform_multifile') . '/multifile/jquery.MultiFile.js' => array(
          'type' => 'file',
        ),
        drupal_get_path('module', 'webform_multifile') . '/webform_multifile.js' => array(
          'type' => 'file',
          'scope' => 'footer',
        ),
        "(function (\$) { \n" . "  if (typeof MultiFile_fields == 'undefined') {MultiFile_fields = []} \n" . "  MultiFile_fields.push(\n  {\n    id :'{$id}',\n    properties : {\n" . "        max:" . $component['extra']['max_amount'] . ",\n" . "        accept:'" . join('|', $current_types) . "',\n        STRING: {\n" . "          remove:'" . t('Remove') . "',\n" . "          denied:'" . t('You are kindly asked not to submit !ext files in this form.', array(
          '!ext' => '$ext',
        )) . "',\n" . "          duplicate:'" . t('The file !file has already been selected and will be uploaded once you submit this form.', array(
          '!file' => '$file',
        )) . "'\n" . "        }\n" . "      }\n    }\n  );\n}) (jQuery);" => array(
          'type' => 'inline',
        ),
      ),
    ),
    '#element_validate' => array(
      '_webform_validate_multifile',
      '_webform_required_multifile',
    ),
    '#pre_render' => array(
      'webform_element_title_display',
    ),
    '#webform_component' => $component,
    '#upload_location' => $component['extra']['scheme'] . '://webform/' . $component['extra']['directory'],
  );
  $element['#webform_required'] = $component['mandatory'];
  $element['#webform_form_key'] = $form_key;
  $element['#weight'] = $component['weight'];
  $element['#theme'] = 'webform_render_multifile';
  $element['#theme_wrappers'] = array(
    'webform_element',
  );
  $element['#webform_component'] = $component;

  // Change the 'width' option to the correct 'size' option.
  if ($component['extra']['width'] > 0) {
    $element[$form_key]['#size'] = $component['extra']['width'];
  }

  // Add a hidden element to store the FID for new files.
  $element['_fids'] = array(
    '#type' => 'hidden',
    '#default_value' => '',
  );

  // Add a hidden element to store the FID for existing files.
  $element['_old_fids'] = array(
    '#type' => 'hidden',
    '#value' => isset($value[0]) ? $value[0] : NULL,
  );
  return $element;
}