0

I am trying to make the changes in commerce_checkout_form but I am facing some issues. Here is my hook_form_FORMID_alter :

function MYMODULE_form_commerce_checkout_form_checkout_alter($form, $form_state, $form_id){ 
  drupal_add_js(drupal_get_path('module','MYMODULE').'/js/checkout_validate.js');
  $form['customer_profile_shipping']['#title'] = '<h2><b>Shipping Information</b></h2>';
  if(isset($form['account'])){
    $form['account']['#title'] = '';
  }

  if ($form_id == 'commerce_checkout_form_checkout') {
    $form['buttons']['continue']['#validate'][] = 'MYMODULE_form_commerce_checkout_form_checkout_validate';
    dsm($form['buttons']['continue']);
    $form['buttons']['continue']['#attributes']['class'][] = 'classTest';

    if (isset($form['buttons']['continue']['#value'])) {
        $form['buttons']['continue']['#value'] = t('Continue');
    }

    // $form['buttons']['continue']['#validate'] = array_reverse($form['buttons']['continue']['#validate']); // I tried this to give preference to my validate function
    dsm($form['buttons']['continue']);
    dsm($form_state);
  }
}

function MYMODULE_form_commerce_checkout_form_checkout_validate(&$form, &$form_state){
  // This is function is not getting called.
  watchdog('MYMODULE_form_commerce_checkout_form_checkout_validate','partner ID. Response details: @request_details',array('@request_details' =>  print_r($form_state,true)), WATCHDOG_DEBUG);
}

I am getting all the dsm on page, means MYMODULE_form_commerce_checkout_form_checkout_alter is getting called but I can see that, neither 'classTest' is added to continue button nor value of continue button is changed.

This is part of dsm($form_state) :

rebuild :  (Boolean) FALSE
rebuild_info :  (Array, 0 elements)
redirect :  (NULL)
temporary :  (Array, 0 elements)
submitted :  (Boolean) FALSE
executed :  (Boolean) FALSE
programmed :  (Boolean) FALSE
programmed_bypass_access_check :  (Boolean) TRUE

$form_state['rebuild'] value is false. Is this the reason, I am not abel to see the changes?

1

Try your code inside hook_form_alter instead of hook_form_commerce_checkout_form_checkout_alter.

function MYMODULE_form_alter($form, $form_state, $form_id){
  if ($form_id == 'commerce_checkout_form_checkout') {   
    drupal_add_js(drupal_get_path('module','MYMODULE').'/js/checkout_validate.js');
    $form['customer_profile_shipping']['#title'] = 'Shipping Information';
    $form['customer_profile_shipping']['#prefix'] = '<h2><b>';
    $form['customer_profile_shipping']['#suffix'] = '</h2></b>';
    if(isset($form['account'])){
      $form['account']['#title'] = '';
    }
    $form['buttons']['continue']['#validate'][] = 'MYMODULE_form_commerce_checkout_form_checkout_validate';
    dsm($form['buttons']['continue']);
    $form['buttons']['continue']['#attributes']['class'][] = 'classTest';
    if (isset($form['buttons']['continue']['#value'])) {
       $form['buttons']['continue']['#value'] = t('Continue');
    }
     // $form['buttons']['continue']['#validate'] = array_reverse($form['buttons']['continue']['#validate']); // I tried this to give preference to my validate function
    dsm($form['buttons']['continue']);
    dsm($form_state);
   }
}
  • @DineshPatil nice to hear that.. – Abin May 27 '16 at 11:30
3

Should it not be that the arguments are passed by reference? You are not returning anything, therefore any changes you make within the function will have no affect outside it's scope.

function MYMODULE_form_commerce_checkout_form_checkout_alter(&$form, &$form_state, $form_id){ .... }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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