You are here

function video_field_formatter_view in Video 7.2

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

Implements hook_field_formatter_view().

File

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

Code

function video_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  $settings = $display['settings'];
  foreach ($items as $delta => $item) {
    switch ($display['type']) {
      case 'video_formatter_thumbnail':

        // Check if the formatter involves a link.
        if ($settings['image_link'] == 'content') {
          $uri = entity_uri($entity_type, $entity);
        }
        elseif ($settings['image_link'] == 'file') {
          $uri = array(
            'path' => file_create_url($item['uri']),
            'options' => array(),
          );
        }
        elseif ($settings['image_link'] == 'colorbox') {
          list($entity_id, $rev, $bundle) = entity_extract_ids($entity_type, $entity);

          // Convert to a URL friendly version, and remove "field_" prefix for brevity.
          $field_path = preg_replace(array(
            '/^field_/',
            '/_/',
          ), array(
            '',
            '-',
          ), $field['field_name']);
          $field_path = "{$entity_type}/{$entity_id}/{$field_path}";
          $dimensions = explode('x', $settings['widthxheight']);
          $uri = array(
            'path' => 'video/embed/' . $field_path . '/' . str_replace('x', '/', $settings['widthxheight']),
            'options' => array(
              'query' => array(
                'width' => $dimensions[0],
                'height' => $dimensions[1] + 10,
                'iframe' => 'true',
              ),
            ),
          );
        }
        $element[$delta] = array(
          '#theme' => 'video_formatter_thumbnail',
          '#item' => $item,
          '#image_style' => $settings['image_style'],
          '#path' => isset($uri) ? $uri : '',
          '#entity' => $entity,
          '#entity_type' => $entity_type,
          '#field' => $field,
          '#instance' => $instance,
          '#colorbox' => $settings['image_link'] == 'colorbox',
        );
        break;
      case 'video_formatter_player':
        $element[$delta] = array(
          '#theme' => 'video_formatter_player',
          '#item' => $item,
          '#entity' => $entity,
          '#entity_type' => $entity_type,
          '#field' => $field,
          '#instance' => $instance,
          '#player_dimensions' => $settings['widthxheight'],
          '#poster_image_style' => $settings['poster_image_style'],
        );
        break;
      case 'video_formatter_url':

        // If the original file is selected then we just return that
        if ($settings['video_extension'] == 'original') {
          $title = empty($settings['link_title']) ? file_create_url($item['uri']) : $settings['link_title'];
          $element[$delta] = array(
            '#markup' => l($title, file_create_url($item['uri'])),
          );
          break;
        }

        // Nobody believes in standards
        // @TODO revisit this, there must be a better way to replace the mimetypes
        $extensions_fix = array(
          'flv' => 'x-flv',
        );
        if (!empty($extensions_fix[$settings['video_extension']])) {
          $extension = $extensions_fix[$settings['video_extension']];
        }
        else {
          $extension = $settings['video_extension'];
        }
        $title = empty($settings['link_title']) ? NULL : $settings['link_title'];

        // Find the file based on the selected video type
        foreach ($item['playablefiles'] as $file) {
          if (strstr($file->filemime, 'video/' . $extension)) {
            $file_url = file_create_url($file->uri);

            // Set the title to the filename if it's not set
            if (empty($title)) {
              $title = file_create_url($file->uri);
            }

            // Stop looking (shouldn't break out of switch)

            //break;
          }
        }
        $element[$delta] = array(
          '#markup' => empty($item['uri']) ? '' : l($title, $item['uri']),
        );
        break;
    }
  }
  return $element;
}