0
votes

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

3

3 Answers

1
votes

"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.

2
votes

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.

0
votes

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.