1

I noticed that when I login I get redirected to localhost/mysite/user/1 and not localhost/mysite or localhost/mysite/homepage. Why is this and what can I do about to make the user go to the homepage after logging in ?

1
  • Why is this this is just simply the default behaviour. – No Sssweat Feb 1 '17 at 11:09
0

It is a default option. But you can redirect user to any page by using hook_form_FORM_ID_alter Example:

 /*
* Implements hook_form_FORM_ID_alter
*/

     function YOUR_MODULE_OR_THEME_NAME_form_user_login_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
      // Alter login form and add own custom submit handler.
      array_push($form['#submit'], '_custom_user_login_form_submit');
    }



 /**
 * Custom submit handler for login form.
 */

    function _custom_user_login_form_submit($form, \Drupal\Core\Form\FormStateInterface $form_state) {
        $parameter = 'some_value';
        // Set redirect to login form.
      $form_state->setRedirect('custom_page', ['parameter' => $parameter]);
    }

For the front page default front page name is view.frontpage.page_1. So it should be

 function _custom_user_login_form_submit($form, \Drupal\Core\Form\FormStateInterface $form_state) {

          $form_state->setRedirect('view.frontpage.page_1');
        }
3
  • If I understand correctly I have to add the code written above in the form.api.php in \core\lib\Drupal\Core\Form ? – Blomkfist174 Feb 1 '17 at 11:52
  • You have to add this code to your theme_name.theme file of to your_module_name.module file and clear cache. Here YOUR_MODULE_OR_THEME_NAME is a name of your theme or module. – Andrew Nim Feb 1 '17 at 12:03
  • You have to add this code to your theme_name.theme file OR to your_module_name.module file and clear cache. Here YOUR_MODULE_OR_THEME_NAME is a name of your theme or module. – Andrew Nim Feb 1 '17 at 12:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.