function hook_js_alter

Same name and namespace in other branches
  1. 7.x modules/system/system.api.php \hook_js_alter()
  2. 9 core/lib/Drupal/Core/Render/theme.api.php \hook_js_alter()
  3. 8.9.x core/lib/Drupal/Core/Render/theme.api.php \hook_js_alter()
  4. 11.x core/lib/Drupal/Core/Render/theme.api.php \hook_js_alter()

Alters JavaScript before it is presented on the page.

Parameters

$javascript: An array of all JavaScript being presented on the page.

\Drupal\Core\Asset\AttachedAssetsInterface $assets: The assets attached to the current response.

\Drupal\Core\Language\LanguageInterface $language: The language for the page request that the assets will be rendered for.

See also

\Drupal\Core\Asset\AssetResolver

Related topics

6 functions implement hook_js_alter()

Note: the procedural functions in this list are found by pattern matching, so the list may include some functions that are not actually implementations of this hook.

Ckeditor5Hooks::jsAlter in core/modules/ckeditor5/src/Hook/Ckeditor5Hooks.php
Implements hook_js_alter().
ckeditor5_js_alter in core/modules/ckeditor5/ckeditor5.module
Implements hook_js_alter().
CommonTestThemeHooks::jsAlter in core/modules/system/tests/modules/common_test/src/Hook/CommonTestThemeHooks.php
Implements hook_js_alter().
common_test_js_alter in core/modules/system/tests/modules/common_test/common_test.module
Implements hook_js_alter().
LocaleHooks::jsAlter in core/modules/locale/src/Hook/LocaleHooks.php
Implements hook_js_alter().

... See full list

1 invocation of hook_js_alter()
AssetResolver::getJsAssets in core/lib/Drupal/Core/Asset/AssetResolver.php
Returns the JavaScript assets for the current response's libraries.

File

core/lib/Drupal/Core/Render/theme.api.php, line 888

Code

function hook_js_alter(&$javascript, \Drupal\Core\Asset\AttachedAssetsInterface $assets, \Drupal\Core\Language\LanguageInterface $language) {
  // Swap out jQuery to use an updated version of the library.
  $javascript['core/assets/vendor/jquery/jquery.min.js']['data'] = \Drupal::service('extension.list.module')->getPath('jquery_update') . '/jquery.js';
}

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