MODULENAME_page_attachment_alter() Undefined I have a custom module named rsm_library.  I would like to use hook_page_attachment_alter() in rsm_library.module and called the hook as follows:

rsm_library_page_attachment_alter($attachments);


I then issued $ drush cr to clear the caches.

This resulted in:


  AH01071: Got error 'PHP message: Error: Call to undefined function
  rsm_library_page_attachments_alter().
First, there's a typo. It's not attachment, it's attachments.

Next, you can't simply call a function without defining it.

In Drupal hooks are used to modify existing code when it's being processed.

See hook_page_attachments_alter for an example of what you are supposed to do.

Using hooks, you simply define a new function prefixed with your module's machine name, put your code/alterations in there, and in the end it's automatically being hooked up for execution. No need to call it again.


function hook_page_attachments_alter(array &amp;$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]);
  }
}