0

When debugging commerce checkout panes, which are basically forms I see that form is loaded on page load(obviously). It is again loaded when I click submit and debugger goes through same lines of form, thing is I am not sure which marker to use to differentiate code when this occurs, I would need to do something different in this case, but only thing I see different is superglobals at this point, which I could use, but somehow I would be more comfortable using drupal local vars?

2
  • Maybe you're looking for something like $form_state['submitted']?
    – Clive
    Commented Nov 25, 2019 at 11:00
  • I am, but it is still the same at this point, this is before this changes. I was checking all the form_state vars and they are the same in both cases at this point.
    – Blissful
    Commented Nov 25, 2019 at 11:02

2 Answers 2

2

If all you want to know is whether the form is being loaded before or after a submit while debugging, then look at the array in $form_state['input']. If it is an empty array, then it is being loaded for the first time (before a submit). If it has elements then the form is being loaded after a submit.

If you want to see if field values have been changed in a submit, then how you do it depends on where or why you need to check. If debugging, then compare $form_state['input'][FIELD] with $form[FIELD]['#default_value']

You can also add your own functions as submit or validate handlers to the form. After the form is submitted, your function will be called, and you can compare the changed values in $form_state['values'][FIELD] (note: use values here, not input) to the original values in $form[FIELD]['#default_value'].

You add your submit/validate handler by implementing hook_form_FORMID_alter()for the form you are debugging, and adding the names of your functions to the end of the $form['#submit'] and $form['#validate'] arrays respectively. Remember to clear caches.

function my_module_form_other_module_form_alter(&$form, &$form_state) {
  $form['#submit'][] = 'my_module_custom_submit';
  $form['#validate'][] = 'my_module_custom_validate';
}

function my_module_custom_submit($form, &$form_state) {
  $original_field_x_value = $form['field_x']['#default_value'];
  $submitted_field_x_value = $form_state['values']['field_x'];
  if ($submitted_field_x_value != $original_field_x_value) {
    do_something();
  }
}

function my_module_custom_validate($form, &$form_state) {
  do_something();
}
1
  • Now that you say this I remember some long time ago using $form_state['input'] and $form_state['values'] and checking those, rings a bell, thnx for this I will check it out.
    – Blissful
    Commented Nov 26, 2019 at 20:33
0

I also needed to check the difference for a field. In my case the field was called field_phone, and below is what worked for me

function myModule_user_profile_form_submit(&$form, FormStateInterface &$form_state) {
  $phone = $form_state->getValue('field_phone')[0]['value'];
  if ($phone != $form['field_phone']['widget'][0]['value']['#default_value']) {
  // Field data changed...
  }
}

Your Answer

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

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