function SystemHooks::jsSettingsBuild

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.

Attributes

#[Hook('js_settings_build')]

File

core/modules/system/src/Hook/SystemHooks.php, line 204

Class

SystemHooks
Hook implementations for system.

Namespace

Drupal\system\Hook

Code

public function jsSettingsBuild(&$settings, AttachedAssetsInterface $assets) : void {
  // 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;
  }
}

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