Drupal7 表單內載入 css, js, attach

Edit: 2012-11-07: external css

Drupal7 中以下的代碼是不會達到你想要的效果的

<?php
function hook_form_alert(&$form, &$form_states) {
 
drupal_add_js(drupal_get_path('module', 'example') . "/js/example.js");
}
?>

hook_form_alter 中使用 drupal_add_css() drupal_add_js() 是沒有效果的

正確的做法是 form 的 preprocess 或者使用 form API 的 #attach:

<?php
$form
['#attached']['css'] = array(
 
drupal_get_path('module', 'ajax_example') . '/ajax_example.css',
);

$form['#attached']['js'] = array(
 
drupal_get_path('module', 'ajax_example') . '/ajax_example.js',
);
?>

Attach external js, key 是 URL, value 是 array:

<?php
$form
['#attached']['js'] = array(
 
'//ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js' => array('type' => 'external'),
);
?>

ref:
http://api.drupal.org/api/drupal/developer!topics!forms_api_reference.ht...

Google