Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

Javascript works fine, but i need to get my_var variable from module. At the moment its not working.

Clear my page cache and browser cache several times.

The variable 'my_var' is showing value on front end but not on backend/admin side.

Front end theme is: Bartik
Backend/Admin theme: Seven I don't know if this help.

All other variables are available on Admin side from Drupal.settings, except the one I pass from my custom module.

This works well on both themes

alert(Drupal.settings.basePath);

Here is my code:

MY_MODULE.module

/**
 * Implements hook_page_build().
 */
function MY_MODULE_page_build(&$page) {
    ctools_include('modal');
    ctools_modal_add_js();

    // HERE it is calling the js
    drupal_add_js(array('MY_MODULE' => array('my_var' => 'MY_VALUE'),), 'setting');
    drupal_add_js(drupal_get_path('module', 'MY_MODULE') . '/js/MY_MODULE.js');
}

MY_MODULE.js

(function ($) {
    Drupal.behaviors.MY_MODULE = {
    attach: function(context) {
        alert("test");
        alert(Drupal.settings.MY_MODULE.my_var);
    }
  }
})(jQuery);

On front end it alerts test followed by MY_VALUE
BUT On admin side it show test in alert then Firebug shows me:

Uncaught TypeError: Cannot read property 'my_var' of undefined 

Any help would be appreciated.

share|improve this question
1  
where are you written drupal_add_js function ? –  Rupesh Apr 26 '14 at 14:53
    
@Rupesh In custom module called MY_MODULE.module –  Zafar S Apr 26 '14 at 14:59
1  
which function or any hook ? –  Rupesh Apr 26 '14 at 14:59
    
in hook_page_build() –  Zafar S Apr 26 '14 at 15:00
    
There are nothing wrong in this code. Check may be some typo error. –  Rupesh Apr 26 '14 at 15:03

3 Answers 3

calling drupal_add_js in hook_init is a fairly standard response to this kind of question, as is hook_boot.

hook_init is used for setting up things required by the page - but doesnt run on cached pages.

hook boot is used for code that has to run on cached pages as well as uncached pages.

The way I usually debug this kind of thing is with the javascript console. In chrome, open the console and type

Drupal.settings

hit enter and it should print out the whole settings object for you to inspect. This will let you see what you have.

I suspect the problem may be down to when hook_page_build gets fired on admin pages if Im reading your question correctly.

Uncaught TypeError: Cannot read property 'my_var' of undefined 

means that

Drupal.settings.MY_MODULE

evaluates to undefined. It could be at any level. so try the javascript console trick and see what you find (is Drupal there.. is drupal.settings there).

you could also try

(function ($) {
    Drupal.behaviors.MY_MODULE = {
    attach: function(context,settings) {
    alert("test");
    alert(settings.MY_MODULE.my_var);
   }   
  }
})(jQuery);

which is more keeping with how it is documented for behaviours on drupal.org

share|improve this answer

your problem is here

    drupal_add_js(array('MY_MODULE' => array('my_var' => 'MY_VALUE'),), 'setting');

you should wrote

drupal_add_js(array('MY_MODULE' => array('my_var' => 'MY_VALUE')), array('type','setting'));
share|improve this answer

You must call drupal_add_js in hook_init

share|improve this answer
    
Why must call drupal_add_js in hook_init? –  Rupesh Apr 26 '14 at 15:11
    
Same result with HOOK_INIT() too. BTW the .js file is calling in both cases but Drupal.settings.MY_MODULE.VAR_NAME is not showing the value. –  Zafar S Apr 26 '14 at 15:14
    
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post. –  4life Apr 26 '14 at 15:42
    
As you are sure that your included javascript loaded may be you could look at console.log(Drupal.settings.MY_MODULE) ? –  gfarishyan Apr 27 '14 at 8:16
    
@gfarishyan it returns: undefined –  Zafar S Apr 27 '14 at 11:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.