You are here

function system_js_settings_build in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/system.module \system_js_settings_build()

Implements hook_js_settings_build().

Sets values for the core/drupal.ajax library, which just depends on the active theme but no other request-dependent values.

File

core/modules/system/system.module, line 613
Configuration system that lets administrators modify the workings of the site.

Code

function system_js_settings_build(&$settings, AttachedAssetsInterface $assets) {

  // Generate the values for the core/drupal.ajax library.
  // We need to send ajaxPageState settings for core/drupal.ajax if:
  // - ajaxPageState is being loaded in this Response, in which case it will
  //   already exist at $settings['ajaxPageState'] (because the core/drupal.ajax
  //   library definition specifies a placeholder 'ajaxPageState' setting).
  // - core/drupal.ajax already has been loaded and hence this is an AJAX
  //   Response in which we must send the list of extra asset libraries that are
  //   being added in this AJAX Response.

  /** @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()))) {

    // Provide the page with information about the theme that's used, so that
    // a later AJAX request can be rendered using the same theme.
    // @see \Drupal\Core\Theme\AjaxBasePageNegotiator
    $theme_key = \Drupal::theme()
      ->getActiveTheme()
      ->getName();
    $settings['ajaxPageState']['theme'] = $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);
  }
}