5

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

4

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 Oct 7 '16 at 9:36

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.