5

Using Drupal 8 and trying to get the contact form to redirect to the same page after submitting. By default it seem to redirect to the home page.

I have edited my themename.theme file

function themename_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

  //contact page form
  if ($form_id == 'contact_message_feedback_form') {
    $url = Url::fromRoute('contact.site_page');
    $form_state->setRedirectUrl($url);
  }
}

This throws a bunch of mysql errors. Like so:

Uncaught exception thrown in session handler.

Drupal\Core\Database\DatabaseExceptionWrapper: SQLSTATE[08S01]: Communication link failure: 1153 Got a packet bigger than 'max_allowed_packet' bytes: UPDATE {sessions} SET uid=:db_update_placeholder_0, hostname=:db_update_placeholder_1, session=:db_update_placeholder_2, timestamp=:db_update_placeholder_3 WHERE ( (sid = :db_condition_placeholder_0) ); Array ( [:db_update_placeholder_0] => 1 [:db_update_placeholder_1] => ::1 [:db_update_placeholder_2] => _sf2_attributes|a:1:{s:3:"uid";s:1:"1";}_sf2_flashes|a:0:{}_sf2_meta|a:4:{s:1:"u";i:1454074283;s:1:"c";i:1452243053;s:1:"l";s:7:"2000000";s:1:"s";s:43:"dVseXv540u6wL5GZm_gs6fzuP9qtck-j5A5lexqnaQE";}authorize_page_title|O:48:"Drupal\Core\StringTranslation\TranslatableMarkup":3:{s:9:"*string";s:14:"Update manager";s:12:"*arguments";a:0:{}s:10:"*options";a:0:{}}messages|a:1:{s:6:"status";a:2:{i:0;O:25:"Drupal\Core\Render\Markup":1:{s:9:"*string";s:1020035:"<script class="-kint-js">(function(){if("undefined"===typeof kintInitialized){kintInitialized=1;var e=[],f=-1,g=function(b){var a=window in Drupal\Core\Session\SessionHandler->write() (line 89 of /Users/timmcdonald/Code/waypoint/v3/core/lib/Drupal/Core/Session/SessionHandler.php).

and other errors. Is there something I am missing?

2
  • You cannot set it in the form itself. You have to use a submit function for that. Although altering forms works in themes this example should totally be in a module and not in theme.
    – user21641
    Commented Jan 29, 2016 at 20:27
  • You might be interested to know that the Contact Storage module provides custom redirects on submission. Although be aware of this issue with it: drupal.org/node/2629630
    – Jonathan
    Commented Jan 29, 2016 at 21:07

5 Answers 5

5

Here is what I ended up doing in a custom module if anyone else needs to do this

use Drupal\Core\Url;

function custom_module_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

  //contact page form
  if ($form_id == 'contact_message_feedback_form') {
    $form['actions']['submit']['#submit'][] = 'custom_module_contact_submit';
    dpm($form['actions']['submit']);
  }

}

function custom_module_contact_submit(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
    drupal_set_message('we sent a form woohoo');
    $url = Url::fromRoute('contact.site_page');
    $form_state->setRedirectUrl($url);
}
2
  • dpm will cause an error unless the devel module is enabled. I also left out $form_id from the parameter list since Drupal complained of a missing third argument.
    – pmagunia
    Commented Dec 23, 2017 at 18:21
  • FYI for latecomers to this answer - drupal_set_message has been replaced by Messenger. See here drupal.org/node/2774931
    – Christian
    Commented Dec 9, 2020 at 6:55
4

And to make it even better

/**
 * hook_form_FORM_ID_alter
 * @param $form
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 * @param $form_id
 */
function minimal_form_contact_message_contact_me_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  $form['actions']['submit']['#submit'][] = 'minimal_contact_submit';
}

/**
 * Contact form submit handler
 * @param $form
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 */
function minimal_contact_submit(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
  drupal_set_message('we sent a form woohoo');
  // From URL to URL object
  $url = \Drupal\Core\Url::fromUserInput('/node/16');
  $form_state->setRedirect('contact.site_page');
}

I found it useful, so maybe other people could find it useful too

3
  • 2
    very useful fromUserInput
    – Matoeil
    Commented Oct 20, 2016 at 7:51
  • 1
    I ended up using $form_state->setRedirect('entity.node.canonical', ['node' => '16']); to get the dynamic route Commented Apr 2, 2017 at 15:03
  • wolffer-east answer is the correct one. In Cifren example, where is $url used? Commented May 24, 2017 at 9:49
1

Tim's solution will also work from a theme.

In this case the theme is called 'minimal', and the code below is in the minimal.theme file. I'm calling hook_form_FORM_ID_alter on a form with ID: contact_message_contact_me

Also note the signature on the submit handler, as well as the direct call to redirect with the default site-wide route for the contact form (my form above is set to the default). You don't need to build the Url first.

/**
 * hook_form_FORM_ID_alter
 * @param $form
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 * @param $form_id
 */
function minimal_form_contact_message_contact_me_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  $form['actions']['submit']['#submit'][] = 'minimal_contact_submit';
}

/**
 * Contact form submit handler
 * @param $form
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 */
function minimal_contact_submit(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
  $form_state->setRedirect('contact.site_page');
}
1

To redirect to the same page, whatever it is (if you use contact block module for instance), you can use this in your submit callback:

$form_state->setRedirectUrl(\Drupal\Core\Url::fromRouteMatch(\Drupal::routeMatch()));
1
  • This should be the accepted answer. Commented Sep 13, 2019 at 1:25
0

I created a custom module for my form. In the submit function, I did not set a redirect URL, in which the submit function will not redirect to any page, and therefore show the form page again. This because the form submits to the same page the form is on.

Edit: After I posted this answer I noted that you asked about the Drupal contact form. So naturally this answer is wrong. Always read the brief twice and then two more times.

<?php
/**
 * File: ContactForm.php
 */

namespace Drupal\module_name\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\UrlHelper;

class ContactForm extends FormBase
{
    /**
     * {@inheritDoc}
     */
    public function getFormId() {
        return 'module_name_contact_form';
    }

    /**
     * {@inheritDoc}
     */
    public function buildForm(array $form, FormStateInterface $form_state) {
        // Form 
        return $form;
    }

    /**
     * {@inheritDoc}
     */
    public function validateForm(array &$form, FormStateInterface $form_state) {
        // Form validation
    }

    /**
     * {@inheritDoc}
     */
    public function submitForm(array &$form, FormStateInterface $form_state) {
        drupal_set_message('Form submitted');
    }
}

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.