0

I've found about a dozen other people who have experienced this problem, but none of those fixes have helped me.

I have written a module that simply loads a Javascript file into the page. This part of it works fine. However, I cannot access any variables from PHP because the console keeps telling me that Drupal.settings.mymodule is undefined. The top of my js file looks like this:

(function($) {
  Drupal.behaviors.mymodule = {
    attach: function (context, settings) {

      // $(document).ready(blahblahblah...

    }
  }
})(jQuery);

I've also tried ending it with }(jQuery)); as I've seen it suggested both ways.

I have my drupal_add_js function running from hook_form (I've also tried hook_menu) in my .module file, but that doesn't seem to be the problem. Here is where it currently is:

function mymodule_form($form, &$form_state) {
  $form['mymodule_safe_path'] = array(
    '#type' => 'textfield',
    '#title' => t('Safe path'),
    '#default_value' => variable_get('mymodule_safe_path', 'mymodule'),
    '#description' => t('...'),
    '#required' => TRUE,
  );  

  $form['mymodule_status'] = array(
    '#type' => 'checkbox',
    '#title' => t('Turn on mymoduleification'),
    '#default_value' => variable_get('mymodule_status', 0),
    '#description' => t('...'),
    '#required' => FALSE,
  );

  // Note: I realized mymodule_status is not properly assigning the variable. I was keeping it as an easy-to-read string for debugging purposes.
  drupal_add_js(array('mymodule' => array('mymodule_status' => 'mymodule_status')), array('type' => 'setting'));    

  return system_settings_form($form);
}

For some reason it seems that mymodule isn't making its way into Drupal.settings at all. I say this because without even referencing a specific setting, just trying to log Drupal.settings.mymodule to the console results in "Undefined."

Any thoughts as to why this might be happening?

  • This javascript looks correct. Can you post an example of where you actually run drupal_add_js? – alex laughnan Mar 20 '15 at 16:57
  • Sure thing. Updated. – rrr45 Mar 20 '15 at 17:07
  • Finally got this working. It seems I needed to disable and reenable the module. – rrr45 Mar 20 '15 at 18:38
  • Glad you solved it! It could also be that the drupal cache needed to be cleared. That's often the case. – alex laughnan Mar 20 '15 at 18:53
  • It wasn't the cache, I just tested by clearing all caches drupal caches and it didn't work. But disabling and enabling the module did. I would like to know why that makes it work, wondering if that makes the existing cookie expire at the server side. – Eduardo Chongkan Aug 26 '16 at 18:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.