3

Consider

drupal_add_js(array(
  'radio86_media' => array('barfoo' => 'booboo')), 'setting'
);    

and

$form['#attached']['js'] = array(
  drupal_get_path('module', 'radio86_media') . '/select_media.js', 
);    

These two 'lines' work well separately, but I think they could (and should) be combined so that the variable setting is set on the second line, combining the logic.

I've been fumbling around it and just can't get it to work. With this current way, I can console.log the barfoo variable via JS by using Drupal.settings.radio86_media.barfoo so it does work, but I really would like to have it combined.

Any suggestions greatly appreciated!

1 Answer 1

5

I never tried, but I guess you can do it like that :

$form['#attached']['js'] = array(
  array(
    'data' => drupal_get_path('module', 'radio86_media') . '/select_media.js',
    'type' => 'file'
  ),
  array(
    'data' => array('radio86_media' => array('barfoo' => 'booboo')),
    'type' => 'setting'
  )
);

According the documentation you should be able to add settings and files.

3
  • The select_media.js gets loaded just fine, but the variable is undefined. I tried swapping the arrays places so that the variable is created before the .js is included, but it did not help either.
    – red
    Sep 21, 2011 at 9:55
  • I changed something in my code (just added 'type' => 'setting' instead of 'setting'. Can you try again ?
    – yvan
    Sep 21, 2011 at 10:02
  • Worth a note that settings must have numeric keys. Weirdly. i.e. $form['#attached']['js']['foo'] = ['data' => ['mysetting' => 1], 'type' => 'setting']; does not work but $form['#attached']['js'][] = ['data' => ['mysetting' => 1], 'type' => 'setting']; does! Feb 8, 2019 at 12:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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