mojs-1.0.0-beta2/mojs.module
mojs.module
<?php
/**
* @file
* Drupal`s integration with mo.js library.
*
* "mo.js" - Is a javascript motion graphics library that is a fast,
* retina ready, modular and open source.
*
* GitHub: https://github.com/mojs/mojs
* Website: https://mojs.github.io/
* license: MIT licensed
*
* Copyright (C) 2016-2023 @legomushroom
*/
use Drupal\Core\Installer\InstallerKernel;
/**
* Check to make sure that mo.js library is installed.
*
* @return bool
* Flag indicating if the library is properly installed.
*/
function mojs_check_installed() {
// Throw error if mo.js library file not found.
/** @var Drupal\Core\Asset\LibraryDiscovery $library_discovery */
$library_discovery = \Drupal::service('library.discovery');
$library_exists = FALSE;
$definition = $library_discovery->getLibraryByName('mojs', 'mo.js');
// Check if $definition is an array before accessing its elements.
if (is_array($definition) && isset($definition['js'][0]['data'])) {
$library_exists = file_exists(DRUPAL_ROOT . '/' . $definition['js'][0]['data']);
}
return $library_exists;
}
/**
* Implements hook_page_attachments().
*/
function mojs_page_attachments(array &$attachments) {
// Don't add the Library during installation.
if (InstallerKernel::installationAttempted()) {
return;
}
// Check first MO.JS UI module is not installed and library not exists.
$moduleHandler = \Drupal::service('module_handler');
if (!$moduleHandler->moduleExists('mojs_ui')) {
// Check if the mo.js library is installed, then load
// from local otherwise it will be used from CDN.
if (mojs_check_installed()) {
$attachments['#attached']['library'][] = 'mojs/mo.js';
}
else {
$attachments['#attached']['library'][] = 'mojs/mo.cdn';
}
}
}
