You are here

function video_field_widget_process in Video 7.2

Same name and namespace in other branches
  1. 7 video.field.inc \video_field_widget_process()

An element #process callback for the video field type.

1 string reference to 'video_field_widget_process'
video_field_widget_form in ./video.field.inc
Implements hook_field_widget_form().

File

./video.field.inc, line 759
Implement a video field, based on the file module's file field.

Code

function video_field_widget_process($element, &$form_state, $form) {
  $file = $element['#value'];
  $file['fid'] = $fid = intval($element['fid']['#value']);
  $field = field_widget_field($element, $form_state);
  $element['#theme'] = 'video_widget';

  // Title is not necessary for each individual field.
  if ($field['cardinality'] != 1) {
    unset($element['#title']);
  }

  // Add our extra fields if in preview mode
  if (empty($file['fid'])) {
    return $element;
  }
  $transcoder = new Transcoder();

  // Various settings.
  if (!empty($field['settings']['autoconversion']) && $transcoder
    ->hasTranscoder()) {
    $description = t('Set the size of the converted video.');
    $options = video_utility::getDimensions();
    $video_info = _video_dimensions_options($options, $file);
    if (!empty($video_info['width']) && !empty($video_info['height'])) {
      $description .= ' ' . t('The original video size is %size. If you choose a higher resolution, this could cause video distortion. You are shown dimensions that match your aspect ratio, if you choose dimensions that do not match your ratio, black bars will be added to maintain the original aspect ratio.', array(
        '%size' => $video_info['width'] . 'x' . $video_info['height'],
      ));
    }

    // Dimensions dropdown.
    $element['dimensions'] = array(
      '#type' => 'select',
      '#title' => t('Output video dimensions'),
      '#default_value' => $file['dimensions'],
      '#description' => $description,
      '#options' => $options,
      '#access' => user_access('override player dimensions'),
    );

    // hide dimensions when video_use_preset_wxh is true
    if (variable_get('video_use_preset_wxh', FALSE)) {
      $element['dimensions']['#access'] = FALSE;
    }
    if (!empty($file['conversionstatus']) && ($file['conversionstatus'] == VIDEO_RENDERING_COMPLETE || $file['conversionstatus'] == VIDEO_RENDERING_FAILED)) {
      $status = array(
        VIDEO_RENDERING_COMPLETE => 'was successful',
        VIDEO_RENDERING_FAILED => 'failed',
      );
      $element['re_convert_video'] = array(
        '#type' => 'checkbox',
        '#title' => t('Video conversion ' . $status[$file['conversionstatus']] . '. Re-queue video conversion?'),
        '#description' => t('This will re-convert your video and schedule it for cron, unless you also check “convert video on save,” below.'),
        '#attributes' => array(
          'class' => array(
            'video-re-convert',
            'video-' . $file['conversionstatus'],
          ),
        ),
        '#access' => user_access('re convert video'),
      );
    }

    // Bypass conversion checkbox.
    $element['bypass_autoconversion'] = array(
      '#type' => 'checkbox',
      '#title' => t('Bypass auto conversion'),
      '#default_value' => $file['bypass_autoconversion'],
      '#description' => t('This video will not convert your video when you save, and it will not be scheduled for cron.'),
      '#attributes' => array(
        'class' => array(
          'video-bypass-auto-conversion',
        ),
      ),
      '#access' => user_access('bypass conversion video'),
    );

    // Convert on save checkbox.
    $element['convert_video_on_save'] = array(
      '#type' => 'checkbox',
      '#title' => t('Convert video on save'),
      '#default_value' => $file['convert_video_on_save'],
      '#description' => t('This will convert your video on save, instead of scheduling it for cron.'),
      '#attributes' => array(
        'class' => array(
          'video-convert-video-on-save',
        ),
      ),
      '#access' => user_access('convert on submission'),
    );
  }

  // Use of default thumbnail checkbox.
  if (!empty($field['settings']['default_video_thumbnail']['fid'])) {
    $element['use_default_video_thumb'] = array(
      '#type' => 'checkbox',
      '#title' => t('Use the default thumbnail for this video'),
      '#default_value' => $file['use_default_video_thumb'],
      '#description' => t('This will set a flag for this video to use the default video thumbnail when output.'),
      '#attributes' => array(
        'class' => array(
          'video-use-default-video-thumb',
        ),
        'data-defaultimage' => image_style_url($field['settings']['preview_video_thumb_style'], $field['settings']['default_video_thumbnail']['uri']),
      ),
      '#access' => user_access('use default thumb'),
    );
  }

  // Thumbnails.
  $defaultthumbfid = isset($field['settings']['default_video_thumbnail']) ? intval($field['settings']['default_video_thumbnail']['fid']) : 0;
  $thumbnailfid = is_array($file['thumbnail']) ? intval($file['thumbnail']['fid']) : intval($file['thumbnail']);
  $gen_fail = FALSE;
  if ($field['settings']['autothumbnail'] == 'auto' && $transcoder
    ->hasTranscoder()) {
    $thumbs = $transcoder
      ->extractFrames($file, $field);
    $thumbstyle = !empty($field['settings']['preview_video_thumb_style']) ? $field['settings']['preview_video_thumb_style'] : 'thumbnail';
    if (!empty($thumbs)) {
      $thumbss = array();
      foreach ($thumbs as $img) {
        $thumbss[$img->fid] = theme('image_style', array(
          'style_name' => $thumbstyle,
          'path' => $img->uri,
        ));
      }
      $currentthumb = 0;
      if ($thumbnailfid > 0 && isset($thumbss[$thumbnailfid])) {
        $currentthumb = $thumbnailfid;
      }
      elseif ($thumbnailfid != $defaultthumbfid) {
        $currentthumb = array_rand($thumbss);
      }
      $element['thumbnail'] = array(
        '#type' => 'radios',
        '#title' => t('Video thumbnail'),
        '#options' => $thumbss,
        '#default_value' => $currentthumb,
        '#weight' => 10,
        '#attributes' => array(
          'class' => array(
            'video-thumbnails',
          ),
        ),
      );
    }
    elseif ($thumbs === FALSE) {
      $gen_fail = TRUE;
    }
  }
  if ($gen_fail || $field['settings']['autothumbnail'] == 'manual_upload') {
    $scheme = isset($field['settings']['uri_scheme_thumbnails']) ? $field['settings']['uri_scheme_thumbnails'] : 'public';
    $element['thumbnail'] = array(
      '#title' => t('Video thumbnail'),
      '#type' => 'managed_file',
      '#description' => t('The uploaded image will be used as video thumbnail on this video.'),
      '#default_value' => NULL,
      '#upload_location' => $scheme . '://' . variable_get('video_thumbnail_path', 'videos/thumbnails') . '/' . $fid,
      '#weight' => 10,
    );

    // Set the current thumbnail fid, if it is not the default one
    if ($thumbnailfid > 0) {
      if ($defaultthumbfid == 0 || $thumbnailfid != $defaultthumbfid) {
        $element['thumbnail']['#default_value'] = $thumbnailfid;
      }
      else {

        // Unset the form_state value for the thumbnail field if it is the default thumbnail.
        // This prevents the "may not be referenced" error.
        form_set_value($element, NULL, $form_state);
      }
    }
  }

  // Setup our large thumbnail that is on the left.
  if (!empty($field['settings']['preview_video_thumb_style'])) {
    $large_thumb = NULL;
    if (!empty($currentthumb)) {
      $large_thumb = file_load($currentthumb);
    }
    elseif ($thumbnailfid > 0) {
      $large_thumb = file_load($thumbnailfid);
    }
    elseif ($defaultthumbfid > 0) {
      $large_thumb = file_load($defaultthumbfid);
    }
    if (!empty($large_thumb)) {
      $element['preview']['#markup'] = '<div class="video-preview video_large_thumbnail-' . $fid . '">' . theme('image_style', array(
        'style_name' => $field['settings']['preview_video_thumb_style'],
        'path' => $large_thumb->uri,
      )) . '</div>';
    }
  }
  return $element;
}