1

Module-noob here. I've seen plenty of threads on this topic in general but haven't found any concerning use of "attach." I'm working on a fairly simple module that applies some JavaScript to every page on the site. It was working fine, but pareview told me not to use drupal_add_js() in hook_page_build(), instructing me to use attach instead. I can't figure out how to get my PHP variables over to my JS using this method.

The JS file itself is loading, but it is not receiving the data from the PHP variables. From what I have read, I understand that the following is the way to accomplish what I am trying to do, but obviously I've got something wrong somewhere. Here is my code:

mymodule.module:

function mymodule_page_build() {

  $page['#attached']['js'] = array(
    array(
      'data' => drupal_get_path('module', 'mymodule') . '/mymodule.js',
      'options' => array(
        'preprocess' => TRUE,
        'every_page' => TRUE,
      ),
    ),
    array(
      'data' => array(
        'mymodule_status' => variable_get('mymodule_status', 0),
      ),' => 'setting',
    ),
  );
}

mymodule.js:

(function ($) {
  "use strict";
  Drupal.behaviors.myModule = {
    attach: function (context, settings) {
      var moduleStatus = Drupal.settings.mymodule.mymodule_status;
      var milk = 'milk';
      $(document).ready(function () {
        // Debug:
        console.log('first');      // works
        console.log(milk);         // works
        console.log(moduleStatus); // Says mymodule is undefined
    }
  };
})(jQuery);

Thank you for any help you can offer.

1

Have a look on this page

For getting the value of a variable that has been set in the module you have to use like this Drupal.settings.<modulename>.<variablename>

In your module file

function mymodule_page_build() {
  $status = variable_get('mymodule_status', 0);
  drupal_add_js(array('MODULENAME' => array('mymodule_status' => $status)), 'setting'); 

  $page['#attached']['js'] = array(
    array(
      'data' => drupal_get_path('module', 'mymodule') . '/mymodule.js',
      'options' => array(
        'preprocess' => TRUE,
        'every_page' => TRUE,
      ),
    ),
  );
}

So in your case it should be

Drupal.settings.mymodule.mymodule_status

  • Hmm, it's telling me that Drupal.settings.mymodule is undefined. Any idea why that might be? – rrr45 Apr 13 '16 at 14:19
  • have you tried Drupal.settings.mymodule.<yourvariablename> this? not to mention please clear cache once. – NTT Apr 13 '16 at 14:20
  • Yes I tried Drupal.settings.mymodule.myvariable, as well as Drupal.settings.mymodule['myvariable'] just to be safe. Each time it said it couldn't get property 'myvariable' of undefined. So then I tested it out in the console with just Drupal.settings.mymodule, and it's just returning undefined. I also checked out the page you linked, but it doesn't seem to have instructions for using the attach method. Edit: Also, yes I've cleared the cache each time. – rrr45 Apr 13 '16 at 14:23
  • Then I doubt on your module, variable is not assigning to the js – NTT Apr 13 '16 at 14:30
  • It looks like it isn't recognizing Drupal.settings.mymodule at all. Am I maybe not attaching it correctly? – rrr45 Apr 13 '16 at 14:39
1

It turns out there were two problems.

First, I needed to be using function mymodule_page_build(&$page). I was originally leaving out the &$page.

Second, it turns out that to use #attached on the page itself, you have to access the 'content' property of the page.

So, instead of:

$page['#attached']['js']

It should be:

$page['content']['#attached']['js']

Your Answer

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

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