1

i'm using Drupal 8. I would like to stop the user to submit a node when he tap save, and show some message. How can I do it programmatically?

Thanks!

1

You can hide the form submit button using hook_form_alter() and show a link Save and Publish on form. And include a jQuery snippet to show message, which will be triggered when user clicks on a link Save and Publish.

1

If you want to prevent form submission and show message, you can add form validation to your form (using hook_form_FORM_ID_alter):

function MYMODULE_form_node_MYNODETYPE_form_alter(&$form, &$form_state, $form_id) {
  $form['#validate'][] = '_node_MYNODETYPE_form_validate';
}

function _node_MYNODETYPE_form_validate($form, $form_state) {  
  // add some logic to show or not show message here
  $form_state->setErrorByName('field_MYFIELD', 'Message to show by my field');  
}
0

To avoid saving the results, you must remove (or override) the save function from the form. Try this:

function custom_module_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id)
{    
        $form['actions']['submit']['#submit'][0] = "_custom_submit";
        unset($form['actions']['submit']['#submit'][1]); //Avoid save results    
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.