I'm trying to use the new form api attribute #attached to attach some javascript settings. The settings look like this:

$js_settings = array('adminNotification' => array('email' => $current_email));

This is the code I've tried (along with other variations):

$form['notification_email'] = array
(
	'#type' => 'textfield',
	'#title' => t('Email'),
	'#attached' => array
	(
		'js' => array
		(
			$js_settings => 'setting',
			$js_path => $js_options,
		),
	),
);

I've also tried this:

$form['notification_email'] = array
(
	'#type' => 'textfield',
	'#title' => t('Email'),
	'#attached' => array
	(
		'js' => array
		(
			'setting' => $js_settings,
			$js_path => $js_options,
		),
	),
);

However, the setting doesn't seem to be properly attached. I'm suspecting that settings are not supposed to be attached, only files, inline js and external js, but I'm not finding any documentation to indicate this one way or another. Does anybody have any idea if my suspicion is correct?

Comments

derjochenmeyer’s picture

Try:

$form['notification_email'] = array(
    '#type' => 'textfield',
    '#title' => t('Email')
);
$form['notification_email']['#attached']['js'][] = array(
  'data' => array('setting_name' => $js_settings),
  'type' => 'setting'
);
$form['notification_email']['#attached']['js'][] = array(
  'data' => 'file.js',
  'type' => 'file'
);
$form['notification_email']['#attached']['js'][] = array(
  'data' => 'http://domain.com/file.js',
  'type' => 'external'
);

----------------------
forward-media.de

Jaypan’s picture

Thanks, that was it.

dx007’s picture

You save my day! Thank you