system_js_settings_alter(&$settings, AttachedAssetsInterface $assets)
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 667
- Configuration system that lets administrators modify the workings of the site.
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | function system_js_settings_alter(& $settings , AttachedAssetsInterface $assets ) { // As this is being output in the final response always use the master // request. $request = \Drupal::requestStack()->getMasterRequest(); $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' ] = LOCALE_PLURAL_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_merge ( $assets ->getLibraries(), $assets ->getAlreadyLoadedLibraries() )); sort( $minimal_libraries ); $settings [ 'ajaxPageState' ][ 'libraries' ] = implode( ',' , $minimal_libraries ); } } |
Please login to continue.