Custom submit handler not work on form alter[D8]

Created on 19 December 2016, over 8 years ago
Updated 12 February 2025, about 2 months ago

Hi Team,

I am currently working on Drupal 8 site in which i have alter node form using hook_form_alter() to add custom submit handler function for node.

Now problem is that my custom submit handler function is not get called on form submission. My code is below.

<?php
/**
 * @file
 * Alter node form
 */
use Drupal\Core\Form\FormStateInterface;

function drupal_to_slack_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $type = \Drupal::config('drupal_to_slack.settings')->get('node_to_notify_on_slack');
  if (isset($type)) {
    $filter = array_filter($type);
    if (count($filter) > 1) {
      foreach ($filter as $filter_val) {
       $actual_form_id[$filter_val] = 'node_' . $filter_val . '_form';
      }
    }
    else {
      $type_var = implode('_node_form,', $filter);
      $actual_form_id = array($type_var . '_node_form' => 'node_'. $type_var . '_form');
    }
    if (in_array($form_id, $actual_form_id)) {
       $form['#validate'][] = 'drupal_to_slack_form_validate';
       $form['#submit'][][] = 'drupal_to_slack_form_submit';
    }
  }
}
/**
 *  Implement custom form submit function
 */

function drupal_to_slack_form_submit(array &$form, FormStateInterface $form_state) {
    foreach ($form_state->getValues() as $key => $value) {
        print_r( $value); // i cant catch my custom element value here T_T why?
    }
exit();    
}

Please let me know what is wrong in this code.

Thanks for help in advance.

🐛 Bug report
Status

Fixed

Component

Code

Created by

🇮🇳India sandipauti Mumbai

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024