Newer
Older
<?php
/**
* @file
* Provides an HTML5-compatible with Flash-fallback video player.
*
Jorrit Schippers
committed
* This module provides functionality for loading the Video.js library and
* formatters for CCK FileFields.
*/
/**
*/
function videojs_menu() {
$items = array();
$items['admin/config/media/videojs'] = array(
Jorrit Schippers
committed
'title' => 'Video.js',
'page callback' => 'drupal_get_form',
'page arguments' => array('videojs_settings_form'),
'access arguments' => array('administer site configuration'),
Jorrit Schippers
committed
'description' => 'Configure the settings for the Video.js module.',
'file' => 'includes/videojs.admin.inc',
);
return $items;
}
/**
*/
function videojs_theme() {
return array(
'videojs' => array(
Jorrit Schippers
committed
'variables' => array('items' => NULL, 'player_id' => NULL, 'attributes' => NULL, 'entity' => NULL, 'entity_type' => NULL, 'posterimage_style' => NULL),
'template' => 'theme/videojs',
'file' => 'includes/videojs.theme.inc',
),
'variables' => array('items' => NULL, 'player_id' => NULL, 'attributes' => NULL),
'template' => 'theme/videojs',
'file' => 'includes/videojs.theme.inc',
),
);
}
/**
*/
function videojs_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'videojs') . '/includes',
);
}
/**
*/
function videojs_field_formatter_info() {
return array(
'videojs' => array(
Jorrit Schippers
committed
'label' => t('Video.js : HTML5 Video Player'),
Jorrit Schippers
committed
'field types' => array('file', 'media', 'link_field'),
'description' => t('Display a video file as an HTML5-compatible with Flash-fallback video player.'),
Jorrit Schippers
committed
'settings' => array(
'width' => NULL,
'height' => NULL,
Jorrit Schippers
committed
'posterimage_field' => NULL,
'posterimage_style' => NULL,
Jorrit Schippers
committed
),
),
/**
* Implements hook_field_formatter_view().
*/
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) {
Jorrit Schippers
committed
// 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'];
}
// Manual override for WebM, because Drupal doesn't detect it.
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';
}
Jorrit Schippers
committed
}
$items[] = array(
Jorrit Schippers
committed
'uri' => url($link['url'], $link),
'filemime' => $mime,
);
}
}
Jorrit Schippers
committed
$settings = $display['settings'];
$attributes = array();
if (!empty($settings['width']) && !empty($settings['height'])) {
$attributes['width'] = intval($settings['width']);
$attributes['height'] = intval($settings['height']);
}
Jorrit Schippers
committed
// 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,
Jorrit Schippers
committed
'#attributes' => $attributes,
Jorrit Schippers
committed
'#posterimage_style' => !empty($settings['posterimage_style']) ? $settings['posterimage_style'] : NULL,
),
);
Jorrit Schippers
committed
}
/**
* Implements hook_field_formatter_settings_form().
*/
function videojs_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
Jorrit Schippers
committed
$image_styles = image_style_options(FALSE);
Jorrit Schippers
committed
Jorrit Schippers
committed
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Determine the image fields that will be selectable.
if ($instance['entity_type'] == 'ctools' && $instance['bundle'] == 'ctools') {
// This is a fake instance (see ctools_fields_fake_field_instance())
// Display all image fields in bundles that contain this field.
$otherfields = field_info_fields();
foreach ($otherfields as $otherfield) {
if ($otherfield['type'] == 'image' && !empty($otherfield['bundles'])) {
// Find a label by finding an instance label
$instancelabels = array();
$bundles_names = array();
foreach ($otherfield['bundles'] as $otherentitytype => $otherbundles) {
foreach ($otherbundles as $otherbundle) {
// Check if this image field appears in one of the video field bundles.
if (isset($field['bundles'][$otherentitytype]) && in_array($otherbundle, $field['bundles'][$otherentitytype])) {
$otherinstance = field_info_instance($otherentitytype, $otherfield['field_name'], $otherbundle);
$instancelabels[$otherinstance['label']] = isset($instancelabels[$otherinstance['label']]) ? $instancelabels[$otherinstance['label']] + 1 : 1;
$bundles_names[] = t('@entity:@bundle', array('@entity' => $otherentitytype, '@bundle' => $otherbundle));
}
}
}
if (!empty($instancelabels)) {
arsort($instancelabels);
$instancelabel = key($instancelabels);
$imagefields[$otherfield['field_name']] = $instancelabel . ' — ' . t('Appears in: @bundles.', array('@bundles' => implode(', ', $bundles_names)));
}
}
}
}
else {
$otherinstances = field_info_instances($instance['entity_type'], $instance['bundle']);
$imagefields = array();
foreach ($otherinstances as $otherinstance) {
$otherfield = field_info_field_by_id($otherinstance['field_id']);
if ($otherfield['type'] == 'image') {
$imagefields[$otherinstance['field_name']] = $otherinstance['label'];
}
Jorrit Schippers
committed
}
}
$form = array(
Jorrit Schippers
committed
'#element_validate' => array('videojs_field_formatter_settings_form_validate'),
);
Jorrit Schippers
committed
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
$form['width'] = array(
'#type' => 'textfield',
'#title' => t('Width'),
'#default_value' => $settings['width'],
'#size' => 6,
'#maxlength' => 5,
'#element_validate' => array('element_validate_integer_positive'),
);
$form['height'] = array(
'#type' => 'textfield',
'#title' => t('Height'),
'#default_value' => $settings['height'],
'#size' => 6,
'#maxlength' => 5,
'#element_validate' => array('element_validate_integer_positive'),
);
if (!empty($imagefields)) {
$form['posterimage_field'] = array(
'#type' => 'select',
'#title' => t('Poster image field'),
'#default_value' => $settings['posterimage_field'],
'#options' => $imagefields,
'#description' => t('If an image is uploaded to the field above it will be used as the poster image.'),
'#empty_value' => NULL,
'#empty_option' => t('- None -'),
);
}
$form['posterimage_style'] = array(
'#title' => t('Poster image style'),
'#type' => 'select',
'#default_value' => $settings['posterimage_style'],
'#empty_option' => t('None (original image)'),
'#description' => t('The original video thumbnail will be displayed. Otherwise, you can add a custom image style at !settings.', array('!settings' => l(t('media image styles'), 'admin/config/media/image-styles'))),
'#options' => $image_styles,
);
return $form;
Jorrit Schippers
committed
function videojs_field_formatter_settings_form_validate($form, $form_state) {
$width = $form['width']['#value'];
$height = $form['height']['#value'];
// The fields need to be both entered or both empty
if (empty($width) != empty($height)) {
form_error($form[empty($width) ? 'height' : 'width'], t('The width and height field need to be both set or both empty.'));
}
}
/**
* Implements hook_field_formatter_settings_summary().
*/
function videojs_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
Jorrit Schippers
committed
$image_styles = image_style_options(FALSE);
Jorrit Schippers
committed
Jorrit Schippers
committed
$output = t('Player dimensions: @widthxheight', array(
Jorrit Schippers
committed
'@widthxheight' => !empty($settings['width']) && !empty($settings['height']) ? $settings['width'] . 'x' . $settings['height'] : t('default'),
));
Jorrit Schippers
committed
if (!empty($settings['posterimage_field'])) {
$imageinstance = field_info_instance($instance['entity_type'], $settings['posterimage_field'], $instance['bundle']);
if ($imageinstance != NULL) {
$output .= '<br/>';
$output .= t('Poster image field') . ': ' . check_plain($imageinstance['label']);
}
}
$output .= '<br/>';
if (isset($image_styles[$settings['posterimage_style']])) {
$output .= t('Poster image style') . ': ' . check_plain($image_styles[$settings['posterimage_style']]);
}
else {
$output .= t('Poster image style') . ': ' . t('None');
}
return $output;
Jorrit Schippers
committed
}
Jorrit Schippers
committed
* Add the Video.js library to the page.
*
* @param $add
* By default this function will add videojs to the page JavaScript array
Jorrit Schippers
committed
* 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;
Jorrit Schippers
committed
if (!isset($added)) {
$added = &drupal_static(__FUNCTION__);
}
$directory = variable_get('videojs_directory', 'sites/all/libraries/video-js');
Jorrit Schippers
committed
if (!file_exists($directory . '/video.min.js')) {
return FALSE;
Jorrit Schippers
committed
$libjs = $directory . '/video.min.js';
$libcss = $directory . '/video-js.min.css';
if ($add && !$added) {
drupal_add_js($libjs, array('group' => JS_LIBRARY));
drupal_add_css($libcss);
$added = TRUE;
Jorrit Schippers
committed
return array(
'js' => array(
$libjs => array('group' => JS_LIBRARY),
),
'css' => array($libcss),
);
Jorrit Schippers
committed
* Return the version of Video.js installed.
Jorrit Schippers
committed
* The directory to check for a Video.js installation.
*/
function videojs_get_version($directory = NULL) {
Jorrit Schippers
committed
$version = NULL;
if (!isset($directory)) {
$directory = variable_get('videojs_directory', 'sites/all/libraries/video-js');
}
Jorrit Schippers
committed
return NULL;
$contents = file_get_contents($directory . '/video.js');
Jorrit Schippers
committed
$matches = array();
if (preg_match('/(?:v[ ]*|Version )([\d.]+)/i', $contents, $matches)) {
$version = $matches[1];
}
return $version;
}
/**
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
*/
function videojs_libraries_info() {
$libraries = array();
$libraries['video-js'] = array(
'name' => 'Video.js',
'vendor url' => 'http://videojs.com',
'download url' => 'https://github.com/zencoder/video-js/downloads',
'version arguments' => array(
'file' => 'video.js',
'pattern' => '/(?:v[ ]*|Version )([\d.]+)/i',
'lines' => 10,
'cols' => 50,
),
'versions' => array(
'2' => array(
'files' => array(
'js' => array('video.js' => array('group' => JS_LIBRARY)),
'css' => array('video-js.css'),
),
),
'3' => array(
'files' => array(
'js' => array('video.min.js' => array('group' => JS_LIBRARY)),
'css' => array('video-js.min.css'),
),
'variants' => array(
'source' => array(
'files' => array(
'js' => array('video.js' => array('group' => JS_LIBRARY)),
'css' => array('video-js.css'),
),
'minified' => array(
'files' => array(
'js' => array('video.min.js' => array('group' => JS_LIBRARY)),
'css' => array('video-js.min.css'),
),
),
),
),
),
),
);
return $libraries;
}