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.


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.

  • Thanks. I did this but I saw no debug message. the hook is not being called – M a m a D Apr 25 '14 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 '14 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. – M a m a D Apr 25 '14 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 '14 at 14:55
  • 1
    another solution is to use rule module and add a rule/action statement. – M a m a D Apr 25 '14 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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