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();
}