How do I make a user have to register to download an attached file?

i-sibbot - February 4, 2009 - 13:50

Hi,

I've got a site that requires a user to register to download an attached file in a page.

Is there a native way in drupal that this can be done or can some one point me in the direction of a module.

More explaination.

It would be cool to have a "You must register to download this file" message displayed inplace of the actual download link for an anonymous user?

Thanks in advance for any comments or suggestions.

S

_

WorldFallz - February 4, 2009 - 16:52

You should be able to theme this into the node.tpl.php file with something like:

<?php
global $user;
if ((
$node->files) && ($user->uid == 0)) {
  print
"You must register or login to download attachments.";
}
?>

===
"Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." - Lao Tzu
"God helps those who help themselves." - Ben Franklin
"Search is your best friend." - Worldfallz

Big up WorldFallz

i-sibbot - February 5, 2009 - 13:25

Cheers dude. Was beginning to lose faith in the drupal forums there!

Implemented and it works a treat. Thank you very much again.

S

_

WorldFallz - February 5, 2009 - 15:51

You're welcome-- and that's dudette to you! ;-)

===
"Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." - Lao Tzu
"God helps those who help themselves." - Ben Franklin
"Search is your best friend." - Worldfallz

Hi...any chance of a few

binarylime - May 14, 2009 - 20:58

Hi...any chance of a few pointers to get this working please?

I've got loads of nodes with attachments: word files, pdfs etc. I want anonymous users to see a message saying 'please login to download attachments' or better still would be for them to see the attachment but when they click on it they are redirected to the login / register page.

Authenticated users would be able to see and download the attachments as normal.

I've played with the code above and tried it in node.tpl.php but I'm not getting anywhere..

Thanks in advance.

Found the solution

simplyrahul - July 3, 2009 - 05:34

I was looking for the same thing

If using CCK - goto file_formatter.inc in filefield folder in module

and paste the following code at the bottom

global $user;
if ($user->uid == 0) {
$url = "";
$link_text = "You must register or login to download attachments.";
}

  return '<div class="filefield-file clear-block">'. $icon . l($link_text, $url, $options) .'</div>';
}

I do not know how safe it it. But it worked for me. And it seems to be fine.

_

WorldFallz - July 3, 2009 - 12:46

Editing core drupal or contrib module files is never the right answer-- you essentially create a fork of the code that you must now maintain yourself.

The code I posted above works for attachments made via the core upload module. I don't know off the top of my head what the answer is for filefields, but there's got to be another way. When I get a chance to look at it I'll post back.

_
Don't be a Help Vampire - read and abide the forum guidelines.
If you find my assistance useful, please pay it forward to your fellow drupalers.

_

WorldFallz - July 3, 2009 - 13:34

Actually-- this is even easier with filefields. There's probably other ways, but the probably the easiest would be to simply use the content permissions module and only check the view field permission for the filefield for authenticated users. Then add a computed_field to the content type with the following:

global $user;
if (!$user->uid) {
  print "You must " . l('register or login', 'user') . " to download this file.";
}

And set the field permission to be viewable to anonymous users.

_
Don't be a Help Vampire - read and abide the forum guidelines.
If you find my assistance useful, please pay it forward to your fellow drupalers.

 
 

Drupal is a registered trademark of Dries Buytaert.