You are here

dragndrop_upload_video.module in Drag & Drop Upload 7

Provides Drag & Drop Upload widget for a Video field.

File

modules/dragndrop_upload_video/dragndrop_upload_video.module
View source
<?php

/**
 * @file
 * Provides Drag & Drop Upload widget for a Video field.
 */

/**
 * Implements hook_field_widget_info().
 */
function dragndrop_upload_video_field_widget_info() {
  $dnd_upload_widget_info = dragndrop_upload_file_field_widget_info();
  $info['dragndrop_upload_video'] = $dnd_upload_widget_info['dragndrop_upload_file'];
  $info['dragndrop_upload_video']['field types'] = array(
    'video',
  );
  return $info;
}

/**
 * Implements hook_field_widget_settings_form().
 */
function dragndrop_upload_video_field_widget_settings_form($field, $instance) {
  return dragndrop_upload_file_field_widget_settings_form($field, $instance);
}

/**
 * Implements hook_field_widget_form().
 *
 * @see video_field_widget_form()
 */
function dragndrop_upload_video_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {

  // Add display_field setting to field because file_field_widget_form()
  // assumes it is set.
  $field['settings']['display_field'] = 0;
  if (module_exists('filefield_role_limit')) {
    filefield_role_limit_file_field_process($element, $form_state, $form);
  }
  $elements = dragndrop_upload_file_field_widget_form($form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  $element_info = element_info('dragndrop_upload');
  $pre_render = array_merge($element_info['#pre_render'], array(
    'dragndrop_upload_video_pre_render',
  ));
  foreach (element_children($elements) as $delta) {

    // If not using custom extension validation, ensure this is an video.
    $supported_extensions = array_keys(video_utility::getVideoExtensionPlayers());
    $extensions = isset($elements[$delta]['#upload_validators']['file_validate_extensions'][0]) ? $elements[$delta]['#upload_validators']['file_validate_extensions'][0] : implode(' ', $supported_extensions);
    $extensions = array_intersect(explode(' ', $extensions), $supported_extensions);
    $elements[$delta]['#upload_validators']['file_validate_extensions'][0] = implode(' ', $extensions);

    // Add all extra functionality provided by the video widget.
    $elements[$delta]['#process'][] = 'video_field_widget_process';

    // Add thumbnail stub to prevent errors in file_ajax_upload().
    $elements[$delta]['thumbnail'] = array(
      '#type' => 'value',
      '#value' => NULL,
    );

    // Override value loader
    $elements[$delta]['#value_callback'] = 'video_field_widget_value';
  }
  if ($field['cardinality'] == 1) {

    // If there's only one field, return it as delta 0.
    if (empty($elements[0]['#default_value']['fid'])) {
      $elements[0]['#description'] = theme('file_upload_help', array(
        'description' => $instance['description'],
        'upload_validators' => $elements[0]['#upload_validators'],
      ));
    }
  }
  else {
    $elements['#file_upload_description'] = theme('file_upload_help', array(
      'upload_validators' => $elements[0]['#upload_validators'],
    ));
  }

  // Set full pre render list to the last element only.
  $elements[$delta]['#pre_render'] = $pre_render;
  return $elements;
}

/**
 * Pre render callback for the widget.
 */
function dragndrop_upload_video_pre_render($element) {
  if (isset($element['droppable_area']['#access']) && !$element['droppable_area']['#access']) {
    return $element;
  }

  // Add dragndrop_upload_file js at first.
  $path = drupal_get_path('module', 'dragndrop_upload_file');
  $element['#attached']['js'][] = array(
    'type' => 'file',
    'data' => $path . '/js/dragndrop-upload-file.class.js',
    'weight' => 5.31,
  );
  $element['#attached']['js'][] = array(
    'type' => 'file',
    'data' => $path . '/js/dragndrop-upload-file.js',
    'weight' => 5.32,
  );
  $path = drupal_get_path('module', 'dragndrop_upload_video');
  $element['#attached']['js'][] = array(
    'type' => 'file',
    'data' => $path . '/js/dragndrop-upload-video.class.js',
    'weight' => 5.41,
  );
  $element['#attached']['js'][] = array(
    'type' => 'file',
    'data' => $path . '/js/dragndrop-upload-video.js',
    'weight' => 5.42,
  );
  $element['#attached']['js'][] = array(
    'type' => 'setting',
    'data' => array(
      'dragndropUploadVideo' => array(
        '#' . $element['droppable_area']['#dnd_id'],
      ),
    ),
  );
  return $element;
}