Looking for some guidance on how to create a custom module, mymodule
, that will pull all the libraries from the custom module and display it in an admin screen. Once the libraries are displayed, I want to be able to set Add to Pages
(checkbox) for each library to determine if it should be added to the nodes. It would be ideal if the setting became a configuration.
The goal is to be able to add any libraries I need to the custom module, but selectively add them to nodes using hook_page_attachments. How would I do this?
I initially tried this, but couldn't find a way to get all the libraries.
mymodule.libraries.yml
library1:
version: 1.x
header: true
js:
path/to/library/file1.js
library2:
version: 1.x
js:
/path/to/library/file2.js
mymodule.module
/**
* Implements hook_page_attachments().
*/
function mymodule_page_attachments(array &$attachments) {
$libraries = [
'mymodule/library1',
'mymodule/library2',
];
foreach($libraries as $library) {
$attachments['#attached']['library'][] = $library;
}
}
hook_page_attachments()
which will affect all pages, you could usehook_ENTITY_TYPE_view()
.