0

In Drupal 8 I would like to redirect users to the frontpage and display a status message after submitting an article?

How can I do that? form_alter and custom submit handler?

1 Answer 1

1

Yes, form_alter and a custom submit handler was the solution.

function _custom_admin_form_node_article_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  foreach (array_keys($form['actions']) as $action) {
    if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
      $form['actions'][$action]['#submit'][] = '_custom_admin_article_form_submit';
    }
  }
}

function _custom_admin_article_form_submit($form, FormStateInterface $form_state) {
  drupal_set_message(t('Thank you for submitting an article.'));

  $url = Url::fromRoute('<front>');
  $form_state->setRedirectUrl($url);
}
1
  • Note that just adding a drupal_set_message() duplicates the one from core and you have two messages. Getting rid of that requires messing with where they are stored ($_SESSION). Also, you can do $form_state->setRedirect('<front>'), has the same arguments as fromRoute().
    – Berdir
    Commented May 18, 2016 at 18:14

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.