Custom module javascript Drupal.behaviours doesn't seems to loadjquery is not adding a classHow do I add a Javascript library to a field formatter?Javascript “once is not a function” errors after library includeJavascript will not load for anonymous usershello_world_page_attachments is not calledHow to add variables in *.libraries.yml in drupal 8What's the correct way to pass variables to the JavaScript of a custom CKEditor plugin?

What do I get by paying more for a bicycle?

Employer says they want Quality & Quantity, but only pays bonuses based on the latter

Why are Buddhist concepts so difficult?

Had J. K. Rowling seen This Is Spinal Tap before writing Harry Potter and the Philosopher's Stone?

529 accounts for multiple kids

Find the closest enemy

Why is Microwaved mac & cheese burnt where they touch?

Wood glue versus epoxy for doweling stripped screw holes

Which are stars and which are noise in this comet photo?

Motivation for an inequality

Are commoners actually this squishy?

Best way to drill square tubing (Without drill press)

Is there a name for the phenomenon of false positives counterintuitively outstripping true positives

How can a signal be both periodic and random?

Confusion in PID loop?

What could an alternative fuel free transportation method look like?

Given small computational resources how navigation was implemented ( Not Samples of old guidance software)

How to delete music as it's being played

Length-terminated sequences

Articles at the beginning of sentences in scientific writing

Are these 2 resistors in parallel?

What's the short and accented note at the very end of a song called?

Why do the Romance languages use definite articles, when Latin doesn't?

Mutate my DNA sequence



Custom module javascript Drupal.behaviours doesn't seems to load


jquery is not adding a classHow do I add a Javascript library to a field formatter?Javascript “once is not a function” errors after library includeJavascript will not load for anonymous usershello_world_page_attachments is not calledHow to add variables in *.libraries.yml in drupal 8What's the correct way to pass variables to the JavaScript of a custom CKEditor plugin?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









1

















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';










share|improve this question























  • 1





    While you created all the files, you still need to load/call/attach the library somewhere. That somewhere depends on where you want this to load at. Every page? a specific page? every form? specific form? etc...

    – No Sssweat
    May 29 at 5:23












  • That's correct @NoSssweat It's suppose to load on a page where I assigned my block to it. I can see the block on that page, but the javascript file doesn't seems to load. I'm wondering if there's anything to configure apart from the above.

    – fkaufusi
    May 29 at 5:38







  • 1





    @fkaufusi did you attach library 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.

    – Niklan
    May 29 at 6:03











  • @Niklan thanks for mentioning that "attach" I just added that attached now. Edited the question to add more info. Do you think the hook_preprocess_block() is the correct place to add the "attach"?

    – fkaufusi
    May 29 at 6:46











  • First of all. What "block" do you use? It's Plugin Block or you created it via admin interface? Those block types are different and have different approaches to attach library, your example is for block template (affects all block on the site, which is not good). Second, you attach the library mymodule/overlayblock. The library naming is [module_name]/[library_name]. In your mymodule.libraries.yml you named library as mymodule, so you must attach mymodule/mymodule library.

    – Niklan
    May 29 at 7:10

















1

















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';










share|improve this question























  • 1





    While you created all the files, you still need to load/call/attach the library somewhere. That somewhere depends on where you want this to load at. Every page? a specific page? every form? specific form? etc...

    – No Sssweat
    May 29 at 5:23












  • That's correct @NoSssweat It's suppose to load on a page where I assigned my block to it. I can see the block on that page, but the javascript file doesn't seems to load. I'm wondering if there's anything to configure apart from the above.

    – fkaufusi
    May 29 at 5:38







  • 1





    @fkaufusi did you attach library 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.

    – Niklan
    May 29 at 6:03











  • @Niklan thanks for mentioning that "attach" I just added that attached now. Edited the question to add more info. Do you think the hook_preprocess_block() is the correct place to add the "attach"?

    – fkaufusi
    May 29 at 6:46











  • First of all. What "block" do you use? It's Plugin Block or you created it via admin interface? Those block types are different and have different approaches to attach library, your example is for block template (affects all block on the site, which is not good). Second, you attach the library mymodule/overlayblock. The library naming is [module_name]/[library_name]. In your mymodule.libraries.yml you named library as mymodule, so you must attach mymodule/mymodule library.

    – Niklan
    May 29 at 7:10













1












1








1


1






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';










share|improve this question

















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';







8 theming javascript






share|improve this question
















share|improve this question













share|improve this question




share|improve this question








edited May 29 at 7:08







fkaufusi

















asked May 29 at 5:15









fkaufusifkaufusi

2011 silver badge11 bronze badges




