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:


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

closed as off-topic by Kevin, leymannx, Pierre.Vriens, Clive Nov 14 '18 at 20:13

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question was caused by a problem that can no longer be reproduced, was solved by a cache clear, or was a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers." – leymannx, Clive
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    Also its not clear what you are doing - you cannot call the function without defining it, it will be automatically invoked for you when alter hooks are called from the main call. The function definition is all you need. – Kevin Nov 14 '18 at 16:55

do it like this :

 * Implements hook_page_attachments_alter().
function rsm_library_page_attachments_alter(array &$attachments) {
  // do stuff
  • Yes, adding this code to rsm_library.module gave me a solution. My hook is invoked and I can work with the $attachments array. Thanks! – Greg Sims Nov 14 '18 at 19:07

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 &$attachments) {

  // Conditionally remove an asset.
  if (in_array('core/jquery', $attachments['#attached']['library'])) {
    $index = array_search('core/jquery', $attachments['#attached']['library']);
  • If you look at the 8.2 version of hook_page_attachments_alter (which is the highest ranked in Google), there is not an example of how to use the hook. The 8.5 version has an example for the theme layer -- which would have likely been enough to understand how to use the hook. Too bad. – Greg Sims Nov 14 '18 at 19:13
  • @GregSims – I also blockquoted the example from that page. What you mean is a comment. – leymannx Nov 14 '18 at 19:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.