I have created a custom module that changes the layout & adds extra functionality to one of my content type creation and edit forms.

It works beautifully and as expected apart from when I submit the form with, for example, a blank node title or any other required fields.

The page is reloaded as expected with the error message, the form modifications are loaded from the module (field #prefix, #suffix etc..) but the css and the js are not loaded.

Here is the code I have:

/**
* Implementation of HOOK_form_alter()
*/
function mymodule_entry_form_modify_form_alter(&$form, $form_state, $form_id) {

  // 1.ENTRY FORM
  if ($form_id == 'entry_node_form') {

  // here were some form field modifications i removed


/* 1.3 LAYOUT - ADD JAVASCRIPT FILES
 * Add .js files
 */
// add jquery script before calling it
drupal_add_js(drupal_get_path('module', 'mymodule') . '/includes/js/jquery.fixheight.js');
// call the script
drupal_add_js(drupal_get_path('module', 'mymodule') . '/includes/js/mymodule_entry_form_modify.js');


/* 1.4 LAYOUT - ADD CSS FILES
 * Add .css files
 */
drupal_add_css(drupal_get_path('module', 'mymodule') . '/includes/css/mymodule_entry_form_modify.css');    

 }
}

Can anyone explain why this happens?

link|improve this question

50% accept rate
I'm still using drupal 7.9 – tecjam Dec 8 '11 at 13:36
If I understand your code correctly the js and css are just loaded only inside the form. – ninjascorner Dec 8 '11 at 13:49
well yes, what the codes says is if the $form_id == 'entry_node_form' (my custom content type is called 'entry') it does the following ... which includes heaps of $form modifications, for example: $form['field_entry_cust_id']['#disabled'] = TRUE; and it also adds javascripts and css files. I only want them to be applied and loaded to that specific content form of course. – tecjam Dec 8 '11 at 14:10
@tecjam is it a typo where in you've used mymodule at one place and mymodul in another? – optimusprime619 Dec 8 '11 at 14:15
Hi, yes it is. My custom module has a different name with regards to the project i'm working on which I didn't feel like enclosing. I've edited it to avoid any further confusion. ;-) – tecjam Dec 8 '11 at 14:43
feedback

1 Answer

up vote 3 down vote accepted

I don't use D7, but I believe you should be using #attached and it should fix your issue.

link|improve this answer
You are right, now it works. Is there any specific reason why drupal_add_ doesn't work in this case, or is it a bug? – tecjam Dec 8 '11 at 14:06
1  
@tecjam It's by design not a bug. The form is cached, and so the code to build it (including your form_alter) is never called whenever the cache is used. That's why it's important that the data's actually stored in the FAPI array itself. – Andy Dec 8 '11 at 14:16
1  
Thanks for your answer, Andy. Again I've learnt something new with regards to FAPI. =) – tecjam Dec 8 '11 at 14:49
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.