2011 silver badge11 bronze badges










  • 1





    While you created all the files, you still need to load/call/attach the library somewhere. That somewhere depends on where you want this to load at. Every page? a specific page? every form? specific form? etc...

    – No Sssweat
    May 29 at 5:23












  • That's correct @NoSssweat It's suppose to load on a page where I assigned my block to it. I can see the block on that page, but the javascript file doesn't seems to load. I'm wondering if there's anything to configure apart from the above.

    – fkaufusi
    May 29 at 5:38







  • 1





    @fkaufusi did you attach library 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.

    – Niklan
    May 29 at 6:03











  • @Niklan thanks for mentioning that "attach" I just added that attached now. Edited the question to add more info. Do you think the hook_preprocess_block() is the correct place to add the "attach"?

    – fkaufusi
    May 29 at 6:46











  • First of all. What "block" do you use? It's Plugin Block or you created it via admin interface? Those block types are different and have different approaches to attach library, your example is for block template (affects all block on the site, which is not good). Second, you attach the library mymodule/overlayblock. The library naming is [module_name]/[library_name]. In your mymodule.libraries.yml you named library as mymodule, so you must attach mymodule/mymodule library.

    – Niklan
    May 29 at 7:10












  • 1





    While you created all the files, you still need to load/call/attach the library somewhere. That somewhere depends on where you want this to load at. Every page? a specific page? every form? specific form? etc...

    – No Sssweat
    May 29 at 5:23












  • That's correct @NoSssweat It's suppose to load on a page where I assigned my block to it. I can see the block on that page, but the javascript file doesn't seems to load. I'm wondering if there's anything to configure apart from the above.

    – fkaufusi
    May 29 at 5:38







  • 1





    @fkaufusi did you attach library 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.

    – Niklan
    May 29 at 6:03











  • @Niklan thanks for mentioning that "attach" I just added that attached now. Edited the question to add more info. Do you think the hook_preprocess_block() is the correct place to add the "attach"?

    – fkaufusi
    May 29 at 6:46











  • First of all. What "block" do you use? It's Plugin Block or you created it via admin interface? Those block types are different and have different approaches to attach library, your example is for block template (affects all block on the site, which is not good). Second, you attach the library mymodule/overlayblock. The library naming is [module_name]/[library_name]. In your mymodule.libraries.yml you named library as mymodule, so you must attach mymodule/mymodule library.

    – Niklan
    May 29 at 7:10







1




1





While you created all the files, you still need to load/call/attach the library somewhere. That somewhere depends on where you want this to load at. Every page? a specific page? every form? specific form? etc...

– No Sssweat
May 29 at 5:23






While you created all the files, you still need to load/call/attach the library somewhere. That somewhere depends on where you want this to load at. Every page? a specific page? every form? specific form? etc...

– No Sssweat
May 29 at 5:23














That's correct @NoSssweat It's suppose to load on a page where I assigned my block to it. I can see the block on that page, but the javascript file doesn't seems to load. I'm wondering if there's anything to configure apart from the above.

– fkaufusi
May 29 at 5:38






That's correct @NoSssweat It's suppose to load on a page where I assigned my block to it. I can see the block on that page, but the javascript file doesn't seems to load. I'm wondering if there's anything to configure apart from the above.

– fkaufusi
May 29 at 5:38





1




1





@fkaufusi did you attach library 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.

– Niklan
May 29 at 6:03





@fkaufusi did you attach library 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.

– Niklan
May 29 at 6:03













@Niklan thanks for mentioning that "attach" I just added that attached now. Edited the question to add more info. Do you think the hook_preprocess_block() is the correct place to add the "attach"?

– fkaufusi
May 29 at 6:46





@Niklan thanks for mentioning that "attach" I just added that attached now. Edited the question to add more info. Do you think the hook_preprocess_block() is the correct place to add the "attach"?

– fkaufusi
May 29 at 6:46













First of all. What "block" do you use? It's Plugin Block or you created it via admin interface? Those block types are different and have different approaches to attach library, your example is for block template (affects all block on the site, which is not good). Second, you attach the library mymodule/overlayblock. The library naming is [module_name]/[library_name]. In your mymodule.libraries.yml you named library as mymodule, so you must attach mymodule/mymodule library.

– Niklan
May 29 at 7:10





First of all. What "block" do you use? It's Plugin Block or you created it via admin interface? Those block types are different and have different approaches to attach library, your example is for block template (affects all block on the site, which is not good). Second, you attach the library mymodule/overlayblock. The library naming is [module_name]/[library_name]. In your mymodule.libraries.yml you named library as mymodule, so you must attach mymodule/mymodule library.

– Niklan
May 29 at 7:10










2 Answers
2






active

oldest

votes


















3


















When attached make sure to use the correct name of the "library"



Inside your mymodule.libraries.yml file. The first line is your library name.



I use a different name inside my library file, and I attached a wrong library name.



 function mymodule_preprocess_block(&variables)
$variables['#attached']['library'][] =
'mymodule/mylibraryname';



My library file mymodule.libraries.yml



mylibraryname:
js:
js/promotional-overlay-block.js:
dependencies:
- core/jquery
- core/jquery.once
- core/drupalSettings





