2
votes

I've created a Drupal 7 custom login page and altered the layout using a user-login.tpl.php template file. Everything was golden until I realized that the most of the forced logins were redirecting to /user not /user/login for anonymous users. I don't want to modify the user.tpl.php template because that will obviously change the layout of my profile pages.

I searched about a bit and found a bit of code for the template.php file to check to see if the user is logged in and if not redirect to the user-login.tpl.php file.

<?php
function mytheme_preprocess_page(&$vars) {
  // Check to see that the user is not logged in and that we're on the correct page.
  if ($vars['user']->uid == 0 && arg(0) == 'user' && (arg(1) == '' || arg(1) ==    'login')) {
// Add a custom template-file.
array_unshift($vars['template_files'], 'page-login');
// Add body classes for easier css.
$vars['body_classes'] .= ' logged';
 }
}
?>

Stripped out the php tags obviously and changing the Mytheme to my theme name, it still doesn't seem to work. I also removed the body classes add on but no go. I get this as an error with the body classes added.

Warning: array_unshift() expects parameter 1 to be array, null given in framework_preprocess_page() (line 123 of /Applications/MAMP/htdocs/mysite/sites/all/themes/framework/template.php).
Notice: Undefined index: body_classes in framework_preprocess_page() (line 125 of /Applications/MAMP/htdocs/mysite/sites/all/themes/framework/template.php).

and if removed

Warning: array_unshift() expects parameter 1 to be array, null given in framework_preprocess_page() (line 123 of /Applications/MAMP/htdocs/mysite/sites/all/themes/framework/template.php).

I also tried a basic redirect

<?php
function YOURTHEME_preprocess_page(&$vars) {
  // Check to see that the user is not logged in and that we're on the correct page.
 if ($vars['user']->uid == 0 && arg(0) == 'user' && (arg(1) == '' || arg(1) == 'login'))     {
drupal_goto("user/login");
  }
}
?>

but the function would hang the page

The page isn't redirecting properly

Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

This problem can sometimes be caused by disabling or refusing to accept cookies.

Switching out the "user/login" path for another page worked, just not the /login path.

Any ideas? I'm a php newb so simple code/explanations are best.

1
Just to be sure, does the page hang before or after you submit the login form?T.A.C. Commandeur

1 Answers

0
votes

You've got an infinite redirect-loop. You redirect the user to page: yourwebsite.com/user/login when the currently requested page is: yourwebsite.com/user/login This is caused by your if statement.

This part arg(0) == 'user' && (arg(1) == '' || arg(1) == 'login') is the check for URL yourwebsite.com/user/login.

I'm asuming you want an explanation on why the statement always returns true. For the logic of the arg() function, it works as follows: yourwebsite.com/arg(0)/arg(1)/arg(2) and so on.