1

How to remove the global library from specific page ?

I want to rearrange java script files.

2 Answers 2

2

You need to implement hook_page_attachments_alter().

4
  • Okay , how to check for that specific page ? Commented Nov 12, 2016 at 6:15
  • Here are some examples drupal.org/node/2274705
    – ya.teck
    Commented Nov 12, 2016 at 7:02
  • Unfortunately this does not seem to work for routes defined in module.routes.yml? Commented Apr 1, 2020 at 10:30
  • It doesn't matter. I just need to know the route name.
    – ya.teck
    Commented Apr 1, 2020 at 16:26
1

Yes, it's very simple to use the Hook_page_attarchment_alter() Please see the following code it may help you.

    function modulename_page_attachments_alter(array &$attachments) {

    // Conditionally remove an asset.
    $current_path = \Drupal::service('path.current')->getPath();
    $result = \Drupal::service('path.alias_manager')->getAliasByPath($current_path);
    if ($result == "/<url slug>" ){
      if (in_array('<theme name>/global-styling', $attachments['#attached']['library'])) {
        $index = array_search('<theme name>/global-styling', $attachments['#attached']['library']);
        unset($attachments['#attached']['library'][$index]);
      }
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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