How can we add a custom submit handler?

Created on 15 January 2023, about 2 years ago

Can we add a custom submit handler in the FormAlter plugin? I can't figure out how to create the function in the plugin. Same for afterbuild etc. Is this possible with this module?

💬 Support request
Status

Active

Version

1.5

Component

Code

Created by

🇧🇪Belgium rgeerolf Belgium 🇧🇪🇪🇺

Live updates comments and jobs are added and updated live.
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.

  • Status changed to Closed: works as designed about 1 year ago
  • 🇳🇴Norway farald

    You can also use a static method inside the same plugin class:

        $form['delete_queue'] = [
          '#type' => 'submit',
          '#value' => $this->t('Delete queue'),
          '#submit' => ['\Drupal\mymodule\Plugin\Formalter\UserFormAlter::deleteQueue'],
          '#button_type' => 'primary',
        ];
    

    And then add a static:

      public static function deleteQueue(array &$form, FormStateInterface $form_state) {
        \Drupal::messenger()->addMessage(t("Deleted @queue successfully."));
      }
    
Production build 0.71.5 2024