I have a custom module which has a javascript file. Here's the content of the javascript file:
(function ($, Drupal) {
Drupal.behaviors.promotionalOverlayBlock = {
attach: function(context, settings) {
console.log("hello world")
}
};
})(jQuery, Drupal);
The location of javascript file:
mymodule/js/overlayblock.js
In my mymodule.libraries.yml
file it has the following:
mymodule:
js:
js/overlayblock.js: {}
dependencies:
- core/jquery
- core/jquery.once
- core/drupalSettings
When I load my drupal page, I expecting to see the "hello world" in the browser console. However, nothing show up. I view the page html code and search for my javascript there, but I can't seems to find anything related to my javascript file.
What could be the problem here?
Here's how I attached my library through the hook_preprocess_block() I'm not sure if this is the right place to add the attached.
function mymodule_preprocess_block(&variables){
$variables['#attached']['library'][] =
'mymodule/overlayblock';
}
mymodule/mymodule
to that block? Can you show your code how you did that? If you don't, what type of block do you use, Plugin Block or Entity Block? The approaches for them will be different.mymodule/overlayblock
. The library naming is[module_name]/[library_name]
. In your mymodule.libraries.yml you named library asmymodule
, so you must attachmymodule/mymodule
library.