You are here

mediaelement.module in MediaElement 7

Same filename and directory in other branches
  1. 8 mediaelement.module
  2. 6 mediaelement.module
  3. 7.2 mediaelement.module

Provides HTML5 video and audio elements using Mediaelement.js.

File

mediaelement.module
View source
<?php

/**
 * @file
 * Provides HTML5 video and audio elements using Mediaelement.js.
 */

// @todo This could be removed when http://drupal.org/node/977052 is fixed.
module_load_include('inc', 'mediaelement', 'mediaelement.field');

/**
 * Implements hook_menu().
 */
function mediaelement_menu() {
  return array(
    'admin/config/media/mediaelement' => array(
      'title' => 'MediaElement.js',
      'description' => 'Settings for MediaElement.js integration with Drupal',
      'page callback' => 'drupal_get_form',
      'page arguments' => array(
        'mediaelement_admin_form',
      ),
      'access arguments' => array(
        'administer site configuration',
      ),
      'file' => 'mediaelement.admin.inc',
    ),
  );
}

/**
 * Implements hook_library().
 */
function mediaelement_library() {
  $path = libraries_get_path('mediaelement');
  $libraries = array();
  $libraries['mediaelement'] = array(
    'title' => 'Media Element',
    'website' => 'http://mediaelementjs.com/',
    'version' => '4',
    'js' => array(
      // The mediaelement script detects the path to itself to call other files
      // in the same location. With preprocessing this is problematic as the
      // script is no longer in the same directory as its other assets. There
      // is an option that can be passed into the script with its location.
      // @todo Update all calls to mediaelement to pass in the assets location.
      $path . '/build/mediaelement-and-player.min.js' => array(
        'group' => JS_LIBRARY,
        'preprocess' => FALSE,
      ),
    ),
    'css' => array(
      $path . '/build/mediaelementplayer.min.css' => array(
        'group' => CSS_SYSTEM,
      ),
    ),
  );
  return $libraries;
}

/**
 * Implements hook_init().
 */
function mediaelement_init() {

  // When the media player is set to be on all pages add it to the page.
  if (variable_get('mediaelement_sitewide', FALSE)) {
    drupal_add_library('mediaelement', 'mediaelement');
    drupal_add_js(drupal_get_path('module', 'mediaelement') . '/mediaelement.js');
    drupal_add_js(array(
      'mediaelementAll' => TRUE,
    ), array(
      'type' => 'setting',
    ));
  }
}

/**
 * Implements hook_theme().
 */
function mediaelement_theme() {
  return array(
    'mediaelement_video' => array(
      'variables' => array(
        'attributes' => array(),
        'settings' => array(),
        'file' => NULL,
      ),
    ),
    'mediaelement_audio' => array(
      'variables' => array(
        'attributes' => array(),
        'settings' => array(),
        'file' => NULL,
      ),
    ),
  );
}

/**
 * Implements theme_mediaelement_video().
 */
function theme_mediaelement_video($variables) {
  $output = '<div class="mediaelement-video">';
  if (!empty($variables['settings']['description']) && !empty($variables['settings']['show_description']) && $variables['settings']['show_description'] != 'none') {
    $description_markup = '<p class="mediaelement-description">' . $variables['settings']['description'] . '</p>';
  }
  $output .= '<video ' . drupal_attributes($variables['attributes']) . ' ></video>';
  if (!empty($variables['settings']['show_description']) && $variables['settings']['show_description'] == 'above') {
    $output .= $description_markup;
  }
  if ($variables['settings']['download_link']) {
    $output .= '<div class="mediaelement-download-link"><a href="' . $variables['attributes']['src'] . '" download>' . filter_xss_admin($variables['settings']['download_text']) . '</a></div>';
  }
  if (!empty($variables['settings']['show_description']) && $variables['settings']['show_description'] == 'below') {
    $output .= $description_markup;
  }
  $output .= '</div>';
  return $output;
}

/**
 * Implements theme_mediaelement_audio().
 */
function theme_mediaelement_audio($variables) {
  $output = '<div class="mediaelement-audio">';
  if (!empty($variables['settings']['description']) && !empty($variables['settings']['show_description']) && $variables['settings']['show_description'] != 'none') {
    $description_markup = '<p class="mediaelement-description">' . $variables['settings']['description'] . '</p>';
  }
  $output .= '<audio ' . drupal_attributes($variables['attributes']) . ' ></audio>';
  if (!empty($variables['settings']['show_description']) && $variables['settings']['show_description'] == 'above') {
    $output .= $description_markup;
  }
  if ($variables['settings']['download_link']) {
    $output .= '<div class="mediaelement-download-link"><a href="' . $variables['attributes']['src'] . '" download>' . filter_xss_admin($variables['settings']['download_text']) . '</a></div>';
  }
  if (!empty($variables['settings']['show_description']) && $variables['settings']['show_description'] == 'below') {
    $output .= $description_markup;
  }
  $output .= '</div>';
  return $output;
}