You are here

mediaelement.module in MediaElement 8

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

Primary module file.

File

mediaelement.module
View source
<?php

/**
 * @file
 * Primary module file.
 */

/**
 * Implements hook_theme().
 */
function mediaelement_theme($existing, $type, $theme, $path) {
  $defaults = [
    'variables' => [
      'files' => [],
      'attributes' => NULL,
      'download_link' => FALSE,
      'download_text' => '',
    ],
    'path' => "{$path}/templates",
    'preprocess functions' => [
      'mediaelement_preprocess_mediaelement_file',
    ],
  ];
  return [
    'mediaelement_file_video' => [] + $defaults,
    'mediaelement_file_audio' => [] + $defaults,
  ];
}

/**
 * Implements hook_preprocess_html().
 */
function mediaelement_preprocess_html(&$variables) {
  $config = \Drupal::config('mediaelement.settings');
  $global_config = $config
    ->get('global_settings');
  $settings = mediaelement_parse_config($global_config);
  if ($settings['attachSitewide']) {
    $library = "mediaelement/mediaelement_{$config->get('library_settings.library_source')}";
    $variables['#attached']['library'][] = $library;
  }
  $variables['#attached']['drupalSettings']['mediaelement'] = $settings;
}

/**
 * Implements mediaelement_preprocess_mediaelement_file().
 */
function mediaelement_preprocess_mediaelement_file(&$variables) {
  $variables['attributes']
    ->addClass('mediaelementjs');
}

/**
 * Implements hook_library_info_build().
 */
function mediaelement_library_info_build() {
  $libraries = [];
  $library_config = \Drupal::config('mediaelement.settings')
    ->get('library_settings');

  // Create a dynamic library based for the configured CDN.
  if ($library_config['library_source'] != 'local') {
    $cdn_name = $library_config['library_source'];
    $cdn_url = mediaelement_get_cdn_url($cdn_name);
    $library_version = $library_config["{$cdn_name}_settings"]['library_version'];
    $cdn_path = "{$cdn_url}/{$library_version}";
    $libraries["mediaelement_{$cdn_name}"] = [
      'version' => $library_config["{$cdn_name}_settings"]['library_version'],
      'js' => [
        "{$cdn_path}/mediaelement-and-player.min.js" => [],
        "js/mediaelement.bundle.js" => [],
      ],
      'css' => [
        'component' => [
          "{$cdn_path}/mediaelementplayer.min.css" => [],
        ],
      ],
      'dependencies' => [
        'core/jquery',
        'core/jquery.once',
        'core/drupal',
        'core/drupalSettings',
      ],
    ];
  }
  return $libraries;
}

/**
 * Gets the base path for the configured CDN.
 *
 * @param string $cdn_name
 *   The name of the CDN to connect to.
 *
 * @return string
 *   The base URL of the CDN.
 */
function mediaelement_get_cdn_url($cdn_name) {
  $cdns = [
    'cdnjs' => '//cdnjs.cloudflare.com/ajax/libs/mediaelement',
  ];
  return $cdns[$cdn_name];
}

/**
 * Converts module into JS config.
 *
 * @param array $confg
 *   An array of config items.
 */
function mediaelement_parse_config($config) {
  $settings = [];
  $config = mediaelement_flatten_config($config);

  // Prepare config for Javascript.
  foreach ($config as $setting => $value) {

    // Camel case the setting name.
    $settingCamel = str_replace('_', '', lcfirst(ucwords($setting, '_')));
    $settings[$settingCamel] = $value;
  }
  return $settings;
}

/**
 * Flattens nested array into a single level array.
 *
 * @param array $array
 *   An array of items.
 * @param array $return
 *   The starting point of the return values.
 *
 * @return array
 *   The flattened array.
 */
function mediaelement_flatten_config($array, $return = []) {
  foreach ($array as $key => $value) {
    if (is_array($value)) {
      $return = mediaelement_flatten_config($value, $return);
    }
    else {
      $return[$key] = $value;
    }
  }
  return $return;
}

Functions

Namesort descending Description
mediaelement_flatten_config Flattens nested array into a single level array.
mediaelement_get_cdn_url Gets the base path for the configured CDN.
mediaelement_library_info_build Implements hook_library_info_build().
mediaelement_parse_config Converts module into JS config.
mediaelement_preprocess_html Implements hook_preprocess_html().
mediaelement_preprocess_mediaelement_file Implements mediaelement_preprocess_mediaelement_file().
mediaelement_theme Implements hook_theme().