function system_js_settings_alter

Same name in other branches
  1. 9 core/modules/system/system.module \system_js_settings_alter()
  2. 8.9.x 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 713

Code

function system_js_settings_alter(&$settings, AttachedAssetsInterface $assets) {
    // As this is being output in the final response always use the main request.
    $request = \Drupal::requestStack()->getMainRequest();
    $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'];
    $route_match = \Drupal::routeMatch();
    if ($route_match instanceof StackedRouteMatchInterface) {
        $route_match = $route_match->getMasterRouteMatch();
    }
    $current_path = $route_match->getRouteName() ? Url::fromRouteMatch($route_match)->getInternalPath() : '';
    $current_path_is_admin = \Drupal::service('router.admin_context')->isAdminRoute($route_match->getRouteObject());
    $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'] = PoItem::DELIMITER;
    }
    // Add the theme token to ajaxPageState, ensuring the database is available
    // before doing so. Also add the loaded libraries to ajaxPageState.
    
    /** @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);
            }
        }
        // Provide the page with information about the individual asset libraries
        // used, information not otherwise available when aggregation is enabled.
        $minimal_libraries = $library_dependency_resolver->getMinimalRepresentativeSubset(array_unique(array_merge($assets->getLibraries(), $assets->getAlreadyLoadedLibraries())));
        sort($minimal_libraries);
        $settings['ajaxPageState']['libraries'] = implode(',', $minimal_libraries);
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.