share|improve this answer

































    2


















    In Case if your library called mymodule, then attached shoud be as i described below



    function mytheme_preprocess_block(&$variables) 
    if ($variables['elements']['#id'] == 'myblock')
    $variables['#attached']['library'][] = 'module_name/mymodule';







    share|improve this answer



























      Your Answer








      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "220"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );














      draft saved

      draft discarded
















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdrupal.stackexchange.com%2fquestions%2f281712%2fcustom-module-javascript-drupal-behaviours-doesnt-seems-to-load%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown


























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3


















      When attached make sure to use the correct name of the "library"



      Inside your mymodule.libraries.yml file. The first line is your library name.



      I use a different name inside my library file, and I attached a wrong library name.



       function mymodule_preprocess_block(&variables)
      $variables['#attached']['library'][] =
      'mymodule/mylibraryname';



      My library file mymodule.libraries.yml



      mylibraryname:
      js:
      js/promotional-overlay-block.js:
      dependencies:
      - core/jquery
      - core/jquery.once
      - core/drupalSettings





      share|improve this answer






























        3


















        When attached make sure to use the correct name of the "library"



        Inside your mymodule.libraries.yml file. The first line is your library name.



        I use a different name inside my library file, and I attached a wrong library name.



         function mymodule_preprocess_block(&variables)
        $variables['#attached']['library'][] =
        'mymodule/mylibraryname';



        My library file mymodule.libraries.yml



        mylibraryname:
        js:
        js/promotional-overlay-block.js:
        dependencies:
        - core/jquery
        - core/jquery.once
        - core/drupalSettings





        share|improve this answer




























          3














          3










          3









          When attached make sure to use the correct name of the "library"



          Inside your mymodule.libraries.yml file. The first line is your library name.



          I use a different name inside my library file, and I attached a wrong library name.



           function mymodule_preprocess_block(&variables)
          $variables['#attached']['library'][] =
          'mymodule/mylibraryname';



          My library file mymodule.libraries.yml



          mylibraryname:
          js:
          js/promotional-overlay-block.js:
          dependencies:
          - core/jquery
          - core/jquery.once
          - core/drupalSettings





          share|improve this answer














          When attached make sure to use the correct name of the "library"



          Inside your mymodule.libraries.yml file. The first line is your library name.



          I use a different name inside my library file, and I attached a wrong library name.



           function mymodule_preprocess_block(&variables)
          $variables['#attached']['library'][] =
          'mymodule/mylibraryname';



          My library file mymodule.libraries.yml



          mylibraryname:
          js:
          js/promotional-overlay-block.js:
          dependencies:
          - core/jquery
          - core/jquery.once
          - core/drupalSettings






          share|improve this answer













          share|improve this answer




          share|improve this answer










          answered May 29 at 7:25









          fkaufusifkaufusi

          2011 silver badge11 bronze badges




          2011 silver badge11 bronze badges


























              2


















              In Case if your library called mymodule, then attached shoud be as i described below



              function mytheme_preprocess_block(&$variables) 
              if ($variables['elements']['#id'] == 'myblock')
              $variables['#attached']['library'][] = 'module_name/mymodule';







              share|improve this answer






























                2


















                In Case if your library called mymodule, then attached shoud be as i described below



                function mytheme_preprocess_block(&$variables) 
                if ($variables['elements']['#id'] == 'myblock')
                $variables['#attached']['library'][] = 'module_name/mymodule';







                share|improve this answer




























                  2














                  2










                  2









                  In Case if your library called mymodule, then attached shoud be as i described below



                  function mytheme_preprocess_block(&$variables) 
                  if ($variables['elements']['#id'] == 'myblock')
                  $variables['#attached']['library'][] = 'module_name/mymodule';







                  share|improve this answer














                  In Case if your library called mymodule, then attached shoud be as i described below



                  function mytheme_preprocess_block(&$variables) 
                  if ($variables['elements']['#id'] == 'myblock')
                  $variables['#attached']['library'][] = 'module_name/mymodule';








                  share|improve this answer













                  share|improve this answer




                  share|improve this answer










                  answered May 29 at 6:49









                  Igor TaldenkoIgor Taldenko

                  211 bronze badge




                  211 bronze badge































                      draft saved

                      draft discarded















































                      Thanks for contributing an answer to Drupal Answers!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdrupal.stackexchange.com%2fquestions%2f281712%2fcustom-module-javascript-drupal-behaviours-doesnt-seems-to-load%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown









                      Popular posts from this blog

                      Tamil (spriik) Luke uk diar | Nawigatjuun

                      Align equal signs while including text over equalitiesAMS align: left aligned text/math plus multicolumn alignmentMultiple alignmentsAligning equations in multiple placesNumbering and aligning an equation with multiple columnsHow to align one equation with another multline equationUsing \ in environments inside the begintabularxNumber equations and preserving alignment of equal signsHow can I align equations to the left and to the right?Double equation alignment problem within align enviromentAligned within align: Why are they right-aligned?

                      Training a classifier when some of the features are unknownWhy does Gradient Boosting regression predict negative values when there are no negative y-values in my training set?How to improve an existing (trained) classifier?What is effect when I set up some self defined predisctor variables?Why Matlab neural network classification returns decimal values on prediction dataset?Fitting and transforming text data in training, testing, and validation setsHow to quantify the performance of the classifier (multi-class SVM) using the test data?How do I control for some patients providing multiple samples in my training data?Training and Test setTraining a convolutional neural network for image denoising in MatlabShouldn't an autoencoder with #(neurons in hidden layer) = #(neurons in input layer) be “perfect”?