How do I redirect after saving the user account with $user->save()?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite
1












How can I redirect users in a custom module after saving a user account with $user->save()? It always goes to the user profile after saving and I want to redirect to a custom page.



function user_update_redirect(&$form, $form_state) 
$value1 = $form_state->getValue('panel');
$value2 = $form_state->getValue('type_of_user');
$storage = Drupal::service('entity_type.manager')->getStorage('taxonomy_term');
$user = Drupal::currentUser();
$user = user_load($user->id());
$user->set('field_panel', $value1);
$user->set('field_type_of_user', $value2);
kint($user);
$user->save();










share|improve this question





























    up vote
    1
    down vote

    favorite
    1












    How can I redirect users in a custom module after saving a user account with $user->save()? It always goes to the user profile after saving and I want to redirect to a custom page.



    function user_update_redirect(&$form, $form_state) 
    $value1 = $form_state->getValue('panel');
    $value2 = $form_state->getValue('type_of_user');
    $storage = Drupal::service('entity_type.manager')->getStorage('taxonomy_term');
    $user = Drupal::currentUser();
    $user = user_load($user->id());
    $user->set('field_panel', $value1);
    $user->set('field_type_of_user', $value2);
    kint($user);
    $user->save();










    share|improve this question

























      up vote
      1
      down vote

      favorite
      1









      up vote
      1
      down vote

      favorite
      1






      1





      How can I redirect users in a custom module after saving a user account with $user->save()? It always goes to the user profile after saving and I want to redirect to a custom page.



      function user_update_redirect(&$form, $form_state) 
      $value1 = $form_state->getValue('panel');
      $value2 = $form_state->getValue('type_of_user');
      $storage = Drupal::service('entity_type.manager')->getStorage('taxonomy_term');
      $user = Drupal::currentUser();
      $user = user_load($user->id());
      $user->set('field_panel', $value1);
      $user->set('field_type_of_user', $value2);
      kint($user);
      $user->save();










      share|improve this question















      How can I redirect users in a custom module after saving a user account with $user->save()? It always goes to the user profile after saving and I want to redirect to a custom page.



      function user_update_redirect(&$form, $form_state) 
      $value1 = $form_state->getValue('panel');
      $value2 = $form_state->getValue('type_of_user');
      $storage = Drupal::service('entity_type.manager')->getStorage('taxonomy_term');
      $user = Drupal::currentUser();
      $user = user_load($user->id());
      $user->set('field_panel', $value1);
      $user->set('field_type_of_user', $value2);
      kint($user);
      $user->save();







      8 users redirect






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 1 hour ago









      kiamlaluno♦

      78.6k9125243




      78.6k9125243










      asked 1 hour ago









      Ștefan Roman

      83




      83




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          2
          down vote













          I suppose you want to remain on your site when redirecting.
          I don't recommend hardcoded URL's anywhere else but in route.yml files.
          Call the right service and don't forget to use dependency injection in your services and don't use a trait or directly call it from the container.



           private function redirect(string $route) 
          $path = $this->container->get('url_generator')->getPathFromRoute($route);
          return new RedirectResponse($path);






          share|improve this answer



























            up vote
            1
            down vote













            After $user->save(); add



             $url = '/some/path';
            $response = new SymfonyComponentHttpFoundationRedirectResponse($url);
            $response->send();





            share|improve this answer



























              up vote
              1
              down vote













              $form_state->setRedirectUrl(Url::fromUserInput($redirect_path));






              share|improve this answer








              New contributor




              PHP is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.

















              • Welcome to Drupal Answer! Thank you for your answer. Please try to elaborate it a bit. Why this snippet, how to use it, in what function, what is $redirect_path? One-line answers don't provide that much of a value for future readers. Thanks! And keep up the good work.
                – leymannx
                2 mins ago











              Your Answer







              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "220"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              convertImagesToLinks: false,
              noModals: false,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













               

              draft saved


              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdrupal.stackexchange.com%2fquestions%2f270453%2fhow-do-i-redirect-after-saving-the-user-account-with-user-save%23new-answer', 'question_page');

              );

              Post as a guest






























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              2
              down vote













              I suppose you want to remain on your site when redirecting.
              I don't recommend hardcoded URL's anywhere else but in route.yml files.
              Call the right service and don't forget to use dependency injection in your services and don't use a trait or directly call it from the container.



               private function redirect(string $route) 
              $path = $this->container->get('url_generator')->getPathFromRoute($route);
              return new RedirectResponse($path);






              share|improve this answer
























                up vote
                2
                down vote













                I suppose you want to remain on your site when redirecting.
                I don't recommend hardcoded URL's anywhere else but in route.yml files.
                Call the right service and don't forget to use dependency injection in your services and don't use a trait or directly call it from the container.



                 private function redirect(string $route) 
                $path = $this->container->get('url_generator')->getPathFromRoute($route);
                return new RedirectResponse($path);






                share|improve this answer






















                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  I suppose you want to remain on your site when redirecting.
                  I don't recommend hardcoded URL's anywhere else but in route.yml files.
                  Call the right service and don't forget to use dependency injection in your services and don't use a trait or directly call it from the container.



                   private function redirect(string $route) 
                  $path = $this->container->get('url_generator')->getPathFromRoute($route);
                  return new RedirectResponse($path);






                  share|improve this answer












                  I suppose you want to remain on your site when redirecting.
                  I don't recommend hardcoded URL's anywhere else but in route.yml files.
                  Call the right service and don't forget to use dependency injection in your services and don't use a trait or directly call it from the container.



                   private function redirect(string $route) 
                  $path = $this->container->get('url_generator')->getPathFromRoute($route);
                  return new RedirectResponse($path);







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 33 mins ago









                  ssibal

                  1,739518




                  1,739518






















                      up vote
                      1
                      down vote













                      After $user->save(); add



                       $url = '/some/path';
                      $response = new SymfonyComponentHttpFoundationRedirectResponse($url);
                      $response->send();





                      share|improve this answer
























                        up vote
                        1
                        down vote













                        After $user->save(); add



                         $url = '/some/path';
                        $response = new SymfonyComponentHttpFoundationRedirectResponse($url);
                        $response->send();





                        share|improve this answer






















                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          After $user->save(); add



                           $url = '/some/path';
                          $response = new SymfonyComponentHttpFoundationRedirectResponse($url);
                          $response->send();





                          share|improve this answer












                          After $user->save(); add



                           $url = '/some/path';
                          $response = new SymfonyComponentHttpFoundationRedirectResponse($url);
                          $response->send();






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 1 hour ago









                          No Sssweat

                          19.1k73161




                          19.1k73161




















                              up vote
                              1
                              down vote













                              $form_state->setRedirectUrl(Url::fromUserInput($redirect_path));






                              share|improve this answer








                              New contributor




                              PHP is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                              Check out our Code of Conduct.

















                              • Welcome to Drupal Answer! Thank you for your answer. Please try to elaborate it a bit. Why this snippet, how to use it, in what function, what is $redirect_path? One-line answers don't provide that much of a value for future readers. Thanks! And keep up the good work.
                                – leymannx
                                2 mins ago















                              up vote
                              1
                              down vote













                              $form_state->setRedirectUrl(Url::fromUserInput($redirect_path));






                              share|improve this answer








                              New contributor




                              PHP is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                              Check out our Code of Conduct.

















                              • Welcome to Drupal Answer! Thank you for your answer. Please try to elaborate it a bit. Why this snippet, how to use it, in what function, what is $redirect_path? One-line answers don't provide that much of a value for future readers. Thanks! And keep up the good work.
                                – leymannx
                                2 mins ago













                              up vote
                              1
                              down vote










                              up vote
                              1
                              down vote









                              $form_state->setRedirectUrl(Url::fromUserInput($redirect_path));






                              share|improve this answer








                              New contributor




                              PHP is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                              Check out our Code of Conduct.









                              $form_state->setRedirectUrl(Url::fromUserInput($redirect_path));







                              share|improve this answer








                              New contributor




                              PHP is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                              Check out our Code of Conduct.









                              share|improve this answer



                              share|improve this answer






                              New contributor




                              PHP is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                              Check out our Code of Conduct.









                              answered 13 mins ago









                              PHP

                              112




                              112




                              New contributor




                              PHP is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                              Check out our Code of Conduct.





                              New contributor





                              PHP is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                              Check out our Code of Conduct.






                              PHP is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                              Check out our Code of Conduct.











                              • Welcome to Drupal Answer! Thank you for your answer. Please try to elaborate it a bit. Why this snippet, how to use it, in what function, what is $redirect_path? One-line answers don't provide that much of a value for future readers. Thanks! And keep up the good work.
                                – leymannx
                                2 mins ago

















                              • Welcome to Drupal Answer! Thank you for your answer. Please try to elaborate it a bit. Why this snippet, how to use it, in what function, what is $redirect_path? One-line answers don't provide that much of a value for future readers. Thanks! And keep up the good work.
                                – leymannx
                                2 mins ago
















                              Welcome to Drupal Answer! Thank you for your answer. Please try to elaborate it a bit. Why this snippet, how to use it, in what function, what is $redirect_path? One-line answers don't provide that much of a value for future readers. Thanks! And keep up the good work.
                              – leymannx
                              2 mins ago





                              Welcome to Drupal Answer! Thank you for your answer. Please try to elaborate it a bit. Why this snippet, how to use it, in what function, what is $redirect_path? One-line answers don't provide that much of a value for future readers. Thanks! And keep up the good work.
                              – leymannx
                              2 mins ago


















                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdrupal.stackexchange.com%2fquestions%2f270453%2fhow-do-i-redirect-after-saving-the-user-account-with-user-save%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Comments

                              Popular posts from this blog

                              What does second last employer means? [closed]

                              List of Gilmore Girls characters

                              Confectionery