By drumdance on
Per the API example, I'm trying to inject an attribute into a form. Specifically, I'm trying to add a Javascript onsubmit handler like so:
$form['#attributes']=array('onsubmit'=>'my_submit_handler();');
However, Drupal appears to be ignoring the #attributes array. Even when I use the example code of:
$form['#attributes'] = array('class' => 'search-form');
...these attributes get ignored. I've tried injecting these changes in both hook_form and hook_form_alter. No dice. I have succeed in altering other form attributes such as #action
Any suggestions?
TIA,
dd
Comments
Use clean JS
You're really much better off adding the handler through code rather than hardcoding it. However, it's possible #attributes is simply not supported for forms at the moment. Feel free to file a bug report.
--
If you have a problem, please search before posting a question.
You're really much better
Could you elaborate? I AM trying to alter it in code: PHP code. Is there a better way & place for me to attach to the form? I only want to attach this on one particular form (ie I need to take a peak at $form_id in hook_form) and it seems cleaner to do it here than in Javascript on the page. Otherwise I'd have to load the JS every time and check the form tag then. Seems like a waste of bandwidth to me.
Which form?
Which form are you trying to add this to? It's possible that your module has a lower weight than another module which also adjusts the form attribute, and therefore your changes are being overwritten.
Try setting the weight of your module to a higher level (you can do this by adjusting a single field in the systems table, but I'm make sure to use LIMIT 1 in your query just in case...).
Dave
Bingo!
Wow, this fixed it. Thanks for the tip!
hook_install
In the future, you can achieve the same effect with hook_install, and an update statement. Do a search through this site for 'adjusting weight of a module' and you'll see what I'm talking about.
Obviously this is only if you intend to release your module to the general public.
Dave
Perhaps a better solution
After playing around with it a little more (and actually after introducing a new bug as a result), I've found that this plays nice with existing form attributes that may have been injected:
The following sentence is true.
The preceding sentence is false.
http://www.AskDerekScruggs.com
change weight of module
hi, i was trying your solution, but dont know exactly how to do.
for example i changed the 'weight' field for (module) 'content' in table 'systems' to 20, which that didnt help).
what field do i have to change now, if i wanna inject $form['field_select_nodereference']['#attributes'] in a cck node-edit template ?
can anyone help me out there ? thanx!
hmmm...
I tried this on drupal 5.2 and it didnt work..
I also tried other ways, cant get it, any ideas?
Thanks
What did you actually try?
It would help to see the code you tried in order to help you.
!!
No I had tried with the exact same syntax but it didnt work...
I managed to make it work though later with a similar, but not exactly the same syntax.. and eventually it did..
Thanks anyways