0

In a custom form, I have this Ajax submit:

  public function ajaxSubmit(array &$form, FormStateInterface $form_state) {

    $response = new AjaxResponse();
    if ($form_state->hasAnyErrors()) {
      $messages = Drupal::messenger()->deleteAll();
      $messages = [
        '#theme' => 'status_messages',
        '#message_list' => $messages,
      ];
      $response->addCommand(new ReplaceCommand('#messages', ''));
      $response->addCommand(new AppendCommand('.region-highlighted', $messages));
    }
    else {

      some code...

      // Completion message -----------------------------------------------------
      if ($this->currentUser()->isAnonymous()) {
        $str = $this->t('membership request');
        $sMessage = $this->t('Your @str has been registered.<BR>It will be effective after reception of your payment.', ['@str' => $str]);
        Drupal::messenger()->addMessage($sMessage);
        $sMessage = $this->t('A confimation email has been sent.');
        Drupal::messenger()->addMessage($sMessage);
      }
      else {
        switch ($form_state->getStorage()['status']) {
          case 1:
            $sMessage = $this->t('Your wish has been recorded.');
            break;
          case 3:
            $str = $this->t('membership renewal request');
            $sMessage = $this->t('Your @str has been registered.<BR>It will be effective after reception of your payment.', ['@str' => $str]);
            break;
          default:
            $sMessage = $this->t('Renew membership: Unexpected case.');
        }
        Drupal::messenger()->addMessage($sMessage);
      }

      $response->addCommand(new \Drupal\Core\Ajax\RedirectCommand(Url::fromRoute('<front>')
        ->toString()));
    }
    return $response;
  }

}

When the code is executed by a logged in user, it works fine: user is redirected to front page and the message (for example 'Your wish has been recorded.') is displayed.
When the user is anonymous, redirection works but no message!
What is wrong with my code?

Update: Adding $form['#attached']['library'][] = 'core/drupal.ajax'; at the beginning of buildForm() didn't work. So I tried to add it at the end of buildForm() and I realized that I already have $form['#attached']['library'][] = 'association/membership'; for some javascript.
So I updated my library to:

LoM:
  version: 1.x
  header: true
  css:
    theme:
      css/ahover.css: {}
      css/datatable.css: {}
  js:
    js/LoM.js: {}
  dependencies:
    - core/jquery
    - shared/shared

LoP:
  version: 1.x
  header: true
  css:
    theme:
      css/ahover.css: {}
      css/datatable.css: {}
  js:
    js/LoP.js: {}
  dependencies:
    - core/jquery
    - shared/shared

membership:
  version: 1.x
  js:
    js/membership.js: {}
  dependencies:
    - core/drupal.ajax

But it still doesn't work! Any idea what I am doing wrong?

15
  • That usually happens if core/drupal.ajax has not been attached.
    – leymannx
    Nov 3, 2023 at 13:43
  • How do I do that? Adding $form['#attached']['library'][] = 'core/drupal.ajax';at the beginning of public function buildForm(array $form, FormStateInterface $form_state)?
    – gbmapo
    Nov 3, 2023 at 16:04
  • By the way, it doesn't work. Any idea?
    – gbmapo
    Nov 3, 2023 at 16:11
  • I tried differently. See my updated question...
    – gbmapo
    Nov 3, 2023 at 16:33
  • 1
    Not here, add cache metadata when rendering the entity. I'm voting to close the question because it was solved in a way that doesn't help others. Cache metadata is a different topic.
    – 4uk4
    Nov 5, 2023 at 9:27

1 Answer 1

0

It was drupal_flush_all_caches() that was the reason of message not showing. Once removed, everything works as expected.

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.