3

I am using hook function in my module to redirect users accordingly as below.

function mymodule_user_login($account) {
 if (!$account->getLastAccessedTime()) {
  // The user has never logged in before.
   return;
 }
 $response = new RedirectResponse('MyModuleFormUrl');
 $response->send();
}

First time users coming from their corresponding emails are getting redirected to change password page able to set new password and existing users are redirected to form page after successful login. But the problem is how to redirect users after successfully setting their password to my module form. From the code above I thought it should happen automatically but it isn't. Can anyone suggest some ideas.

Edit to the Question: So as suggested below I tried both the ways but still after the new user clicks save button on password reset page(setting his new password), he is not getting redirected to mymodule form page. Below are the codes I tried.

Method 1:

    function MyModule_form_alter(&$form, &$form_state, $form_id){
    if($_SESSION['first_time_login']==TRUE)
    {
       $_SESSION['first_time_login']==FALSE;
       $form_state->setRedirect('url');
    }

Method 2:

    function mymodule_user_form_alter(&$form, 
    \Drupal\Core\Form\FormStateInterface $form_state, $form_id)
    {
       foreach (array_keys($form['actions']) as $action) {
        if (isset($form['actions'][$action]['#type']) && $form['actions'] 
        [$action]['#type'] === 'submit') {
        $form['actions'][$action]['#submit'][] = 'mymodule_form_submit';
         }
       }
    }

    function mymodule_form_submit(array $form, FormStateInterface 
     $form_state){
         $form_state['redirect'] = 'url';
     }

Final function which worked for me:

    function MyModule_form_alter(&$form, &$form_state, $form_id){
      if($form_id == 'user_form')
      {
        foreach (array_keys($form['actions']) as $action) {
           if (isset($form['actions'][$action]['#type']) && $form['actions'] 
           [$action]['#type'] === 'submit')
           {
            $form['actions'][$action]['#submit'][] = 
               '_MyModule_user_profile_submit';
           }
       }
     }

      function _MyModule_user_profile_submit($form, &$form_state)
      {
           $response = new RedirectResponse('url');
           $response->send();
      }     

3 Answers 3

3

It's actually pretty easy, the user module adds user_pass_reset to the form_state all you need to do is check for that value:

function MyModule_form_user_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  if ($form_state->get('user_pass_reset')) {
    $form['actions']['submit']['#submit'][] = '_MyModule_user_profile_submit';
  }
}
2

In your case you also need to alter the user form to add a handler after editing the user pass, e.g.

/**
 * Implements hook_form_FORM_ID_alter().
 */
function MYMODULE_form_user_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id)
{
  foreach (array_keys($form['actions']) as $action) {
    if (isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
      $form['actions'][$action]['#submit'][] = '_mymodule_form_submit';
    }
  }
}

function _mymodule_form_submit(array $form, FormStateInterface $form_state){
  //Add logic for checking is this is first time logged in User
  $form_state->setRedirect('ROUTE_NAME');
}

UPDATE: I updated the code to reflect the correct redirection method. I tested it on a Drupal installation and it works. You should replate ROUTE_NAME with a defined and working route.
The only thing remaining for you is to add the logic to detect if this is the first time this User is logged in.
Hope that helps.

4
  • edited my question..new to drupal... sorry if the question is not detailed enough. Sep 5, 2018 at 20:03
  • @user3324848 I updated my answer, please check it Sep 6, 2018 at 7:40
  • Awesome it worked....thanks a lot...I posted my final code what i had in the function.....Is there any significance for the prefix '_' in '_mymodule_form_submit'. Sep 6, 2018 at 18:27
  • No, it is just a convention I use for private functions in module file. Sep 6, 2018 at 20:50
0

Let the system know that the user is logged in the first time.

function mymodule_user_login($account) {
  if (!$account->getLastAccessedTime()) {
   // The user has never logged in before.
   $_SESSION['first_time_login'] = TRUE;
  }
}

Then using hook_form_alter you can provide additional submit handler for reset pass form and check if first_time_login exists in the session, if so just use $form_state->setRedirect(...) and then remove the value from your session.

Good luck.

1
  • edited my question..new to drupal... sorry if the question is not detailed enough. Sep 5, 2018 at 20:03

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.