Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

Theme functions are defined in the template.php but is it possible to define hook functions in there? for example There is a content type called phone-book, its add content form ID is phone-book-node, I want it to redirect to my custom page when it is submitted. so I created this function in the template.php :

function MYTHEME_form_phone_book_node_form_alter(&$form, &$form_state, $form_id) {
$form_state['redirect'] = 'go/some/where';
}

but it doesn't work. I don't know if it is totally impossible to define such hook functions inside template.php or something is wrong with my code.

share|improve this question
2  
I'm pretty sure any alter hook can go in template.php or a custom module. –  Felix Eve Apr 25 at 14:42
add comment

1 Answer 1

up vote 5 down vote accepted

Yes, you can implement any alter hook in a theme, as well as hook_theme() and preprocess hooks.

Adding $form_state['redirect'] won't do anything there, though. You need to do that in a submit handler:

function MYTHEME_form_phone_book_node_form_alter(&$form, &$form_state, $form_id) {
  $form['#submit'][] = 'MYTHEME_phone_book_node_form_submit';
}

function MYTHEME_phone_book_node_form_submit($form, &$form_state) {
  $form_state['redirect'] = 'go/some/where';
}

To ensure that your hook is running, get the Devel module installed and add dpm('alive'); in there. Then clear the caches (to be sure) and refresh the page a couple of times. If you don't see the debug message, the hook isn't being called, and it might be named incorrectly.

share|improve this answer
    
Thanks. I did this but I saw no debug message. the hook is not being called –  Drupalist Apr 25 at 14:44
    
Then it's likely the function is named wrong (check the name of your content type), or you haven't cleared the caches... –  Clive Apr 25 at 14:45
    
Please look at this screenshot of Firebug http://i.cubeupload.com/4wwXbs.png every thing seems to be right but after node submission the form is redirected to the node page. –  Drupalist Apr 25 at 14:54
    
Are you trying to keep the user on the same page after submitting the form? If so, try Add another –  Clive Apr 25 at 14:55
1  
another solution is to use rule module and add a rule/action statement. –  Drupalist Apr 25 at 15:41
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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