开发者

drupal_add_js() only adds the JS when no error message (D6)

开发者 https://www.devze.com 2023-03-28 10:53 出处:网络
In my custom form (in a custom module) drupal_ad开发者_StackOverflowd_js() only adds the JS when there is no error message.

In my custom form (in a custom module) drupal_ad开发者_StackOverflowd_js() only adds the JS when there is no error message.

My code goes like this:
function ntcf_redo_order_form( &$form_state = array() ) {
  global $base_path, $user;

  $my_dir = drupal_get_path('module', 'ntcf_redo');
  drupal_add_js("$my_dir/order.js", 'module', 'header', FALSE, TRUE, FALSE);

  $form = array();
  ...
  return $form;
  }

If the validation function used _form_set_error()_ to display an error message and highlight the offending field, the message is displayed and the field highlighted, but the _drupal_add_js()_ call does nothing. Without a pending error message to display, all is well.

EDIT: this problem does not occur with drupal_set_message(), only with form_set_error().

I tried adding the 3 later parameters to the *drupal_add_js()* call to tell it to not optimize it (don't combine it with other JS files). There is no mention of the file order.js in the HTML, and it makes no difference whether I use the last 4 parameters ('header', FALSE, TRUE, FALSE) or not.

In Admin/Performance, I turned off Optimize Javascript Files, and pretty much all caching, which also made no difference.

Extra Details: I'm not sure if this makes a difference, but it wouldn't surprise me, so here goes: What I'm doing here is a multi-part "wizard" form that allows the user to proceed forward and go back. Also, many of the pages use AJAX, so I need to do all the "required" field validation in the _submit function instead of letting Drupal do it automatically (since that makes a mess of AJAX). So, if there's a "required" field that's missing, the _submit() function sets an error message, and the form generation function generates the same form again (with the additional decoration resulting from the error message).

Also: this is off-topic, but it might help someone using Google: when doing a multi-page form that allows going backward, you MUST assign a weight to every item on the form, or else the fields tend to "wander" when you go backwards.

Any ideas?


I had the same problem, this is a workaround I found (for Drupal 7, may work in 6) :

1. in your form setup (or hook_form_alter), do this :
  $form['#post_render'][]='yourfunction';

2. define :

function yourfunction($content,$element){
 $my_dir = drupal_get_path('module', 'ntcf_redo');
  drupal_add_js("$my_dir/order.js", 'module', 'header', FALSE, TRUE, FALSE);
 return $content;
}

I think this works (while your approach does not), because hook_form_alter (and/or hook_form) do NOT get called again for a prepared/cached form, so the initial form load WILL load the javascript, but subsequent posts will NOT. HTH


Mikes answer ($form['#post_render'][]='yourfunction';), will work, though its not the optimal way and will cause issues with drupal_add_js.

The best way to do this is by adding your javascript via the form api '#attached'.

Instead of using drupal_add_js or a new callback on the '#post_render':

$form['#attached']['js'] = array(
  drupal_get_path('module', 'module_name') .'/file/path/filename.js',
);

You may pass in a 'css' array as well. Being an array, you can pass in as may files as you want.

*This is for Drupal 7. Other versions may be different.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号