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 framework_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).Is this not formatted properly for Drupal 7?
Comments
Does a direct redirect work?
Does a direct redirect work? i.e.
<?phpfunction 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");
}
}
?>
drupalshrek
Maintainer of the VChess module visible in use at chess website Chesspos.com
Hmm, sort of.Your code loads
Hmm, sort of.
Your code loads the proper page in the url, but Firefox throws up an error on redirect
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.
Doesn't seem to like the idea of going to the user login form. Switching out "user/login" for another page on the site works fine.
Can you not redirect using
Can you not redirect using the template to anything dealing with the login form?
Not Not Logged in
Tried using straight css on the body class not-logged in but this, also, changed my user pages for those visiting user pages when not logged in. Is there a solution to my original question?
A user-friendly way of changing user.tpl.php
Maybe just change the normal user.tpl.php but with a big condition around the logged in/not logged in user?
i.e.
<?php if ($logged_in) { ?>
<p>Here is all my normal user template stuff</p>
<?php }
else { ?>
<p>Here is my layout specific for users not logged in </p>
<?php } ?>
drupalshrek
Maintainer of the VChess module visible in use at chess website Chesspos.com
This sounded promising, but
This sounded promising, but it seems /user and users/* use the same template for layout.
Looking a little deeper it seems as though I have a couple of different "Login" links; one for comments that relinks to the comment form for the node, one general login and one for my Plus +1 implementation- both of which link to /user. Maybe the redirect problem lies there. Going to investigate a bit more....
Okay I surrender, how do you
Okay I surrender, how do you create a redirect that works to the "user/login" page from "user"?
Solution I think
Okay, this seems to work
<?phpfunction 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) == '' ) {
drupal_goto("user/login");
}
}
?>
Avoiding (logout) loop
This variation avoids a possible loop (e.g. with "user/logout"):
<?phpfunction 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) == '' ) {
header('Location: [paste the http-link to the preferred page here, without square brackets]'); /* Redirect browser */
exit; /* Make sure that code below does not get executed when we redirect. */
}
}
?>
(Source)
I did something like this
I did something like this with role theme switcher and sending all un-authenticated users to a given theme which had no menu and only a login block on the homepage
C
That's what I like about
That's what I like about Drupal, many paths to the same summit