2

is there a way to prevent node from being saved? I want to save values form this content type into user profile fields and other profile module fields. Thank you

My code:

/**
 * Implements hook_form_alter().
 *
 * @param $form
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 * @param $form_id
 */
function hospitator_qr_decode_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if ($form_id == 'node_time_tracker_form') {

    foreach (array_keys($form['actions']) as $action) {
      if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
        $form['actions'][$action]['#submit'][] = 'hospitator_qr_decode_form_submit';
      }
    }
  }
}

/**
 * Implements custom form submit. Try to prevent node save.
 *
 * @param $form
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 */
function hospitator_qr_decode_form_submit($form, FormStateInterface $form_state) {
  $form_state->setRedirect('hospitator_user_interface.main_page');

  \Drupal::messenger()->addMessage(t('Form Custom Submited'), 'status');
  //How to don't save the node?

  return;
}

1 Answer 1

3

Did you try to replace the existing submit handlers with your own?

This adds a submit handler:

$form['actions'][$action]['#submit'][] = 'hospitator_qr_decode_form_submit';

This replaces the submit handlers:

$form['actions'][$action]['#submit'] = ['hospitator_qr_decode_form_submit'];
1
  • Thank you. Works like a charm!
    – Alvaro dM
    May 1, 2020 at 16:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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