views:

29

answers:

3

Hi,

I want to redirect my form after submissions, I'm trying to use a hook, but this is what I get:

function node_form_submit(&$form, $form_state) {  
    $form_state['redirect'] = 'contentManager/';
}

Fatal error: Cannot redeclare node_form_submit() (previously declared in drupal/modules/node/node.pages.inc:451) in drupal/sites/all/themes/bluemarine/template.php on line 31

I'm using the module name "node" for my hook. I guess this is wrong, but I don't understand why

Update: Ok, I'm not developing a new module. I've added the function to template.php in my Bluemarine template.

function bluemarine_form_alter(&$form, $form_state) {
    $form_state['redirect'] = 'contentManager/';
}

Bluemarine is the name of my theme.. "contentManager" is a page I've created in my backend with a View with all nodes.

This function is completely ignored.

Update2 Ok, so now, this is my current template.php file (I'm sure phptemplate_node_form is invoked)

function phptemplate_node_form($form) {
  $form['#submit'][] = 'myFormSubmit';
}

function myFormSubmit() {
    echo "hello";
    die();
}

The function myFormSubmit is not invoked.

thanks

+1  A: 

PHP doesn't allow duplicate function names. node_form_submit which isn't actually a hook, but a form submit handler, is implemented in the node module. That is why you are getting the error.

All hooks should always start with your module / theme name. This is done to avoid the exact problem with duplicate function names you are having.

If you want to add a form submit handler to a form, you need to use hook_form_alter on the form and add your submit handler to $form['submit'].

Modules:
When developing a module, you need to use a module_name.module file. This is where the core of your code should go, along with every hook implementation.

Themes:
When developing a theme, you should use the template.php file for all preprocess functions, theme overrides, hook implementations etc.

What's wrong:
You say you both have a bluemarine module and theme. If you truly have both, you should rename of the them.

When using hook_form_alter, you should specify the $form_id, so you don't override all forms.

If you want your effects on $form_state to carry through, you need to pass it by reference using the & notation.

Update 2:
Unless you are using Drupal 7, you can't use hook_form_alter in a theme's template.php file. You will have to create a custom module to implement any hook.

googletorp
ok, I've updated my question..
Patrick
sorry, I meant template. I'm not creating a new module at all. I just want to change my form forwarding from my template.php file. That's it. I've updated my question.
Patrick
A: 

you can't declare hook_form_alter in a theme. you must do it in a module. please notice that declaring mymodule_form_alter requires three arguments and you should check whether $form_id == 'form_name' before applying changes (otherwise you will alter EVERY form in drupal).

if you don't know the form_id of the form you want to change you can just put print_r($form_id . ' '); in your hook and it will print the form_id for every form in the page. you can then use the form_id to declare a more specific hook, and add your changes there.

finally, if what you want to do is change the redirect in a submit form, you need to add a function name to $form['#submit'] (add, not substitute, see the forms api docs) and in that function, which should run after node_form_submit, change $form['#redirect'] to whatever you please.

barraponto
ok i've updated my question (Update2)
Patrick
+1  A: 

"I'm using the module name "node" for my hook. I guess this is wrong, but I don't understand why"

You can't do this because node is already a module, and therefore, node_ functions are already defined.

To use hooks you need to create your own module, and the naming convention is then mymodule_hookname().

Within modules that have their own hooks, Drupal has a function called module_invoke_all, which invokes a specific hook across all modules during execution based on the hook name.

You cannot dump programming into template.php as this is not it's purpose. This all needs to be in a module, and is easily achievable with hook_form_alter, changing the form submit, and having a mymodule_form_submit() function.

Kevin
so, I have 2 questions: (1) For each module I want to modify, I should create a new module. Let's say I want to change 3 small things in 3 different modules, I have to create 3 new modules. Is this correct ? (2) If I want simply to forward the user to another page after the form submission, should I create a new module ? I usually do small changes to forms from my template.php file (for example changing weight, default collapsing), but I want to change the forwarding, I should create a new module ?
Patrick
If what you want to do is change forms, you can use one hook_form_alter and provide sets of changes to it based on $form_id. It will change any form you want based on field name, and you can limit it wrapped in an if($form_id == 'node_form') for example.
Kevin
so in other words, I guess this answer question 1, I can change forms of different modules, from 1 new module, providing different $form_ids. And if I want to change the form forwarding I don't have other options, if I can only create a new module ?
Patrick
Yes. Stuff like this needs to go into a module. This will help http://drupal.org/node/204270
Kevin

related questions