You are here

javascript_libraries.module in JavaScript Libraries Manager 8

Same filename and directory in other branches
  1. 7 javascript_libraries.module

Toggle the inclusion of Drupal system libraries. Upload and reference custom libraries as well.

File

javascript_libraries.module
View source
<?php

/**
 * @file
 * Toggle the inclusion of Drupal system libraries. Upload and reference custom libraries as well.
 */
use Drupal\Core\File;

/**
 * Helper function that validates the external url.
 * @param $url
 * @return bool
 */
function javascript_libraries_valid_external_url($url) {
  $parts = parse_url($url);
  return $parts && ($parts['scheme'] == 'http' || $parts['scheme'] == 'https') && $parts['host'] && preg_match('@/.+\\.(js|txt)$@i', $parts['path']);
}

/**
 * Implements hook_page_attachments().
 * Used to add the javascript files in the html head section.
 * @param array $page
 */
function javascript_libraries_page_attachments(array &$page) {
  $external_lib = \Drupal::config('javascript_libraries.settings')
    ->get('javascript_libraries_custom_libraries');
  $externals = array();
  $count = 1;
  foreach ($external_lib as $key => $lib) {
    if ($lib['scope'] == 'header') {
      if ($lib['type'] == 'file') {
        $lib['uri'] = file_create_url($lib['uri']);
      }
      $description[$key] = [
        '#type' => 'html_tag',
        // The HTML tag to add, in this case a  tag.
        '#tag' => 'script',
        '#attributes' => array(
          'src' => $lib['uri'],
        ),
      ];
      $page['#attached']['html_head'][] = [
        $description[$key],
        'description' . $count++,
      ];
    }
  }
}

/**
 * Implements hook_library_info_build().
 * Used in order to declare new library and attach dependency to it.
 * @return mixed
 */
function javascript_libraries_library_info_build() {
  $external_lib = \Drupal::config('javascript_libraries.settings')
    ->get('javascript_libraries_custom_libraries');
  $externals = array();
  foreach ($external_lib as $key => $lib) {
    if ($lib['scope'] == 'footer') {
      if ($lib['type'] == 'file') {
        $lib['uri'] = file_create_url($lib['uri']);
      }
      $externals[$lib['uri']] = array(
        'type' => 'external',
        'minified' => 'true',
      );
    }
  }
  $core_lib = \Drupal::config('javascript_libraries.settings')
    ->get('javascript_libraries_core_libraries');
  foreach ($core_lib as $key => $lib) {
    $dependencies[] = $lib;
  }
  $libraries['external']['dependencies'] = $dependencies;
  $libraries['external']['js'] = $externals;
  return $libraries;
}

/**
 * Implements hook_preprocess_page.
 * @param $variables
 */
function javascript_libraries_preprocess_page(&$variables) {
  $variables['#attached']['library'][] = 'javascript_libraries/external';
}

Functions

Namesort descending Description
javascript_libraries_library_info_build Implements hook_library_info_build(). Used in order to declare new library and attach dependency to it.
javascript_libraries_page_attachments Implements hook_page_attachments(). Used to add the javascript files in the html head section.
javascript_libraries_preprocess_page Implements hook_preprocess_page.
javascript_libraries_valid_external_url Helper function that validates the external url.