You are here

function plupload_element_pre_render in Plupload integration 7.2

Same name and namespace in other branches
  1. 7 plupload.module \plupload_element_pre_render()

Pre render (#pre_render) callback to attach JS settings for the element.

1 string reference to 'plupload_element_pre_render'
plupload_element_info in ./plupload.module
Implements hook_element_info().

File

./plupload.module, line 252
Implementation of plupload.module.

Code

function plupload_element_pre_render($element) {
  $settings = isset($element['#plupload_settings']) ? $element['#plupload_settings'] : array();

  // The Plupload library supports client-side validation of file extension, so
  // pass along the information for it to do that. However, as with all client-
  // side validation, this is a UI enhancement only, and not a replacement for
  // server-side validation.
  if (empty($settings['filters']) && isset($element['#upload_validators']['file_validate_extensions'][0])) {
    $settings['filters'][] = array(
      // @todo Some runtimes (e.g., flash) require a non-empty title for each
      //   filter, but I don't know what this title is used for. Seems a shame
      //   to hard-code it, but what's a good way to avoid that?
      'title' => t('Allowed files'),
      'extensions' => str_replace(' ', ',', $element['#upload_validators']['file_validate_extensions'][0]),
    );
  }

  // Check for autoupload and autosubmit settings and add appropriate callback.
  if (!empty($element['#autoupload'])) {
    $settings['init']['FilesAdded'] = 'Drupal.plupload.filesAddedCallback';
    if (!empty($element['#autosubmit'])) {
      $settings['init']['UploadComplete'] = 'Drupal.plupload.uploadCompleteCallback';
    }
  }

  // Add a specific submit element that we want to click if one is specified.
  if (!empty($element['#submit_element'])) {
    $settings['submit_element'] = $element['#submit_element'];
  }

  // Check if there are event callbacks and append them to current ones, if any.
  if (!empty($element['#event_callbacks'])) {

    // array_merge() only accepts parameters of type array.
    if (!isset($settings['init'])) {
      $settings['init'] = array();
    }
    $settings['init'] = array_merge($settings['init'], $element['#event_callbacks']);
  }
  if (empty($element['#description'])) {
    $element['#description'] = '';
  }
  $element['#description'] = theme('file_upload_help', array(
    'description' => $element['#description'],
    'upload_validators' => $element['#upload_validators'],
  ));
  $element['#attached']['js'][] = array(
    'type' => 'setting',
    'data' => array(
      'plupload' => array(
        $element['#id'] => $settings,
      ),
    ),
  );
  return $element;
}