Score:-3

can I set multiple JavaScript files on a custom module library?

co flag

I created a new module and I need to add multiple JavaScript files to my pageI.

Should I create a new library for each file, or is it possible to add multiple JavaScript files on the same library as in the following library definition?

organigram_admin_library:
  version: 1.0
  js:
    js/admin/jquery-1.10.2.min.js: {}
    js/admin/admin.js: {}
Score:0
jm flag

Yes, you can add multiple JS and CSS files to the same library. Here is an example you can follow.

custom-jbox-modal:
  version: 1.x
  css:
    theme:
      '//cdn.jsdelivr.net/gh/StephanWagner/[email protected]/dist/jBox.all.min.css': {type: external, minified: true}
  js:
    '//cdn.jsdelivr.net/gh/StephanWagner/[email protected]/dist/jBox.all.min.js': {type: external, minified: true}
    js/jbox-custom-modal.js: {}
Score:0
us flag

Drupal doesn't put any limit on the number of JavaScript files a library can contain. This is how the drupal.collapse library is defined in core.libraries.yml.

drupal.collapse:
  version: VERSION
  js:
    misc/details-aria.js: {}
    misc/collapse.js: {}
  dependencies:
    - core/jquery
    - core/modernizr
    - core/drupal
    - core/drupal.form
    - core/jquery.once

It's not a common case in Drupal core, as most of the libraries contain a single JavaScript file, and adds the other files as dependencies, but that doesn't mean a library cannot contain more than a JavaScript file.

In the case of using a jQuery file different from the one used from Drupal core, I would be careful, and first verify it doesn't create issues.

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.