You are here

function jw_player_field_formatter_view in JW Player 7.2

Same name and namespace in other branches
  1. 7 jw_player.module \jw_player_field_formatter_view()

Implements hook_field_formatter_view().

File

./jw_player.module, line 492
Adds a theme function which allows theme developers to use the JW Player.

Code

function jw_player_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();

  // Exclude files without a supported media format (if enabled).
  if (!empty($display['settings']['check_support'])) {
    $items = array_filter($items, 'jw_player_supports');
  }

  // Support for transcoded videos in Video a field.
  if ($field['type'] == 'video') {
    foreach ($items as $delta => $item) {
      if (!empty($file['playablefiles'])) {
        $items[$delta] = reset($file['playablefiles']);
      }
    }
  }
  if ($display['type'] == 'jw_player') {

    // Prepare preview image.
    $image = NULL;
    if ($display['settings']['preview_image_field']) {
      $split = explode('|', $display['settings']['preview_image_field']);
      $preview_image_field = $split[1];
      $preview_image_entity_type = explode(':', $split[0]);
      if ($preview_items = field_get_items($preview_image_entity_type[0], $entity, $preview_image_field)) {
        $image = image_style_url($display['settings']['preview_image_style'], $preview_items[0]['uri']);
      }
    }

    // Process files for the theme function.
    foreach ($items as $delta => $item) {

      // Give each instance of the player a unique id. The combination
      // of fid and preset are considered to be save even in cases where
      // the entire theme functions output is cached.
      // Prefixing the id makes sure that the id does not start with a
      // invalid numeric value.
      $id = $item['fid'] . $display['settings']['jwplayer_preset'];
      $element[$delta] = array(
        '#theme' => 'jw_player',
        '#file' => $item,
        '#preset' => $display['settings']['jwplayer_preset'],
        '#html_id' => drupal_html_id('jwplayer' . $id),
      );

      // Add preview image.
      if ($image) {
        $element[$delta]['#image'] = $image;
      }
    }
  }
  if ($display['type'] == 'jw_player_playlist') {

    // Process files for the theme function.
    $id = $items[0]['fid'] . $display['settings']['jwplayer_preset'];
    $element = array(
      '#theme' => 'jw_player',
      '#playlist' => $items,
      '#preset' => $display['settings']['jwplayer_preset'],
      '#html_id' => drupal_html_id('jwplayer' . $id),
      '#playlist_size' => $display['settings']['playlist_size'],
      '#playlist_position' => $display['settings']['playlist_position'],
    );
  }
  if ($display['type'] == 'jw_player_sources') {

    // Process files for the theme function.
    $id = $items[0]['fid'] . $display['settings']['jwplayer_preset'];
    $element = array(
      '#theme' => 'jw_player',
      '#sources' => $items,
      '#preset' => $display['settings']['jwplayer_preset'],
      '#html_id' => drupal_html_id('jwplayer' . $id),
    );
    if ($images = field_get_items($entity_type, $entity, 'field_video_thumbnail')) {
      $element['#image'] = $images[0];
    }
  }
  return $element;
}