contextly-8.x-2.1/src/ContextlyLibraryService.php
src/ContextlyLibraryService.php
<?php
namespace Drupal\contextly;
use Drupal\Component\Serialization\Json;
use Drupal\contextly\Contextly\ContextlyDrupalKit;
/**
* The ContextlyLibraryService class.
*/
class ContextlyLibraryService {
/**
* ContextlyLibraryService constructor.
*
* @param \Drupal\Component\Serialization\Json $serializationJson
* The serialization json.
*/
public function __construct(
protected readonly Json $serializationJson,
) {
}
/**
* Return the libraries.
*
* @return array
* The libraries array.
*/
public function addLibbraries(): array {
$libraries = [];
// $module_path = drupal_get_path('module', 'contextly');
// $js_path = $module_path . '/js/';
// Libraries from the Kit.
$map = $this->getLibrariesMap();
$kit = ContextlyDrupalKit::getInstance();
$assets_manager = $kit->newAssetsManager();
foreach ($map as $key => $info) {
$info += [
'ignore' => [],
'library' => [],
];
$info['library'] += [
'version' => ContextlyDrupalKit::version(),
];
$assets = $kit->newAssetsList();
$assets_manager->extractPackageAssets($info['package'], $assets, $info['ignore']);
$library = $kit->newDrupalAssetsLibraryRenderer($assets)
->renderAll();
$library += $info['library'];
$libraries[$key] = $library;
}
// Add runtime json2 loading to the EasyXDM.
$assets = $kit->newAssetsList();
$assets_manager->extractPackageAssets('libraries/json2', $assets);
$script = [];
foreach ($assets->buildJsUrls() as $url) {
$script[] = 'easyXDM.DomHelper.requiresJSON(' . $this->serializationJson->encode($url) . ');';
}
$libraries['easy-xdm']['js'][] = [
'type' => 'inline',
'data' => implode("\n", $script),
];
return $libraries;
}
/**
* Return the libraries map.
*
* @return array
* The libraries map array.
*/
protected function getLibrariesMap(): array {
// Libraries from the Kit.
return [
'editor-overlay' => [
'package' => 'overlay-dialogs/overlay',
'ignore' => [
'libraries/jquery' => TRUE,
],
'library' => [
'dependencies' => [
'system/jquery',
],
],
],
'easy-xdm' => [
'package' => 'libraries/easy-xdm',
],
'widget' => [
'package' => 'widgets/page-view',
'ignore' => [
'libraries/jquery' => TRUE,
'libraries/easy-xdm' => TRUE,
],
'library' => [
'dependencies' => [
'system/jquery',
'contextly/easy-xdm',
],
],
],
];
}
}
