You are here

public static function videojs_utility::normalizeFieldItems in Video.js (HTML5 Video Player) 7.3

Normalizes field items so link items and file items become similar.

Also fills in missing mime types and optionally filters on mime type.

1 call to videojs_utility::normalizeFieldItems()
videojs_field_formatter_view in ./videojs.module
Implements hook_field_formatter_view().

File

includes/videojs.utility.inc, line 240
This file will be used to keep all utility functions.

Class

videojs_utility
Helper functions for the Video.js module.

Code

public static function normalizeFieldItems($field_name, array &$items, $defaultmime, $mimefilter = NULL) {
  if (empty($items)) {
    return;
  }
  $field = field_info_field($field_name);
  if ($field['type'] == 'link_field') {
    foreach ($items as $key => $link) {
      $title = $link['title'];

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

        // If the mime type is application/octet-stream, use the default.
        // This happens for instance for links without extensions.
        if ($mime == 'application/octet-stream') {
          $mime = $defaultmime;
        }
      }
      $items[$key] = array(
        'uri' => url($link['url'], $link),
        'filemime' => $mime,
        'description' => $title,
      );
    }
  }
  else {

    // Make sure the item is fully loaded. Image fields of file entities can only have a fid and no additional data.
    foreach ($items as $key => $item) {
      if (isset($item['fid']) && !isset($item['filemime']) && $item['fid'] > 0) {
        $file = file_load($item['fid']);
        $items[$key] = $item + (array) $file;
      }
    }
  }
  if (!empty($mimefilter)) {
    $filterlength = strlen($mimefilter);
    foreach ($items as $k => $item) {
      if (strncmp($item['filemime'], $mimefilter, $filterlength) !== 0) {
        unset($items[$k]);
      }
    }
  }
}