Score:0

Status messages don't show

us flag

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.

fr flag
See https://drupal.stackexchange.com/questions/138697/what-function-method-can-i-use-to-redirect-users-to-a-different-page/303653#303653
4uk4 avatar
cn flag
Did you try to `return $response;` and not to send and exit?
Jaypan avatar
de flag
For redirecting from a controller, you can use return `$this->response($route=name);`. Maybe that will help. https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Controller%21ControllerBase.php/function/ControllerBase%3A%3Aredirect/9.3.x
us flag
@anonymous: Thank you that fixes the problem!
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.