Posted by ki on October 10, 2008 at 2:22am
Could someone tell me how I can have two front pages, one for anon and the other for authenticated users?
That is, you see one page before login and see different front page when you log in. I got to work it to redirect authenticated users to front page for authenticated. But if the user tries to see the anon's front page, he would be redirected back.
Another question:
Which module (or something else?) is used to have the side block for Group categories that shows Dojo Tags?
Thanks.
Comments
Greetings: About Front Page
Check out this module (http://drupal.org/project/front).
It could help with half of your stated issue..
I'll think about the other half,
Cheers,
Robert
"Who is honored (deserves Kavod)? The person who honors (gives Kavod to) other people." "Eizehu mechubad? Hamechabed et habriot,"
--Pirkei Avot 4:1
"Code testing will find all the possible errors that you can think of, data testing will find all the errors you don’t. "
--Bioinformatics Zen
Wonderful!
Just what I was looking for! Love the Drupal forums! Thank you!
Here is how I do it.
I would just use the page.tpl.php file as a relay to other page templates based on if the user is logged in or not...
Here is how...
<?php
global $user;
if ($user->uid)
{
include 'page_loggedin.tpl.php';
return;
}
include 'page_default.tpl.php';
return;
?>
Hope this helps.
While your suggestion works
While your suggestion works there is actually a way to do this in a much "cleaner" fashion. Depending on the version you are running, here I'll show 5.x.
In template.php you need to place the following:
<?php/**
* Intercept template variables
*
* @param $hook
* The name of the theme function being executed (name of the .tpl.php file)
* @param $vars
* A copy of the array containing the variables for the hook.
* @return
* The array containing additional variables to merge with $vars.
*/
function _phptemplate_variables($hook, $vars = array()) {
// Get the currently logged in user
global $user, $theme_key;
switch ($hook) {
case 'page':
if ($user->uid) {
$vars['template_file'] = 'page-logged-in';
}
else {
$vars['template_file'] = 'page-anonymous';
}
break;
}
return $vars;
}
?>
If you are using Drupal 6 you will have to use the preprocess hook (related info on the preprocess system: http://api.drupal.org/api/function/theme/6)
Drupal 6.x
<?phpfunction phptemplate_preprocess_page(&$vars, $hook) {
global $user;
if ($user->uid) {
$vars['template_file'] = 'page-logged-in';
}
else {
$vars['template_file'] = 'page-anonymous';
}
}
?>
The above code is a feature of the phptemplate engine that allows you to override any template variable before it goes to the template. The function is called before any template file is loaded and in this case we are catching the case that we are loading the page template file. We then check to see if the user is logged in or not, and depending on that we tell drupal to use our new template file.
For examples of using these preprocess hooks I would recommend looking at the code and comments in the template.php file provided by the zen theme
Remember however, that the actual content that is printed out in the page template file (in the $content variable) will not be any different, this just gives you a way to present two different layouts (maybe you move the regions around or provide some custom message somewhere.
To provide different content to different types of users I would recommend you check out the panels module. You can easily create a new panel page and insert blocks of content in the different 'panes'. Panels allows you to set access control for each individual block of content.
:)
Ultra light weight but maybe less readable... but ya just gotta love that tertiary operator :)
<?phpfunction phptemplate_preprocess_page(&$vars, $hook) {
global $user;
$vars['template_file'] = ($user->uid) ? 'page-logged-in' : 'page-anonymous';
}
?>
(At least I think that's right! :) Could have factored out the 'page-' bit as well, but I think the cost of the string concatenation makes that defunct)
It's worth noting as well that using this method means you don't end up storing html/php in the database, which is what will happen with front.module. You can then use version control and so on for these pages if necessary.
Web Development in Nottingham, UK by Kineta Systems
Web Development in Nottingham, UK by Kineta Systems / Follow me on Twitter! @NikLP
Ternary
not Tertiary ;) Tertiary means third, ternary means having 3.
http://www.economicexpert.com/a/Ternary:operation.html
And generally as long as certain standards are followed(no nesting, short line length, proper spacing) the ternary operator is considered more readable.
Sorry for off-topic.
Great way to create multiple templates
This is a great way to create multiple templates, even split templates off by taxonomy, user, etc...since of course you can use whatever logic you like and presumably any number of possible return values. Very cool, thanks for pointing this out.
Its also worth noting that
Its also worth noting that using the array $vars['template_files'] you can define multiple template files which drupal will then search for in your theme folder. This is how phptemplate works to provide multiple possible theme override templates.
<?php$vars['template_files'][] = 'page-template1';
$vars['template_files'][] = 'page-template2';
?>
In this case drupal looks for page-template1.tpl.php and if not found, will try to use page-template2.tpl.php. The benefit to this is that if you add the code in to the template.php file but don't add the template, you don't get any errors because it will fall back on the default (page.tpl.php, node.tpl.php etc based on which preprocess hook you are overriding.)
Just be aware that there are differences in the two keys and that these options are available to you.
video of $vars['template_files']
Here's a video showing the $vars['template_files'] variable Blake mentions above.
http://gotdrupal.com/videos/unique-pages-by-content-type
Chris Charlton, Author & Drupal Community Leader, Enterprise Level Consultant
I teach you how to build Drupal Themes http://tinyurl.com/theme-drupal and provide add-on software at http://xtnd.us
I think those need to be the
I think those need to be the other way round...
The array in template_files is merged onto the $suggestions array in the engine, so what you want to end up with is:
Array(
[0] => node-story
[1] => node-myfallback
[2] => node-mypreferred
)
So the code in template.php should be:
$vars['template_files'][] = 'node-myfallback';$vars['template_files'][] = 'node-mypreferred';
At least that's the way it's working for me on D5.
Implicit here
is the idea that this stuff belongs in template.php. There is no upside to putting it in page.tpl.php, which only forces an additional include.
Thanks so much!
Thanks so much! This tips work for me.
Best regards.
Yes this is very nice and simple 1 travist
Yes this is very nice and simple 1 travist tnq..
both of the above sound like
both of the above sound like they'd work for frontpage issues.
alternatively: you might also look at Views module. Views allows pretty good control based on permissions/roles. so, setting up two views (Views module) with appropriate permissions you should be able to "redirect" to alternate views depending on status/role. I don't recall off top of my head how well it handles redirects tho. MIGHT be able to also utilize 403 redirects,,, but that's probably too general - and not good form.
as far as the block, I'm pretty sure its just taxonomy. the blockin question might be generated/output by Panels module... but read a bit of taxonomy handbook - I'm sure there's an easy snippet /tutorial on how to get a term list )
--
mike stewart { twitter: @MediaDoneRight | IRC nick: mike stewart }
Thanks all for the valuable
Thanks all for the valuable tips and great insights! Didn't expect this much feedback, not a single comment to throw away.
That's what the Drupal Dojo is all about!
That's what the Dojo is all about! Maybe next time someone googles for "Drupal alternate frontpage" they'll find this thread. :)
http://www.chapterthreellc.com | http://www.outlandishjosh.com
https://pantheon.io | http://www.chapterthree.com | https://www.outlandishjosh.com
Well...
This is kind of what I hoped to achieve when I started the thread about an internal Dojo bookmark system... that people would not chance upon great pages; they would trip over them before they barely started looking!
http://drupal.org/node/205683
Web Development in Nottingham, UK by Kineta Systems
Web Development in Nottingham, UK by Kineta Systems / Follow me on Twitter! @NikLP
Please Extend
Please extend this to an arbitrary number of different types of authenticated users.
Easy peasy
As suggested by blake above, the method for having this based on templates is infinitely extensible based on whatever criteria you like:
<?php$vars['template_files'][] = 'page-template1';
$vars['template_files'][] = 'page-template2';
?>
Could easily be extended to look like:
<?phpglobal $user;
$vars['template_files'][] = 'page-template-'. $user->uid;
?>
Meaning that each user could potentially have his/her own page. That would quickly turn into a tpl.php file administration problem, but you could do it based on role or any other grouping criteria as well.
http://www.chapterthreellc.com | http://www.outlandishjosh.com
https://pantheon.io | http://www.chapterthree.com | https://www.outlandishjosh.com
Another approach implementing hook_init()
In my case, using 'template_files' did not work because two different pages are generated by Panel with different urls. however, I could not figure out a way to insert any php codes into Views or Panels.
Both front pages for anon and auth users are generated by Panel with urls; say, 'anon_home' and 'auth_home' respectively. I set the default front page to be 'anon_home' at /admin/settings/site-information.
Then I created a small module and implemented hook_init() in it. hook_init() is supposed to run before anything else.
<?php
function mymodule_init() {
global $user;
if ($_GET['q'] == 'anon_home' and $user->uid) drupal_goto('auth_home');
}
?>
Bug fix to code.
There was a bug in my code above.
Since $_GET['q'] gets manipulated and drupal bootstrap is used by /update.php, if you try to run /update.php it will get redirected too. So I put another condition to check. Modified code is,
<?php
function mymodule_init() {
global $user;
if ($_GET['q'] == 'anon_home' and $_SERVER['SCRIPT_NAME'] == '/index.php' and $user->uid) drupal_goto('auth_home');
}
?>
I could have dropped checking $_GET and just check $_SERVER such as,
<?php
function mymodule_init() {
global $user;
if (($_SERVER['REQUEST_URI'] == '/' or $_SERVER['REQUEST_URI'] == '/anon_home') and $user->uid) drupal_goto('auth_home');
}
?>
to get the same effect.
Page not found issue
I need for my site several home pages, because the site is divided in separate sections.
I am using the strategy you suggest to choose from several front pages. While you all seem to base the choice about the user, my case is little different: i need to switch according to the path args.
I mean, if path is http://drupal_root/custom-front1 i need to add 'custom-front1.tpl.php' in the template_files array. This is actually working, but i still have a problem: since the path 'custom-front1' is not being registered in the menu_router, i have a "page not found" record for every hit, beside the minor issue regarding pages having the "page not found" title aswell. Anyway this is potentially cascading other issues out of my control.
You have any alternative approach to suggest?
Regards
I forgot about this post. I
I forgot about this post. I did resolve this issue by linking a fake handler to each involved menu entry.
Anyway, this is how i achieved my goal, that i remember being to use several home pages based on the arg(0) in the url (es.: drupal_root/home1 leads to home page 1, drupal_root/home2 leads to home page 2, and so on)
I first check how many url arguments are being passed. If this count==1 i do like this:
<?php$templates = array (
'home-1' => 'page-front-1',
'home-2' => 'page-front-2',
);
?>
where page-front-1 is a template placed in my theme (page-front-1.tpl.php).
Then i check the array for the requested keys:
<?php
if ($args == 1 && array_key_exists(arg(0), $templates)) {
$arg = arg(0);
if (isset($templates[$arg])) $variables['template_files'] = array($templates[$arg]);
return;
}
?>
The above solution leads to lots of "page not found" errors, because drupal does not knows how to resolve the requested path. So i added some fake entry in the menu router itself (hook menu):
<?php$items['home-1'] = array(
'title' => t('Home page 1'),
'description' => '',
'page callback' => 'home_fake',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
'weight' => 0,
);
$items['home-2'] = array(
'title' => t('Home page 2'),
'description' => '',
'page callback' => 'home_fake',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
'weight' => 0,
);
// ...
// Place how many entries you like.
?>
Finally, i wrote the fake handler like this
<?phpfunction home_fake ()
{
return false;
}
?>
Now, probably this is not the cleanest way to do, but its running nice since 1 year. Hope it helps.
Regards
Let's use the KISS principle -- LoginToboggan
Ki,
In my experience, the LoginToboggan module is the way to go (
http://drupal.org/project/logintoboggan).
Among its features is the ability to redirect a user to a specific page upon successful login.
Why reinvent the wheel?
Bob
You write your own front page styles...
Step 1: Copy your page.tpl.php as page_default.tpl.php
Step 2: Create a new logged in page as page_loggedin.tpl.php.
Step 3: Create a new logged in page as page_loggedout.tpl.php.
Step 4: Modify your page.tpl.php to the following code....
<?php
global $user;
if ($user->uid)
{
include 'page_loggedin.tpl.php';
return;
}
else
{
include 'page_loggedout.tpl.php';
return;
}
include 'page_default.tpl.php';
return;
?>
You write your own front page styles...
How to have two front pages...
Step 1: Copy your page.tpl.php as page_default.tpl.php
Step 2: Create a new logged in page as page_loggedin.tpl.php.
Step 3: Create a new logged in page as page_loggedout.tpl.php.
Step 4: Modify your page.tpl.php to the following code....
<?php
global $user;
if ($user->uid)
{
include 'page_loggedin.tpl.php';
return;
}
else
{
include 'page_loggedout.tpl.php';
return;
}
include 'page_default.tpl.php';
return;
?>
You write your own front page styles...
All the methods above are
All the methods above are good. But not the best in my opinion.
The best method is to use the Role Theme Switcher contributed module. You can define with it different themes for different user roles, thus you can set a theme for anonymous user and another one for authenticated users.
In love with drupal and actively studying computer science, majoring in web.