You are here

function videojs_field_formatter_view in Video.js (HTML5 Video Player) 7.3

Same name and namespace in other branches
  1. 7 videojs.module \videojs_field_formatter_view()
  2. 7.2 videojs.module \videojs_field_formatter_view()

Implements hook_field_formatter_view().

File

./videojs.module, line 76
Provides an HTML5-compatible with Flash-fallback video player.

Code

function videojs_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  if ($display['type'] !== 'videojs') {
    return array();
  }
  if (empty($items)) {
    return array();
  }
  videojs_utility::normalizeFieldItems($field['field_name'], $items, 'video/mp4');
  $settings = $display['settings'];
  $attributes = array();
  if (!empty($settings['width']) && !empty($settings['height'])) {
    $attributes['width'] = intval($settings['width']);
    $attributes['height'] = intval($settings['height']);
  }
  if ($settings['autoplay'] !== NULL) {
    $attributes['autoplay'] = $settings['autoplay'];
  }
  if ($settings['loop'] !== NULL) {
    $attributes['loop'] = $settings['loop'];
  }
  if ($settings['hidecontrols'] !== NULL) {
    $attributes['hidecontrols'] = $settings['hidecontrols'];
  }
  if ($settings['preload'] !== NULL) {
    $attributes['preload'] = $settings['preload'];
  }
  if ($settings['centeredplaybutton'] !== NULL) {
    $attributes['centeredplaybutton'] = $settings['centeredplaybutton'];
  }

  // Add the poster image.
  if (!empty($settings['posterimage_field']) && !empty($entity->{$settings['posterimage_field']})) {
    $images = field_get_items($entity_type, $entity, $settings['posterimage_field']);
    if (!empty($images)) {
      videojs_utility::normalizeFieldItems($settings['posterimage_field'], $images, 'image/jpeg', 'image/');
      array_unshift($items, array_shift($images));
    }
  }

  // Add the text tracks.
  if (!empty($settings['tracks_field'])) {
    $tracks = field_get_items($entity_type, $entity, $settings['tracks_field']);
    if (!empty($tracks)) {
      videojs_utility::normalizeFieldItems($settings['tracks_field'], $tracks, 'text/vtt', 'text/vtt');
      $items = array_merge($items, $tracks);
    }
  }

  // Post-process tracks.
  $hastracks = FALSE;
  foreach ($items as &$item) {
    if ($item['filemime'] != 'text/vtt') {
      continue;
    }

    // Try to find the language for subtitles by reading the description field.
    if (empty($item['langcode']) && !empty($item['description'])) {
      $language = videojs_utility::resolveLanguage($item['description']);
      if ($language !== NULL) {
        $item['langcode'] = $language[0];
        if (empty($item['label'])) {
          $item['label'] = $language[1];
        }
      }
    }
    $hastracks = TRUE;
  }

  // Select the default track.
  if ($hastracks && !empty($settings['defaulttrack'])) {
    foreach ($items as &$item) {
      if ($item['filemime'] != 'text/vtt') {
        continue;
      }
      switch ($settings['defaulttrack']) {
        case 'first':
          $item['default'] = TRUE;
          break 2;
        case 'user':
          global $language;
          if ($item['langcode'] == $language->language) {
            $item['default'] = TRUE;
            break 2;
          }
          break;
        default:
          if ($item['langcode'] == $settings['defaulttrack']) {
            $item['default'] = TRUE;
            break 2;
          }
          break;
      }
    }
  }
  $themefunction = 'videojs';

  // Special handling for file entity based videos that can be added using the media module.
  if ($entity_type == 'file' && !empty($entity->override)) {
    if (!empty($entity->override['wysiwyg'])) {

      // When in wysiwyg mode, use a special theme function because
      // media only handles images or spans in the wysiwyg area.
      $themefunction = 'videojs_media_wysiwyg_preview';
    }
    elseif (!empty($entity->override['attributes'])) {

      // Allow the user to override width and height by resizing the player
      // in the wysiwyg area.
      if (!empty($entity->override['attributes']['width'])) {
        $attributes['width'] = intval($entity->override['attributes']['width']);
      }
      if (!empty($entity->override['attributes']['height'])) {
        $attributes['height'] = intval($entity->override['attributes']['height']);
      }
      if (!empty($entity->override['attributes']['class'])) {
        $attributes['class'] = $entity->override['attributes']['class'];
      }
      else {
        $attributes['class'] = array();
      }
      $attributes['class'][] = 'videojs-' . $entity->fid;
      $attributes['class'][] = 'videojs-' . $entity->type;
    }
  }
  list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  return array(
    array(
      '#theme' => $themefunction,
      '#items' => $items,
      '#player_id' => 'videojs-' . $id . '-' . str_replace('_', '-', $instance['field_name']),
      '#attached' => videojs_add(FALSE),
      '#entity' => $entity,
      '#entity_type' => $entity_type,
      '#attributes' => $attributes,
      '#posterimage_style' => !empty($settings['posterimage_style']) ? $settings['posterimage_style'] : NULL,
    ),
  );
}