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 at 14:53
    
@Rupesh In custom module called MY_MODULE.module –  Zafar S Apr 26 at 14:59
1  
which function or any hook ? –  Rupesh Apr 26 at 14:59
    
in hook_page_build() –  Zafar S Apr 26 at 15:00
    
There are nothing wrong in this code. Check may be some typo error. –  Rupesh Apr 26 at 15:03

1 Answer 1

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 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 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 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 at 8:16
    
@gfarishyan it returns: undefined –  Zafar S Apr 27 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.