I have a controller that manages a password reset form submit. Everything works fine, except for setting messages. I've tried to alter the message in the .module file too with no luck. It worked on Drupal 8, but after migrating to drupal 9 it doesn't work anymore. Here is the code:
<?php
namespace Drupal\custom_form_save\Controller;
use Symfony\Component\HttpFoundation\Response;
use \Drupal\Core\Session\AccountProxyInterface;
use \Drupal\user\UserInterface;
use Drupal\Core\Image\ImageFactory;
use \Drupal\user\Entity\User;
use \Drupal\node\Entity\Node;
use \Drupal\file\Entity\File;
use \Drupal\Core\Entity\EntityManagerInterface;
use \Drupal\Core\Entity\EntityInterface;
use Drupal\comment\Entity\Comment;
use Drupal\Core\Controller\ControllerBase;
use Drupal\block\Entity\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\HttpFoundation\Request;
use Drupal\node\NodeInterface;
use Drupal\image\Entity\ImageStyle;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\user\PrivateTempStoreFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\custom_form_save\Controller\EmailsController;
use Drupal\content_manager\Controller\StaticController;
use Drupal\Core\Messenger\MessengerInterface;
class ResetPasswordSendController extends ControllerBase {
public function resetpasswordsend(){
$static_functions = new StaticController();
$routes = $static_functions->urlmanagerwebsite();
$session = \Drupal::service('session');
if (!$session->isStarted()) {
$session->migrate();
}
$host = \Drupal::request()->getSchemeAndHttpHost();
$name = $_POST['email'];
$langcode = \Drupal::languageManager()->getCurrentLanguage()->getId();
$users = \Drupal::entityTypeManager()->getStorage('user')->loadByProperties(array('mail' => $name));
if (!empty($users)) {
$account = reset($users);
$mail = _user_mail_notify('password_reset', $account, $langcode);
\Drupal::messenger()->addMessage('Wir haben eine Anleitung zum Zurücksetzen des Passworts an deine registrierte E-Mail-Adresse gesendet.'); //NOT WORKING
$response = new RedirectResponse($host . $routes['resetpassword']);
$response->send();
exit;
}
$addmessage = $name . ' wird nicht als E-Mail-Adresse erkannt.';
$this->messenger()->addMessage($addmessage); //NOT WORKING
$session->set('messagescustom', $addmessage); //NOT WORKING
\Drupal::messenger()->addMessage('Die eingegebenen E-Mail Adresse stimmen nicht überein.'); //NOT WORKING
$response = new RedirectResponse($host . $routes['resetpassword']);
$response->send();
exit;
}
}
The theme should not be the problem, because on every other page messages are working.