0

Goal: Print/dump $formState values to debug

I am altering a form and the functionality works:

$form['elements']['location']['deparment']['#ajax'] = [
  'callback' => 'myAjaxCallback',
  'event' => 'change',
  'progress' => [
    'type' => 'throbber',
    'message' => t('Verifying entry...'),
  ]
];

but what I want is to be able to print out $formState in myAjaxCallback

function myAjaxCallback(array &$form, FormStateInterface $formState){
 $response = new AjaxResponse();
 print_r($formState); //I've tried, with memory_limit=-1 . it just hangs
 dump($formState); //gives me File upload exceeds. This comes from issue with using dump in ajax
                     https://drupal.stackexchange.com/questions/250078/ajax-submission-leads-to-an-unrecoverable-error-occurred-the-uploaded-file-l

 return $response
}

I have also read https://www.drupal.org/docs/drupal-apis/javascript-api/ajax-forms and tried their Debugging AJAX Callback Functions, which has:

$response = new AjaxResponse();
$debugOut = @Kint::dump($formState); //but this uses dump too, which gives me ajax errors

so dd($formState->getValues()); kind of works. This is what it shows me. So, as you can see, I can now see the variables. It's not the best, but readable.

enter image description here

whereas if I go to [website]/webform?ajax_form=1 , it's not even readable.

enter image description here

I am running out of ideas to be able to debug the variable $formState .

4
  • 4
    Have you tried using xdebug?
    – sonfd
    Commented Aug 25, 2020 at 18:47
  • @sonfd I have not. I'll give it a go Commented Aug 25, 2020 at 20:26
  • @sonfd can you submit your comment as the solution. Just installed Xdebug for phpstorm on XAMP, it's amazing! exactly what I've always needed! Commented Aug 26, 2020 at 19:56
  • Armando Silva - go ahead and add it as an answer and feel free to add any helpful details you can about setting it up.
    – sonfd
    Commented Aug 26, 2020 at 20:53

1 Answer 1

2

@sonfd Xdebug helps print out the values beautifully. No more print_r() or kint() errors.

enter image description here

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.