function system_js_settings_alter in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/system/system.module \system_js_settings_alter()
Implements hook_js_settings_alter().
Sets values which depend on the current request, like core/drupalSettings as well as theme_token ajax state.
File
- core/
modules/ system/ system.module, line 648 - Configuration system that lets administrators modify the workings of the site.
Code
function system_js_settings_alter(&$settings, AttachedAssetsInterface $assets) {
$request = \Drupal::request();
$current_query = $request->query
->all();
// Let output path processors set a prefix.
/** @var \Drupal\Core\PathProcessor\OutboundPathProcessorInterface $path_processor */
$path_processor = \Drupal::service('path_processor_manager');
$options = [
'prefix' => '',
];
$path_processor
->processOutbound('/', $options);
$pathPrefix = $options['prefix'];
$current_path = \Drupal::routeMatch()
->getRouteName() ? Url::fromRouteMatch(\Drupal::routeMatch())
->getInternalPath() : '';
$current_path_is_admin = \Drupal::service('router.admin_context')
->isAdminRoute();
$path_settings = [
'baseUrl' => $request
->getBaseUrl() . '/',
'pathPrefix' => $pathPrefix,
'currentPath' => $current_path,
'currentPathIsAdmin' => $current_path_is_admin,
'isFront' => \Drupal::service('path.matcher')
->isFrontPage(),
'currentLanguage' => \Drupal::languageManager()
->getCurrentLanguage(LanguageInterface::TYPE_URL)
->getId(),
];
if (!empty($current_query)) {
ksort($current_query);
$path_settings['currentQuery'] = (object) $current_query;
}
// Only set core/drupalSettings values that haven't been set already.
foreach ($path_settings as $key => $value) {
if (!isset($settings['path'][$key])) {
$settings['path'][$key] = $value;
}
}
if (!isset($settings['pluralDelimiter'])) {
$settings['pluralDelimiter'] = LOCALE_PLURAL_DELIMITER;
}
// Add the theme token to ajaxPageState, ensuring the database is available
// before doing so.
/** @var \Drupal\Core\Asset\LibraryDependencyResolver $library_dependency_resolver */
$library_dependency_resolver = \Drupal::service('library.dependency_resolver');
if (isset($settings['ajaxPageState']) || in_array('core/drupal.ajax', $library_dependency_resolver
->getLibrariesWithDependencies($assets
->getAlreadyLoadedLibraries()))) {
if (!defined('MAINTENANCE_MODE')) {
// The theme token is only validated when the theme requested is not the
// default, so don't generate it unless necessary.
// @see \Drupal\Core\Theme\AjaxBasePageNegotiator::determineActiveTheme()
$active_theme_key = \Drupal::theme()
->getActiveTheme()
->getName();
if ($active_theme_key !== \Drupal::service('theme_handler')
->getDefault()) {
$settings['ajaxPageState']['theme_token'] = \Drupal::csrfToken()
->get($active_theme_key);
}
}
}
}