2

I have a page with a table of nodes each one with it's own submit button. When pressed the button should call a custom ajax function which creates a cookie with the corresponding orm ID as its value.

My code:

function custom_cart_form_alter(&$form,&$form_state,$form_id){      
    if ((stristr($form['#form_id'], 'uc_product_add_to_cart_form')) != FALSE){                      
        $form['#submit'][] = 'set_custom_cookie';
    }

    return $form;
}

function set_custom_cookie($form, &$form_state){
    user_cookie_save(array($form['#form_id'] => TRUE));     
}

However, when the set_custom_cookie() function is called it receives the last value of $form['#form_id'] and not the ID of the corresponding form.

To clear things up: if I add the following at the end of hook_form_alter() :

function custom_cart_form_alter(&$form,&$form_state,$form_id){      
    if ((stristr($form['#form_id'], 'uc_product_add_to_cart_form')) != FALSE){                      
        $form['#submit'][] = 'set_custom_cookie';
    }

    $form['#form_id'] = "test123";  //Added this line to demonstrate the issue
    return $form;
} 

then function set_custom_cookie() will receive "test123" on call as the value of $form['#form_id'].

How can I make set_custom_cookie() receive the corresponding form ID?

I'm using Drupal 7.

1 Answer 1

1

You should be able to get the form id from the $form_state variable as $form_state['build_info']['form_id'] inside your submit handler.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.