You are here

videojs.module in Video.js (HTML5 Video Player) 6

Provides an HTML5-compatible with Flash-fallback video player.

This module provides functionality for loading the Video.js library and formatters for CCK FileFields.

File

videojs.module
View source
<?php

/**
 * @file
 * Provides an HTML5-compatible with Flash-fallback video player.
 *
 * This module provides functionality for loading the Video.js library and
 * formatters for CCK FileFields.
 */

/**
 * Implementation of hook_menu().
 */
function videojs_menu() {
  $items = array();
  $items['admin/settings/videojs'] = array(
    'title' => 'Video.js',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'videojs_settings_form',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'description' => 'Configure the settings for the Video.js module.',
    'file' => 'includes/videojs.admin.inc',
  );
  return $items;
}

/**
 * Implementation of hook_theme().
 */
function videojs_theme() {
  return array(
    'videojs_formatter_videojs' => array(
      'arguments' => array(
        'element' => NULL,
      ),
      'template' => 'theme/videojs',
      'file' => 'includes/videojs.theme.inc',
    ),
    'videojs_view' => array(
      'arguments' => array(
        'view' => NULL,
        'items' => NULL,
      ),
      'template' => 'theme/videojs',
      'file' => 'includes/videojs.theme.inc',
    ),
    'videojs' => array(
      'arguments' => array(
        'items' => NULL,
        'player_id' => NULL,
        'attributes' => NULL,
      ),
      'template' => 'theme/videojs',
      'file' => 'includes/videojs.theme.inc',
    ),
  );
}

/**
 * Implementation of Views' hook_views_api().
 */
function videojs_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'videojs') . '/includes',
  );
}

/**
 * Implementation of CCK's hook_field_formatter_info().
 */
function videojs_field_formatter_info() {
  return array(
    'videojs' => array(
      'label' => t('Video.js : HTML5 Video Player'),
      'field types' => array(
        'filefield',
        'uploadfield',
      ),
      'multiple values' => CONTENT_HANDLE_MODULE,
      'description' => t('Display a video file as an HTML5-compatible with Flash-fallback video player.'),
    ),
  );
}

/**
 * Get the path to the video.js external library.
 *
 * @return
 *   The path to the directory where video.js library is located.
 */
function videojs_get_path() {
  $directory = variable_get('videojs_directory', 'sites/all/libraries/video-js');
  if (!file_exists($directory . '/video.js') && module_exists('libraries')) {

    // Libraries API integration
    $directory = libraries_get_path('video-js');
  }
  return $directory;
}

/**
 * Add the Video.js library to the page.
 *
 * @param $add
 *   By default this function will add Video.js to the page JavaScript array
 *   directly. If wanting to store the Video.js file as an #attached property,
 *   set this to FALSE and videojs_add() will only return the needed array
 *   suitable for use as an #attached property.
 */
function videojs_add($add = TRUE) {
  static $added = FALSE;
  $directory = videojs_get_path();
  $return = FALSE;
  if (file_exists($directory . '/video.js')) {
    $filepath = $directory . '/video.js';
    $filepath_css = $directory . '/video-js.css';
  }
  if (isset($filepath)) {
    $videojs_js = drupal_get_path('module', 'videojs') . '/theme/videojs.js';
    $videojs_css = drupal_get_path('module', 'videojs') . '/theme/videojs.css';
    $settings = array(
      'videojs' => array(
        'swfPath' => base_path() . videojs_get_path(),
        'autoPlay' => (int) variable_get('videojs_autoplay', ''),
      ),
    );
    if ($add) {
      drupal_add_js($filepath);
      drupal_add_css($filepath_css);
      drupal_add_js($videojs_js);
      drupal_add_css($videojs_css);
      if (!$added) {
        drupal_add_js($settings, 'setting');
        $added = TRUE;
      }
    }
    $return = array(
      'js' => array(
        array(
          'data' => $filepath,
        ),
        array(
          'data' => $videojs_js,
        ),
        array(
          'data' => $settings,
          'type' => 'setting',
        ),
      ),
      'css' => array(
        array(
          'data' => $filepath_css,
        ),
        array(
          'data' => $videojs_css,
        ),
      ),
    );
  }
  return $return;
}

/**
 * Return the version of Video.js installed.
 *
 * @param $directory
 *   The directory to check for a Video.js installation.
 */
function videojs_get_version($directory = NULL) {
  $version = NULL;
  if (!isset($directory)) {
    $directory = videojs_get_path();
  }
  if (!file_exists($directory . '/video.js')) {
    return NULL;
  }
  $contents = file_get_contents($directory . '/video.js');
  $matches = array();
  if (preg_match('/(?:v[ ]*|Version )([\\d.]+)/i', $contents, $matches)) {
    $version = $matches[1];
  }
  return $version;
}

Functions

Namesort descending Description
videojs_add Add the Video.js library to the page.
videojs_field_formatter_info Implementation of CCK's hook_field_formatter_info().
videojs_get_path Get the path to the video.js external library.
videojs_get_version Return the version of Video.js installed.
videojs_menu Implementation of hook_menu().
videojs_theme Implementation of hook_theme().
videojs_views_api Implementation of Views' hook_views_api().