Blogs

Include JS or CSS Library in Drupal 8

1. Create a file module_name.libraries.yml and add the below code

Plugin_name: 
  version: 1.x
  js:
    js/JS_FILE_NAME: {}

2. Implement the below hook into your module_name.module file

function module_name_page_attachments_alter(array &$attachments) {
  $attachments['#attached']['library'][] = 'module_name/plugin_name';
}

3. Place your js file into module folder inside the js folder.

Pages