Skip to content

Instantly share code, notes, and snippets.

@v1talii-dev
Created May 17, 2018 06:54
Show Gist options
  • Save v1talii-dev/353e258e9dbc8814e9041b8aea02bfe8 to your computer and use it in GitHub Desktop.
Save v1talii-dev/353e258e9dbc8814e9041b8aea02bfe8 to your computer and use it in GitHub Desktop.
D8: Add a custom submission handler to a form
<?php
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\HttpFoundation\Request;
function MYMODULE_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if ($form_id == 'node_trends_form' || $form_id == 'node_trends_edit_form') {
foreach (array_keys($form['actions']) as $action) {
if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
$form['actions']['submit']['#submit'][] = 'mymodule_form_submit';
}
}
}
}
function mymodule_form_submit(array $form, FormStateInterface $form_state){
//die("why won't this execute? :(");
drupal_set_message("Why won't this message show?");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment