You are here

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

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

Implements hook_field_formatter_view().

File

./videojs.module, line 78
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();
  }
  if ($field['type'] == 'link_field') {
    $links = $items;
    $items = array();
    foreach ($links as $link) {

      // Allow the user to override the mime type using the title field
      if (!empty($link['title']) && strncmp('video/', $link['title'], 6) === 0) {
        $mime = $link['title'];
      }
      elseif (substr($link['url'], -5) == '.webm') {
        $mime = 'video/webm';
      }
      else {
        $mime = DrupalLocalStreamWrapper::getMimeType($link['url']);

        // If the mime type is application/octet-stream, default to MP4.
        // This happens for instance for links without extensions.
        if ($mime == 'application/octet-stream') {
          $mime = 'video/mp4';
        }
      }
      $items[] = array(
        'uri' => url($link['url'], $link),
        'filemime' => $mime,
      );
    }
  }
  $settings = $display['settings'];
  $attributes = array();
  if (!empty($settings['width']) && !empty($settings['height'])) {
    $attributes['width'] = intval($settings['width']);
    $attributes['height'] = intval($settings['height']);
  }

  // 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)) {
      array_unshift($items, array_shift($images));
    }
  }
  list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  return array(
    array(
      '#theme' => 'videojs',
      '#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,
    ),
  );
}