I found this example: http://deeson-online.co.uk/labs/how-use-formattached-javascript-setting-drupal-7-form-api

I tried:

   $form['#attached']['js'][] = array(
            'data' => array(
                'my_module' => array(
                    'xx_code' => 'dsadsad',
                    'xx_key' => 'sadasd',
            'type' => 'setting',

but with no luck, Drupal.settings.my_module is undefined

1 Answer 1


Documentation is available:

Form API

$settings = array('id' => 'mymodule-element-1');

$form['#attached']['js'][] = array(
  'data' => array('mymodule' => $settings),
  'type' => 'setting',

Managing JavaScript in Drupal 7

$element['#attached']['js'][] = array(
  'type' => 'setting',
  'data' => array('myModule' => array('key' => 'value')),

Both are roughly the same as what OP (@drupality) said worked for him in a comment on Jun 14 2013.

The code in the original question that the OP said he tried but fails seems roughly the same as the working code, so I can't see what the original error was.

  • Cheers @Adam, good that this is cleaned up
    – Clive
    Commented Oct 7, 2016 at 9:36

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.