3

I want to make some changes to the "Content" page. The ID of the form is views_form_content_page_1, so I made a quick test (after drush cr of course):

function my_module_form_views_form_content_page_1_alter(&$form, FormStateInterface $form_state) {
  kint($form);
}

When I go to the page, the $form is being printed twice by the devel. I checked, all of the array items are the same. If I do kint('cat');, then it will print cat two times.

Why is this happening? Of course I could make it work, that my action will run only once (eventho drupal tries to run it twice) but I would like to rather solve this one, and understand if it's a bug, and why it is happening.

0

It has to do with the fact that Drupal redirects after the form submit. So after a form submit you will make 2 requests: the form submit and the redirect. Hence the 2 messages in your hook_form_alter().

You can disable this redirect by calling $form_state->disableRedirect() in your form submit handler. By disabling the redirect the form values will be preserved in the form. The redirect is in place to 'clean' the form inputs after submission.

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.