Hi !

I add some extra fields to node using hook_form_alter() but also i need add some CSS to style them. So I have something like:

<?php function node_thumb_form_alter(&$form, $form_state, $form_id) {
  drupal_add_css(drupal_get_path('module', 'node_thumb') .'/node_thumb.css');
?>

and after i load my node page all is right but after i send the node form and some get some errors in fields (for example required field was empty) my node_thumb.css wasn't load. Why? What I'am doing wrong?

Comments

vasi1186’s picture

Hi jan,

I think the problem is that the form is loaded from cache when an error appears in submission, so that the hook_form_alter() is not invoked. I also had this problem when I wanted to alter a node form, and I solved it by putting the statement that adds the css in the hook_nodeapi, on the $op = 'prepare' (http://api.drupal.org/api/function/hook_nodeapi/6).

I hope to work also for you...

Vasi.

jan.koprowski’s picture

Thank You for greate response :) I found very intresting discussion drupal_add_js in hook_form_alter where solution which seems to be ideal for me was add css using #build_after trigger :) I just add:

$form['#build_after'] = array_merge((array)$form['#build_after'], array('module_add_stylesheet'));

Working like a charm:)

jaypan’s picture

Marking for reference.

Contact me to contract me for D7 -> D10/11 migrations.

Anonymous’s picture

It's $form['#after_build'] not $form['#build_after']. So, this works perfectly :

<?php
function module_name_formID_form_alter(&$form, &$form_state) {
	//Modifications
	$form['#after_build'][] = 'custom_function';
}

function custom_function(&$form, &$form_state){
	drupal_add_css(drupal_get_path('module', 'your_module').'/your_css.css');     
	drupal_add_js(drupal_get_path('module', 'your_module').'/your_js.js');
	return $form;  
}
?>

Thanks for the help!