I'm familiar with the process of attaching JavaScript and CSS to specific forms and form items in Drupal 7 using the #attached attribute.
I'm running into an issue which that solves, but unfortunately I'm on a D6 installation.
Basically, drupal_get_form() is being called from the theme layer (in a custom template file). The form it's printing has form_alter hooks which execute drupal_add_js() (and css), but because the drupal_get_form is being called in the theme layer, those JS files are being added too late, and therefore not being included.
What's the best Drupal 6 solution to this problem?
As said in the question, drupal_get_form()
is called too late in the page building process for the drupal_add_js()
and drupal_add_js()
calls to have any effect. Calling drupal_get_form()
in the theme layer is usually not an issue, but in this case I'm assuming that in this case it's called in a page-<something>.tpl.php
. Becasue of CSS and JS addition happen after the execution of template_preprocess_page()
which build the $script
, css
and $styles
variables.
A simple solution is to call drupal_get_form()
from your theme's page template preprocessor and then re-build the $script
, css
and $styles
variables.
function THEME_preprocess_page(&$variables) {
$form = drupal_get_form('the_form_name');
$variables['my_form'] = $form;
$variables['styles'] = drupal_get_css();
$variables['css'] = drupal_add_css();
$variables['scripts'] = drupal_get_js();
}
A better solution would be to call drupal_get_form()
earlier, from a lower level template (such as node.tpl.php
ou user.tpl.php
). But the best would be to retrieve the form outside of the theme layer in a page, block, etc. callback function.