Custom submit handler stops working when checking route I'm trying to setup a custom submit handler, so I can change the redirect URL after submitting comments. Edit: I just want to send users back to the page they were viewing to begin with, but it's a webform with a couple of route parameters, so I need to save them somehow.

This works (the function is called and I see the debug messages):

function mymodule_form_comment_form_alter(&amp;$form, &amp;$form_state) {
    $form['actions']['submit']['#submit'][] = '_submit_foo';
    kint($form['actions']);
}

function _submit_foo(&amp;$form, \Drupal\Core\Form\FormStateInterface $form_state) {
    kint('Hello world!');
    \Drupal::logger( 'foo' )-&gt;info('hello world');
    exit;
}


This DOESN'T work - i.e. as soon as I add a check on the route: 

function mymodule_form_comment_form_alter(&amp;$form, &amp;$form_state) {

    $route_name = \Drupal::routeMatch()-&gt;getRouteName();

    if ($route_name == 'entity.webform_submission.canonical') {
        // it's DEFINITELY getting this far!

        kint( 'in conditional' );
        kint( $route_name );
        $form['actions']['submit']['#submit'][] = '_submit_foo';
        kint( $form['actions'] );
    }
}



I know the conditional is being reached, I'm seeing the debug statements.  
_submit_foo() isn't called
user is sent to comment.reply instead


Here's a screenshot of the kint($form['actions']):



Context:


these comments are shown inside a view block (format - show: content, single contextual filter, also generated from URL), which is placed, in block layout,  below webform submissions on the standard entity.webform_submission.canonical route - e.g. URL like  /admin/structure/webform/manage/application/submission/123
there's also code in HOOK_comment_view_alter that adds a 'user' cache context to the view (because each user has their comments filtered, so doing it by role is not enough) - in any case, I've tried disabling that and clearing cache
the comments are in a node that has an entity reference to the webform (in case you're wondering how I've managed to combine comments and webform)
comment previews are disabled, and I assume I can rule out that submit action based on the first piece of code working as designed?
The comment form sets an #action to the comment.reply route, which will rebuild the form and no longer go inside your condition as the route there is differdnt.

Whatever you're trying to do exactly, you need to find a different way to do it.