Login to view full article (anonymous users see teasers)

wOOge - March 2, 2007 - 22:30

Hey all,

I was wondering if anyone knows of a way I can setup my Drupal site so that anonymous visitors can view teasers on the site, but have to click a "Login to read the rest of this article" link to view the whole article? - or when they click on the article itself, it asks them to log in, then sends them to the article after they're in.

Right now I post teasers in a separate category that has permissions allowing all to view it - then I post a copy of the full story in another category for logged in users to view. I'm looking for a way to get around having to post these (sorta) duplicate pages :(

me too

sampelo - March 2, 2007 - 22:45

.

I don't know if it's the

coreyp_1 - March 3, 2007 - 00:25

I don't know if it's the best way, but you can use Contemplate to return a different body text for anonymous users. It wasn't designed for this use, but it works well.

All that you do is, for the body area, use this code:

<?php
global $user;

if (
$user->uid) {
 
//user is logged in, print normal node body

}
else {
 
// user is not logged in, print login message

}
?>

You'll have to fill in the missing pieces, of course. I don't remember how the variables are accessed in Contemplate (it's been a while since I used it), but I know it can be done. In fact, I believe that the available variables are shown in a block just to the right of the code input area.

Hope that helps.

- Corey

I'll try that out..

wOOge - March 3, 2007 - 04:28

Hmm, this looks promising - I'll def. give it a shot - thanks Corey.

--
wOOge | axonz.com

Did you manage to find a solution?

polar-bear - April 9, 2007 - 09:11

I am interested in this as well. Did you manage to find the solution?

Solution coming...

wOOge - April 17, 2007 - 14:01

I came up with a working solution to this - I'll try to post the code today.

--
wOOge | axonz.com

Premium Module

polar-bear - April 18, 2007 - 07:26

I found that Premium Modules (http://drupal.org/project/premium) will probably do the task but there is no version for Drupal 5.1 and there does not seem to be active development on this.

thanks for the find, we are

gdtechindia - April 18, 2007 - 07:32

thanks for the find, we are using 4.7 and this works for us :D

I'm looking for something

Irrow - April 18, 2007 - 16:35

I'm looking for something similar as well, and while the contemplate solution brings it a step closer its not quite what I'm looking for..

I'd like everyone to be able to view the teasers of a custom node type I created called Lesson, and when they click on it they will see the teaser and a brief passage promoting membership along with a link to sign up. The 'Lesson' type is part of a private organic group (that I use for courses we run) that paid members have access to.

So if you find a similar solution please post it, Cheers :)

---
http://www.irrow.com/

Here it is...

wOOge - May 4, 2007 - 17:44

Ok, it's a little late, but here it is.
This isn't a tutorial, its an example of what I've done - you'll have to create your own categories, and roles to make this your own - but the code all works.

I'm running 4.7.
I have roles setup for Authors, and for subscribers. I also have different categories assigned as feeds that users can "subscribe" to. This script shows the page while in "preview" mode, like on lists and the home page, then when displaying the full page, it checks that you're a subscriber and acts accordingly.

This also requires that you use the -- drupal break -- feature to break the story. I'm not sure if it works if you use the automatic teaser feature.

The following code goes into your node.tpl.php and replaces the code that shows the page's content.

Hope this helps others out there!

/* Setup our environment */
$pagecontent = '<div class="submitted">' . $date . " by ". $authorship . '</div>' . '<div class="nodecontent">' . $content . '</div><div class="node-options">' . theme('links', $node->links) . '</div>';
$nid = $node->nid;
$taxonomy_term = taxonomy_node_get_terms($nid);
$taxonomy_id = array_keys($taxonomy_term);
/* Set the isAuthor flag - on my site I have authors that I grant all access to. */
if (array_search("Author-Staff Writer",$user->roles) == "") { $isAuthor = FALSE; } else { $isAuthor = TRUE;}


if (arg(0)!="node") {
/* if the page is being displayed in any other format that on it's own, show conent (in this case the teaser) */
print $pagecontent;
} else {
if (
/*
* list of items that you *want* visible to everyone logged-in or not
* The numbers used here are category IDs.
*/
$isAuthor != FALSE ||
current($taxonomy_id) =="4" /* About us Pages */ ||
current($taxonomy_id) =="5" /* News Pages */ ||
current($taxonomy_id) =="6" /* Polls */ ||
$user->uid!="0" /* is not the main site Admin */
) {
print $pagecontent;
} else {

if ($user->uid=="0") {
/* User not logged in - show a message */
echo "<h2>Please register or log-in to view this posting<h2>";
include($error_message);
} else {
/*
* Now we made usre they are logged-in, we check if they are a subscriber to the article being shown
* This uses the drupal user roles to determine if they are subscribers
*/
switch (current($taxonomy_id)) {
case '1':
if (array_search('Subscriber (Monthly)', $user->roles)) { $authorized = TRUE; } else { $authorized = FALSE;}
break;
case '2':
if (array_search('Subscriber (Weekly)', $user->roles)) { $authorized = TRUE; } else { $authorized = FALSE;}
break;
case '3':
if (array_search('Subscriber (Digest)', $user->roles)) { $authorized = TRUE; } else { $authorized = FALSE;}
break;
}
if ($authorized==FALSE) {
/* They are logged in, but do not subscribe to this particular feed, let them know */
echo "<h2>You are logged in, but do not subscribe to this feed. Please contact us to become subscribed to this feed.<h2>";
} else {
/* Finally, all is good, show the page content */
print $pagecontent;
}
}
}
}

--
wOOge | axonz.com

will this work for version 5?

mrgoltra - July 13, 2007 - 08:10

Good Day,

I was wondering if this will work for version 5?

Thank you,

Mark

 
 

Drupal is a registered trademark of Dries Buytaert.