4

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;
kenorb
  • 155,785
  • 88
  • 678
  • 743
n00b0101
  • 6,863
  • 17
  • 42
  • 36
  • sorta / kinda? ;) def. let me know if you need more help. – mikewaters Oct 15 '09 at 17:10
  • If you want to edit the action url, I found this article which helped me: http://actionscript-flash-guru.com/blog/47-exposed-form-in-block-change-the-action-url--drupal-6--views--hookformalter – N D Aug 01 '11 at 06:48

2 Answers2

7

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

Form Theming: How do I set $form['action']?

Modifying Forms in Drupal 5 and 6

hook_form_alter

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

htoip
  • 437
  • 5
  • 19
mikewaters
  • 3,668
  • 3
  • 28
  • 22
  • I tried using form_alter, but it didn't work. Although, I put the code in my template.php file. Is that right? – n00b0101 Oct 15 '09 at 21:04
1

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',
);
htoip
  • 437
  • 5
  • 19
user190907
  • 11
  • 1
  • I tried this, added it to my my.module and flushed the caches,etc., but still didn't work:
    function user_profile_form_alter(&$form, $form_state, $form_id) {
     switch($form_id) {
      case 'user_profile_form':
      if(arg(0) == 'user' && is_int(arg(1))) {
       if($form['_category']['#value'] == 'education') {
        $form['#action'] = 'user/'.arg(1).'/edit/education';
       }
      }
                    break;
     }
    }
    
    – n00b0101 Oct 16 '09 at 00:35
  • 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 Oct 16 '09 at 15:45