partytown_drupal-1.0.5/partytown_drupal.module
partytown_drupal.module
<?php
/**
* @file
* The module file for partytown_drupal.
*
* The main job of this file is to inject Partytown's settings interface
* and load the Partytown libraries.
*/
/**
* Implements hook_preprocess_html().
*/
function partytown_drupal_preprocess_html(&$variables) {
$config = \Drupal::config('partytown_drupal.settings');
$enable = $config->get('enable');
// Do nothing if disabled.
if (!$enable) {
return;
}
// Bind all of the settings to a settings array.
$debug = $config->get('debug');
$partytown_drupal_settings = [
'debug' => $debug,
'lib' => $config->get('lib'),
'resolveUrl' => $config->get('resolve_url'),
'fallbackTimeout' => $config->get('fallback_timeout'),
];
// Parse forwarding.
$forwards = $config->get('forward');
if (!empty($forwards)) {
foreach ($forwards as $forward) {
$preserve_behavior = [
'preserveBehavior' => $forward['preserve_behavior'] == 1 ? TRUE : FALSE,
];
$partytown_drupal_settings['forward'][] = [$forward['function'], $preserve_behavior];
}
}
// Parse main thread calls.
$load_scripts_on_main_thread = [];
if (!empty($config->get('load_scripts_on_main_thread'))) {
$value = preg_replace('/\\r/', '', $config->get('load_scripts_on_main_thread'));
$load_scripts_on_main_thread = explode(PHP_EOL, $value);
}
$partytown_drupal_settings['loadScriptsOnMainThread'] = $load_scripts_on_main_thread;
// Attach settings manager library.
$variables['#attached']['library'][] = 'partytown_drupal/partytown.settings';
// Attach the requested Partytown library.
if ($debug === TRUE) {
$variables['#attached']['library'][] = 'partytown_drupal/partytown.debug';
}
else {
$variables['#attached']['library'][] = 'partytown_drupal/partytown';
}
// Attach partytown settings to drupalSettings.
$variables['#attached']['drupalSettings']['partytownDrupal'] = $partytown_drupal_settings;
}
