0

Trying to figure out how to redirect a user back to referring page after they have completed our feedback form. So a scenario would be if im on page foo/bar and I click on feedback webform link on that page. I then proceed to fill out the webform and then hit submit I will then be directed back to foo/bar page with confirmation message displayed at top of page. Webform module does give me an option to add in a custom confirmation URL and also the ability to add in tokens. However I have had no success with current tokens as none of them redirect me back to referring page.

enter image description here

Would creating a custom token maybe help with resolving this issue or hook_form_alter? Kind of stuck on how best to approach this.

  • The referrer header is not always present, add instead a destination query parameter to the link pointing at the web form, which should override any url you configure. If not, you can also try to use a token, see drupal.stackexchange.com/questions/158003/… – 4k4 Oct 9 at 20:43
  • Thanks, I tried adding destination query parameter to webform link directly and nothing happened. I also tried solutions in link you provided but they did not work. I might just have to look into custom token solution. – Paul H Oct 10 at 0:19
  • The linked question is for D7, but it works for D8 the same way. I've just tested it, configured "URL with message" and "<front>", navigated the browser to the webform with a destination query string contact?destination=admin/people. After submitting I got redirected to the destination url, not the front page, and I could see the message "Your message has been sent." – 4k4 Oct 10 at 6:30
  • Yes that works, however, I need the destination query string to somehow capture the URL the user was on when thy clicked on the feedback link so that after they click on the submit button they get directed back to whatever page they were on orginally. Thanks again for your help on this. – Paul H Oct 10 at 13:08
  • 1
    Thanks for the suggestion! I think I may have found a solution. Would something like this work ok? href="/form/feedback?destination={{ path('<current>') }}". Any security implications with this approach? – Paul H Oct 10 at 14:14
0

You can't rely on the referrer header, because clients can refuse to provide one. For this use case Drupal has implemented a query parameter destination. Configure the webform with a default Confirmation URL, like <front>, and then add the current url as destination query string to links pointing at the webform.

For example add a feedback link to nodes:

use Drupal\Core\Url;

/**
 * Implements hook_preprocess_HOOK() for node templates.
 */
function mymodule_preprocess_node(&$variables) {
  $variables['content']['feedback'] = [
    '#type' => 'link',
    '#title' => t('Feedback'),
    '#url' => new Url(
      'entity.webform.canonical',
      ['webform' => 'feedback'],
      ['query' => ['destination' => Url::fromRoute('<current>')->toString()]]
    ),
    '#cache' => ['contexts' => ['url.path']],
  ];
}

This overrides the configured confirmation URL and returns to the referring page after the webform is submitted.

In a page template you don't need the cache context and you can use this Twig only solution, as @PaulH has suggested:

href="/form/feedback?destination={{ path('<current>') }}"
  • Thanks for posting this and sorry for not posting earlier. – Paul H 2 days ago

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.