views:

1293

answers:

3

I originally started this question in another thread, but that thread was sorta, kinda answered, and now I primarily want to know how to specify another form action... I tried using the code below, but the form action, when output, remains unchanged, although looking at the print_r($form), it's correctly changed... Why isn't it picking up?

function mytheme_user_profile_form($form) {
        global $user;
        $uid = $user->uid;
        //print '<pre>'; print_r($form); print '</pre>';
    $category = $form['_category']['#value'];

    switch($category) {
            case 'account':
                $form['#action'] = '/user/'.$uid.'/edit?destination=user/'.$uid;
                        break;
        case 'education':
                        $form['#action'] = '/user/'.$uid.'/edit/education?destination=user/'.$uid;
                        break;
        case 'experience':
                        $form['#action'] = '/user/'.$uid.'/edit/experience?destination=user/'.$uid;
                        break;
            case 'publications':
                        $form['#action'] = '/user/'.$uid.'/edit/publications?destination=user/'.$uid;
                        break;
        case 'conflicts':
                        $form['#action'] = '/user/'.$uid.'/edit/conflicts?destination=user/'.$uid;
                        break;
    }

        //print '<pre>'; print_r($form); print '</pre>';
        //print $form['#action'];
        $output .= drupal_render($form);
        return $output;
A: 

I don't have a chance to test this right now. But I think that you will need to use hook_form_alter to do this before you get to the theming layer. Drupal has some securty code in it to prevent forms being spoofed I recon this is what is catching you out.

Jeremy French
+3  A: 

hook_form_alter() is likely the way to go. Here are some hopefully helpful links:

http://drupal.org/node/304170

http://www.lullabot.com/articles/modifying-forms-5-and-6

http://api.drupal.org/api/function/hook_form_alter/6

EDIT: reply to comment #1 below:

How to implement hook_form_alter():

You must create a module (you cannot use template.php). It's easier than it looks.

For a module named 'formstuff', you would create formstuff.info and formstuff.module and put them in either sites/all/modules or sites/yoursitename/modules. Set up the .info and .module files per the instructions (see below link to tutorial), then just create the following function in your .module file:

function formstuff_form_alter(&$form, $form_state, $form_id) {
  // do stuff
}

This function is a hook because it is named properly (i.e. replace the word 'hook' with the name of your module), and it matches hook_form_alter's function signature (i.e. it takes the same parameters).

Then just enable your module in your site's admin and the hook should do it's magic.

Note that hook_form_alter takes a reference to the form - this allows you to modify it in-place.

gg - late for work.

Link: http://drupal.org/node/206753

threecheeseopera
I tried using form_alter, but it didn't work. Although, I put the code in my template.php file. Is that right?
n00b0101
A: 

You need to put the form_alter function in a module and then use either IF or SWITCH to check the form ID. If the form ID is the one you want to alter then give the form an action property $form['someID'] = array( '#action' => 'path/you/want', );

I tried this, added it to my my.module and flushed the caches,etc., but still didn't work: <pre>function user_profile_form_alter( } } break; }}</pre>
n00b0101
The function name should be "my_module_form_alter". To avoid the switch statement, you can use "my_module_form_user_profile_form_alter", assuming you have the form id right.
Grayside