Score:0

How to remove addtoany js for a particular node page?

nz flag

I need to unset addtoany module js for a particular node.

For that I have implemented

 function theme_name_js_alter(&$javascript) {
    if(\Drupal::service('path.matcher')->isFrontPage()) {
            unset($javascript['https://static.addtoany.com/menu/page.js']);
            unset($javascript['modules/contrib/addtoany/js/addtoany.js']);
            unset($javascript['themes/test/js/index.js']);
            unset($javascript['themes/test/js/custom.js']);
        }
    }

Above code unset index.js & custom.js but not works for addtoany.js and page.js.

Thanks for your suggestion.

Kevin avatar
in flag
Double check the asset URLs?
misterdidi avatar
de flag
addtoany library is added using hook_page_attachments(), did you ensure your hook_js_alter() is called after that one? Perhaps you could give a try to hook_page_attachments_alter() or try removing the addtoany/addtoany library instead of the files.
Tushar avatar
nz flag
Thanks @misterdidi. Your comment help to solve my issue.
Score:1
nz flag

Implements hook_page_attachments_alter() to resolve it.

function theme_name_page_attachments_alter(array &$attachments) {
    foreach ($attachments['#attached']['library'] as $key => $library) {
        if ($library == 'addtoany/addtoany') {
          unset($attachments['#attached']['library'][$key]);
        }
    }
}
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.