A
A
Altron83372018-12-05 07:06:53
Drupal
Altron8337, 2018-12-05 07:06:53

How to hide ajax form in hook_form_alter after submit?

Hello, I am changing the login form, making it ajax, everything is fine, but how can I make it so that after a successful submission the form disappears and only a welcome message remains, now both the form and the message remain

function mymodule_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if ($form_id == 'user_login_form') {
    $form['system_messages'] = [
      '#markup' => '<div id="' . Html::getClass($form_id) . '-messages"></div>',
      '#weight' => -100,
    ];
    $form['name']['#attributes'] = ['style' => 'background-color: #0a6fb4'];
    $form['actions']['submit']['#ajax'] = [
      'callback' => 'ajax_callback',
      'event' => 'click',
      'progress' => [
        'type' => 'throbber',
      ],
    ];
    $form['#submit'][] = 'mymodule_form_submit';
  }
}

function ajax_callback(array &$form, FormStateInterface $form_state) {
// код не по теме
}

function mymodule_form_submit(&$form, FormStateInterface $form_state) {
 \Drupal::messenger()
    ->addMessage((t('Привет')));
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
andead, 2018-12-06
@andead

In the shape of:

$form['#prefix'] = '<div id="form-wrapper">';
$form['#suffix'] = '</div>';
$form['actions']['submit']['#ajax']['wrapper'] = 'form-wrapper';

In ajax callback:
return ['#markup' => 'Привет'];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question