W3cubDocs

/Drupal 8

function hook_page_attachments_alter

hook_page_attachments_alter(array &$attachments)

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().

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

Hooks
Define functions that alter the behavior of Drupal core.

File

core/lib/Drupal/Core/Render/theme.api.php, line 1040
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]);
  }
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Render!theme.api.php/function/hook_page_attachments_alter/8.1.x