Drupal Answers Asked by Arbee on November 23, 2021
I’ve been updating one of my Drupal 7 modules to 8 and I’m having an issue with my form alter. I’m adding a validation function and a submit handler. The validation function is getting called just fine, but the submit function is not getting fired. I’m wondering if this means there is a Symfony-style approach that needs to be taken to adding submit handlers to existing forms. I’ve done a lot of searching and I can’t find any examples. Also looked through some work-in-progress D8 modules but I haven’t really found any working examples there either.
Does anyone have any idea what the correct Drupal 8 way to do this is?
To replace/override the existing handler in Drupal 9 this worked for me:
**
* Implements hook_form_FORM_ID_alter()
*/
function MODULE_NAME_form_FORM_ID_alter(&$form, $form_state, $form_id) {
$form['submit']['#submit'][] = 'custom_submit';
}
function custom_submit($form, $form_state) {
Drupal::messenger()->addStatus(t('Submit overriden by custom_submit!'));
}
Answered by Joris Lucius on November 23, 2021
Here's how to attach a custom submit handler to the article node add/edit form in Drupal 8:
<?php
use DrupalCoreFormFormStateInterface;
/**
* Implements hook_form_alter().
*/
function my_module_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Use this to reveal the form id.
//drupal_set_message($form_id);
// Use this with the devel module to inspect the button action(s).
//kint($form['actions']);
switch ($form_id) {
case 'node_article_form': // New article nodes.
case 'node_article_edit_form': // Existing article nodes.
// Attach our custom submit handler.
$form['actions']['publish']['#submit'][] = 'my_module_node_article_form_submit';
break;
}
}
function my_module_node_article_form_submit($form, FormStateInterface $form_state) {
drupal_set_message('Running custom submit handler...');
}
I was not able to successfully append a custom submit handler to $form['#submit']
and have the function fire properly. I had to explicitly attach it to the Save and publish button when creating a new article, and attach it to the Save and keep published button when editing an existing article.
Besides publish
, here are some of the other button actions available:
unpublish
preview
delete
Answered by tyler.frankenstein on November 23, 2021
You can add how many submit handler you want using the following code:
$form['actions']['submit']['#submit'][] = 'mymodule_what_ever_function';
If you want to add a submit handler after the default submit handler, (a submit handler that will be called after the submit callback), you can use the following.
$form['#submit'][1] = test_function;
To remove the submit handler:
unset($form["actions"]['submit']);
Answered by DEVARAJ JOHNSON on November 23, 2021
The behavior is either changed or differs from case to case. With the Node Delete Multiple confirmation form
$form['actions']['submit']['#submit'][] = 'mymodule_upload_enabled_types_submit';
@Clive's code causes Drupal to ignore the main handler. In this case add the handler to the $form['#submit']
section:
$form['#submit'][] = 'mymodule_upload_enabled_types_submit';
Answered by lifecoder on November 23, 2021
According to the example in the documentation it's the same as for Drupal 7:
$form['actions']['submit']['#submit'][] = 'mymodule_upload_enabled_types_submit';
You can find similar (working) logic in
contact_form_user_form_alter()
editor_form_filter_format_form_alter()
locale_form_language_admin_edit_form_alter()
And a few others besides.
Answered by Clive on November 23, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP