You are here

function _webform_render_multifile in Webform Multiple File Upload 6

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

Implementation of _webform_render_component().

File

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

Code

function _webform_render_multifile($component, $value = NULL, $filter = TRUE) {
  drupal_add_js(drupal_get_path('module', 'webform_multifile') . '/multifile/jquery.MultiFile.js', 'module');
  drupal_add_js(drupal_get_path('module', 'webform_multifile') . '/webform_multifile.js', 'module');
  $id = 'MultiFile-identifier-' . str_replace('_', '-', $component['form_key']);

  //drupal_add_js(array('webform_multifile' => array('id' => $id)), 'setting');
  if (!isset($component['extra']['attributes']['class'])) {
    $component['extra']['attributes']['class'] = 'form-item multi ' . $id;
  }
  else {
    $component['extra']['attributes']['class'] .= ' 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',
    '#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><script type='text/javascript'>\n/* <![CDATA[ */\n" . "if (typeof MultiFile_fields == 'undefined') {MultiFile_fields = []} \n" . "MultiFile_fields.push({\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/* ]]> */\n</script>",
    '#element_validate' => array(
      '_webform_validate_multifile',
      '_webform_required_multifile',
    ),
    '#pre_render' => array(
      'webform_element_title_display',
    ),
    '#webform_component' => $component,
  );
  $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_wrapper',
  );
  $element['#post_render'] = array(
    'webform_element_wrapper',
  );
  $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;
}