How to have two front pages

public
Ki - Fri, 2008-10-10 02:22

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.

Greetings: About Front Page

rdsmith@drupal.org - Fri, 2008-10-10 02:37

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

Here is how I do it.

travist's picture
travist - Fri, 2008-10-10 04:04

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...

 

  • 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: Modify your page.tpl.php to the following code....
    <?php
    global $user;

    if (
    $user->uid)
    {
        include
    'page_loggedin.tpl.php';
        return;
    }

    include
    'page_default.tpl.php';
    return;
    ?>
  • And your done!!

Hope this helps.


While your suggestion works

BlakeLucchesi's picture
BlakeLucchesi - Fri, 2008-10-10 06:22

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

<?php
function 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.


:)

NikLP's picture
NikLP - Fri, 2008-10-10 09:42

Ultra light weight but maybe less readable... but ya just gotta love that tertiary operator :)

<?php
function 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


Ternary

tbartels - Fri, 2008-10-10 18:55

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

dwees's picture
dwees - Fri, 2008-10-10 13:02

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

BlakeLucchesi's picture
BlakeLucchesi - Fri, 2008-10-10 17:34

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']

Chris Charlton's picture
Chris Charlton - Fri, 2008-10-10 17:53

Here's a video showing the $vars['template_files'] variable Blake mentions above.

http://gotdrupal.com/videos/unique-pages-by-content-type


I think those need to be the

joachim's picture
joachim - Tue, 2008-11-18 10:12

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

Cary Gordon's picture
Cary Gordon - Fri, 2008-10-24 15:30

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.


both of the above sound like

Mike Stewart's picture
Mike Stewart - Fri, 2008-10-10 06:05

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 )


Thanks all for the valuable

Ki - Fri, 2008-10-10 21:20

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!

joshk's picture
joshk - Fri, 2008-10-10 21:36

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 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


Well...

NikLP's picture
NikLP - Sat, 2008-10-11 07:03

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


Please Extend

dharveymi - Sat, 2008-10-11 10:52

Please extend this to an arbitrary number of different types of authenticated users.

Easy peasy

joshk's picture
joshk - Sat, 2008-10-11 20:11

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:

<?php
global $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


Another approach implementing hook_init()

Ki - Sat, 2008-10-18 00:24

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.

Ki - Tue, 2008-10-21 18:53

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.