hook_form_alter doesn't submit in Drupal.

davecoventry's picture

He has: 112 posts

Joined: Jun 2009

I have the following hook_form_alter:

<?php
function sportsa_form_alter(&$form, $form_state, $form_id){
    switch(
$form_id){
        case
'add_new_sport_node_form':
           
$form=Array (
               
'title' => Array (
                   
'#type' => 'textfield',
                   
'#title' => 'Title',
                   
'#required' => 1,
                   
'#default_value' =>'',
                   
'#maxlength' => 32,
                   
'#weight' => -5,
                   
'#description' => 'You can seperate multiple Sports with commas.',
                ),
               
'body' => Array (
                   
'#type' => 'textarea',
                   
'#title' => 'Forum Description',
                   
'#default_value' =>'',
                   
'#rows' => 4,
                   
'#description' => 'Covering blurb for the forum description.'
               
),
               
'submit' => Array (
                   
'#type' => 'submit',
                   
'#value' => t('Save'),
                   
'#submit' => Array('add_new_sport_node_submit')
                )
            );
            return
$form;
        break;
    }
}

function
add_new_sport_node_submit($form, &$form_state){
   
print_r($form);//<- Never gets here
}
?>

It seems to work fine and the form is displayed as designed, but there is no reaction when the User clicks 'Save'.

What am I doing wrong?

JeevesBond's picture

He has: 3,956 posts

Joined: Jun 2002

It's because you're using hook_form_alter for something it's not designed to do. Smiling

The code above is trying to build the whole form array, but it doesn't work because the hook is only for altering existing forms. Looking at the code it generates there's no form tag, for example. Changing:

<?php
$form
=Array (
?>

to:

<?php
$form
+= Array (
?>

Stops the form being destroyed, but might also break what you were trying to do. The way to show the user a form is to implement hook_menu with 'page callback' => 'drupal_get_form', and 'page arguments' =>  array('sportsa_add'), where page arguments is the name of the function that provides the form.

There's an example module with a couple of page hook examples, almost all modules include a menu example. user.module for example, has user_menu, one of the things this points to is the user_register form.

Anyway, not sure that's what you're trying to do, hope this helps nevertheless.

a Padded Cell our articles site!

davecoventry's picture

He has: 112 posts

Joined: Jun 2009

Yes.

I've abandoned this approach.

As you say, it's clearly not designed to do this.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.