Same name and namespace in other branches
  1. 8.9.x core/lib/Drupal/Core/Render/theme.api.php \hook_page_attachments_alter()
  2. 9 core/lib/Drupal/Core/Render/theme.api.php \hook_page_attachments_alter()

Alter attachments (typically assets) to a page before it is rendered.

Use this hook when you want to remove or alter attachments on the page, or add attachments to the page that depend on another module's attachments (this hook runs after hook_page_attachments(). This hook can be implemented by both modules and themes.

If you try to add anything but #attached and #cache to the array, an exception is thrown.

Parameters

array &$attachments: Array of all attachments provided by hook_page_attachments() implementations.

See also

hook_page_attachments()

Related topics

1 string reference to 'hook_page_attachments_alter'
PageRenderTest::testHookPageAlter in core/modules/system/tests/src/Kernel/Common/PageRenderTest.php
Tests hook_page_attachments_alter() exceptions.
4 functions implement hook_page_attachments_alter()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

claro_page_attachments_alter in core/themes/claro/claro.theme
Implements hook_page_attachments_alter().
common_test_page_attachments_alter in core/modules/system/tests/modules/common_test/common_test.module
Implements hook_page_attachments_alter().
contextual_test_page_attachments_alter in core/modules/contextual/tests/modules/contextual_test/contextual_test.module
Implements hook_page_attachments_alter().
system_module_test_page_attachments_alter in core/modules/system/tests/modules/system_module_test/system_module_test.module
Implements hook_page_attachments_alter().
1 invocation of hook_page_attachments_alter()
HtmlRenderer::invokePageAttachmentHooks in core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php
Invokes the page attachment hooks.

File

core/lib/Drupal/Core/Render/theme.api.php, line 1121
Hooks and documentation related to the theme and render system.

Code

function hook_page_attachments_alter(array &$attachments) {

  // Conditionally remove an asset.
  if (in_array('core/jquery', $attachments['#attached']['library'])) {
    $index = array_search('core/jquery', $attachments['#attached']['library']);
    unset($attachments['#attached']['library'][$index]);
  }
}