Okay, so I've searched high and low and seem to be getting conflicting information on the use of hook_form_alter in template.php.

I have found this article (http://drupal.org/node/154137) (on drupal) that demonstrates the use of hook_form_alter.
While there is lots of half-stated mentions that you can't use hook_ functions in template.php.

Looking at the hook_form_alter API(7) documentation on drupal.org also does not mention that it cannot be used in template.php. There is even mention of the theme being the last thing to process the function in the processing stack.

Both methods have been tried and neither work (yes, I have cleared the cache).

Can someone please give me a definitive answer on ifhook_form_alter can be used in template.php; and if yes, any possible reasons as to why the code in my template.php is not working.

Appreciate your help.

(p.s. template.php is working as I have some other functions in it that are working fine).


arshadcn’s picture

Can you post some codes here?

Arshad Chummun
projects | twitter

laurencemercer’s picture

yes hook_form_alter can be used in a Drupal 7 template.php file.
See: http://drupal.org/node/254940#theme_alter for confirmation.
Also, I've used it :)

It's hard to say why it's not working for you without seeing your code - could you post it?

LHM Design — Drupal theming and frontend development
My Drupal blog — Drupal theming, one post at a time

sunchaser’s picture

Same issue here I'm afraid.

I just have this in my template.php:

function <themename>_form_alter(&$form, &$form_state, $form_id) {
       drupal_set_message("Look at that! We are in a form ... supposedly");

Behavior: Message is never displayed.

above code works fine when called in a custom module :/

chellman’s picture

If the theme you're editing is not active on the page of interest, this won't work. If, for example,
is the default theme, but you're using Seven as your admin theme (and your admin theme is used for content editing -- checking the settings at the bottom of the Appearance page), this form_alter won't be called.

If you're sure you have the themes set correctly, you should try clearing the Drupal cache to make sure the form_alter is being picked up. This is on Admin > Configuration > Development > Performance.

Fix those two things, and you should be in business.

hoff331’s picture

I have the following code added to my template.php file:

function MYTHEME_form_alter(&$form, &$form_state, $form_id) {
       drupal_set_message("Look at that! We are in a form ... supposedly");
	$form['field_my_field']['und']['0']['value']['#attributes']['disabled'] = TRUE;

The Drupal message displays properly, buy my field is still editable by non-admin users. I also get the following error:

Notice: Undefined index: #type in template_preprocess_search_block_form() (line 1070 of /home/content/c/h/r/chrishoff331/html/MYTHEME/modules/search/search.module)

Is my code wrong or is there another reason why this is conficting with the Drupal 7 core search module?

chellman’s picture

This is the code you want:

$form['field_my_field']['#disabled'] = TRUE;

What you have above is what the form code looks like just before it's rendered, which is not where the altering happens.

Here's the Form API reference, which might help.

egomezpe’s picture

I was wondering why I had this problem (in theme, the function never get called) and the point was I was editing the form with the seven theme...

I think in my case, that i was doing some extra validations, the best is put the code in one module, so the code gets called in spite of the theme used