Question

Currently I have a products page using the Views and Drupal Commerce modules with a sort functionality of the product categories (exposed filter). The form with the dropdown list/select list has a GET method, so the category ID is appended to the URL. My goal is to grab the category ID appended to the URL and redirect users to a specific product page instead of having just the category id appended to the end of the URL.

I've googled for answers regarding this problem but none of them has worked. So what I did was I created a custom module that implements the hook_form_alter module. I wanted to add a custom submit handler to the form, so that after the first form submission, another submission is triggered in order retrieve the category ID from the URL, which then redirects the user to a specific page. My code is as below for the module.

function custom_form_submit_form_alter(&$form, &$form_state, $form_id) {
    if ($form_id == 'views_exposed_form') { 
         $form['#submit'][] = 'my_custom_handler_submit';
    }   
}


function my_custom_handler_submit(&$form, &$form_state) {
    if ($_GET['field_categories_tid'] == 13) {
         $form_state['redirect'] = '/products/furnitures';
         }
}

The first function works as it's returning values of the form when I did kpr($form) with the Devel module enabled. The second function doesn't seem to be triggered at all. I've been stuck on this for couple of days and I couldn't really figure out after tons of research. I'm relatively new to Drupal and is hoping someone could shed some light on this. Your help is much appreciated!

Était-ce utile?

La solution

I think I have this problem sorted out. Here's what I did in case someone runs into the same issue in the future. My custom submit handler was never triggered simply because $form_state['no_redirect'] is set to TRUE by default. So I did a conditional statement where if the URL contains category ID, $form_state['no_redirect'] = FALSE. My custom submit handler is then triggered. I hope this helps.

Autres conseils

The default submit function which runs before your function might redirect or did not able to jump to the function which you attached through form_alter. You can sort this by calling your function first through

$temp = $form['#submit'][0];
$form['#submit'][0] = 'my_custom_handler_submit';
$form['#submit'][1] = $temp;

i see problem in the hook name

custom_form_submit_form_alter

my_custom_handler_submit

I don't know wich one is the good one but they should be the same

hope it helps

PR

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top