0

I've looked at this question, but I want a div to be displayed rather than a Drupal Message

After a user successfully submits the user_register_form, I want to call a jQuery function like this:

function showMessage(){
  $('#message-overlay').fadeIn();
}

This is so I can show users that they have to confirm their email address in order to login.

I tried using hook_form_alter like this to display a custom message, but even this does not work...

function MYTHEME_form_alter( &$form, &$form_state, $form_id ) {
  if ( TRUE === in_array( $form_id, array( 'user_register_form') ) ) {
    $form['#submit'][] = 'submitForm';
  }
}

function submitForm(array &$form, FormStateInterface $form_state) {
  drupal_set_message(t('Form Submitted Successfully'), 'status', TRUE);
}

Please advice!

1 Answer 1

1

I used like below and it works for me:

function MYMODULE_form_alter( &$form, &$form_state, $form_id ) {
  if ( TRUE === in_array( $form_id, array( 'user_register_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'][] = '_message_submit';
        }
      }
  }
}
function _message_submit(array &$form, FormStateInterface $form_state) {
  $values = $form_state->getValues();
  drupal_set_message(t('The user @name account has been created successfully.', ['@name' => $values['name']]));
}
3
  • Works perfectly, thank you very much!! One thing though ... how can I delete the standard message? It now displays A welcome message with further instructions has been sent to your e-mail address. along with your created message
    – Rubenxfd
    Commented Oct 10, 2017 at 12:14
  • Messages will be in SESSION. So check the session for messages and unset that.
    – Ajay Reddy
    Commented Oct 10, 2017 at 12:20
  • Could you maybe be more specific? I am quite new to Drupal =/
    – Rubenxfd
    Commented Oct 10, 2017 at 12:24

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.