You are here

videojs.admin.inc in Video.js (HTML5 Video Player) 6.2

Administrative pages for the Video.js module.

File

includes/videojs.admin.inc
View source
<?php

/**
 * @file
 * Administrative pages for the Video.js module.
 */

/**
 * Menu callback; Provides the Video.js settings form.
 */
function videojs_settings_form() {
  $form = array();
  $form['videojs_directory'] = array(
    '#type' => 'textfield',
    '#title' => t('Video.js file directory'),
    '#default_value' => variable_get('videojs_directory', NULL),
    '#description' => t('Specify the path that contains the Video.js library. The video.js file should be in the root of this directory.'),
  );
  if (module_exists('libraries')) {
    $form['videojs_directory']['#description'] .= ' ' . t('Leave empty to detect the directory using the Libraries API.');
  }
  else {

    // Enter the default path if the libraries module is not preset.
    if (empty($form['videojs_directory']['#default_value'])) {
      $form['videojs_directory']['#default_value'] = 'sites/all/libraries/video-js';
    }
    $form['videojs_directory']['#required'] = TRUE;
  }
  $form['options'] = array(
    '#type' => 'fieldset',
    '#title' => t('Video.js options'),
    '#collapsible' => FALSE,
  );
  $form['options']['videojs_autoplay'] = array(
    '#type' => 'checkbox',
    '#title' => t('Auto-play files on page load'),
    '#description' => t('Use caution when combining this option with multiple players on the same page.'),
    '#default_value' => variable_get('videojs_autoplay', FALSE),
  );
  $form['options']['videojs_width'] = array(
    '#type' => 'textfield',
    '#title' => t('Player width'),
    '#default_value' => variable_get('videojs_width', 640),
  );
  $form['options']['videojs_height'] = array(
    '#type' => 'textfield',
    '#title' => t('Player height'),
    '#default_value' => variable_get('videojs_height', 264),
  );
  $form['thumbnails'] = array(
    '#type' => 'fieldset',
    '#title' => t('Video.js thumbnails'),
    '#collapsible' => FALSE,
    '#description' => t('Thumbnail settings for Video.js widget'),
  );
  foreach (_videojs_settings_thumbnail_fields() as $node_type => $value) {
    $form['thumbnails']['videojs_' . $node_type] = array(
      '#type' => 'select',
      '#title' => t('@fieldname in @contenttype', array(
        '@fieldname' => $value['video_js_field'],
        '@contenttype' => $value['description'],
      )),
      '#default_value' => variable_get('videojs_' . $node_type, module_exists('video') ? 'video_module' : $value['filefields'][0]),
      '#options' => module_exists('video') ? array(
        'video_module' => t('Video module'),
      ) + $value['filefields'] : $value['filefields'],
    );
  }
  $form = system_settings_form($form);
  $form['#validate'][] = 'videojs_settings_form_validate';
  array_unshift($form['#submit'], 'videojs_settings_form_submit');
  return $form;
}

/**
 * Validation function to validate the videojs_settings_form() form.
 */
function videojs_settings_form_validate($form, &$form_state) {
  if ($form_state['values']['videojs_directory'] == '' && module_exists('libraries')) {
    $form_state['values']['videojs_directory'] = NULL;
  }
  $form_state['videojs_version'] = videojs_get_version($form_state['values']['videojs_directory']);
  if (!$form_state['videojs_version']) {
    form_error($form['videojs_directory'], t('The directory specified does not seem to contain the Video.js library. Check to make sure that the video.js file is located within this directory.'));
  }
}

/**
 * Submit handler for the videojs_settings_form() form.
 *
 * This submit handler is executed before system_settings_form_submit().
 */
function videojs_settings_form_submit($form, &$form_state) {
  $v =& $form_state['values'];

  // Clean all variables before system_settings_form_submit() creates new ones.
  // This prevents variables for removed content types remaining in the variables table.
  db_query("DELETE FROM {variable} WHERE name LIKE 'videojs_%%'");

  // Store width & height as ints, autoplay as bool
  $v['videojs_height'] = intval($v['videojs_height']);
  $v['videojs_width'] = intval($v['videojs_width']);
  $v['videojs_autoplay'] = !empty($v['videojs_autoplay']);

  // Unset variables with empty values
  if ($v['videojs_directory'] == NULL) {
    unset($v['videojs_directory']);
  }
  if ($v['videojs_height'] == 0) {
    unset($v['videojs_height']);
  }
  if ($v['videojs_width'] == 0) {
    unset($v['videojs_width']);
  }
  drupal_set_message(t('The Video.js library (version @version) successfully found in the %directory directory.', array(
    '@version' => $form_state['videojs_version'],
    '%directory' => $form_state['values']['videojs_directory'],
  )));
}

/**
 * Generate list of content types with filefield widget set to 'videojs'
 *
 * @return
 *   Returns array in format [node_type]=>array(
 *     'description',
 *     'video_js_field',
 *     filefields=>array('filefield'=>'filefield description'))
 */
function _videojs_settings_thumbnail_fields() {
  $video_js_node_types = array();

  // Iterate through all node_types
  foreach (array_map('check_plain', node_get_types('names')) as $node_type => $description) {

    // Iterate through all filefield field types in the current node type
    foreach (filefield_get_field_list($node_type) as $field) {

      // If widget format of the field is 'videojs' - generate structure containing node_type, videojs field name and list of possible options for thumbnails
      if ($field['display_settings']['full']['format'] == 'videojs') {
        $video_js_node_types[$node_type] = array(
          'node_type' => $field['type_name'],
          'description' => $description,
          'video_js_field' => $field['field_name'],
          'filefields' => array(),
        );

        // Get all filefield type fields except 'videojs' field (should we check only compatible extentions allowed in some widgets?)
        foreach (filefield_get_field_list($node_type) as $thumbnail_field) {
          if ($thumbnail_field['display_settings']['full']['format'] != 'videojs') {
            $video_js_node_types[$node_type]['filefields'][$thumbnail_field['field_name']] = t($thumbnail_field['field_name']);
          }
        }
      }
    }
  }
  return $video_js_node_types;
}

Functions

Namesort descending Description
videojs_settings_form Menu callback; Provides the Video.js settings form.
videojs_settings_form_submit Submit handler for the videojs_settings_form() form.
videojs_settings_form_validate Validation function to validate the videojs_settings_form() form.
_videojs_settings_thumbnail_fields Generate list of content types with filefield widget set to 'videojs'