Posted by i-sibbot on February 4, 2009 at 1:50pm
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
Comments
_
You should be able to theme this into the node.tpl.php file with something like:
<?phpglobal $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
_
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.
Big up WorldFallz
Cheers dude. Was beginning to lose faith in the drupal forums there!
Implemented and it works a treat. Thank you very much again.
S
_
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
_
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.
Hi...any chance of a few
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
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.
_
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.
_
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.
I followed up your suggestion
I followed up your suggestion (simple and clean). I just wanted to add an if condition : print it only if there is a file attached to the filefield node. I tried this in the computed field (I am a newby in php and computed field) (field_private is a filefield):
global $user;if (!$node->field_private['0']['value'] != '') {
if (!$user->uid) {
$node_field[0]['value'] = t("Vous devez " . l('vous enregistrer ou vous connecter', 'user') . " pour télécharger le fichier attaché.");
}
}
It does not work! The problem I believe is that filefield does not seem to have a 'value' column in the database - tried 'fid' and 'list' with no success. Not sure whether I should have the computed fied stored in the dabatase for this or not.
Sorry for my incompetence ! Any help welcome...
Tomski
_
I still can't keep all the different parts of the different fields straight in my mind-- try a simple
<?php print '<pre>'; print_r($node->field_private); print '</pre>'; ?>to see exactly whats there that you can use._
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.
I found the solution by
I found the solution by putting this in the computed field (using the computed field module) :
$node_field[0]['value'] = $node->field_private[0]['fid'];
if ($node->field_private['0']['fid'] != '') {
$node_field[0]['value'] = t("Vous devez " . l('vous enregistrer ou vous connecter', 'user') . " pour télécharger le fichier attaché.");
} else {
$node_field[0]['value'] = '';
}
The filefield does not indeed have any 'value'. By printing the 'fid' I realised I could use this. My previous code had two ! in the if clause which I guess caused some troubles.
Thanks for the support.
_
Excellent! And thanks for posting back your solution-- i'm sure it will help someone else in the future.
_
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.
I mixed the two codes I used
I mixed the two codes I used : so this is what you need to put in the computed field to make this work (after t( you will put of course the message you want to appear at the bottom of the node):
if ($node->field_private['0']['fid'] != '') {$node_field[0]['value'] = t("Vous devez " . l('vous enregistrer ou vous connecter', 'user') . " pour télécharger le fichier attaché.");
} else {
$node_field[0]['value'] = '';
}
I still have a question : with a computed field you may have the value computed stored in the database when the node is saved or calculated each time when the node is seen. The advantage of having calculated each time is that you do not have to redit all your nodes. Nevertheless, I guess this will take its little toll in terms of performance. Is it always better to store the value in the database ?
_
I don't think you can say one way is always better than the other-- it really depends on what you're doing. Depending on the code in the field, it may only be accurate if calculated fresh every time.
_
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.
Hi guys. Anybody looked at
Hi guys.
Anybody looked at doing this for Drupal 7? I know that the file module has become part of core and a few things have changes. I've looked at node.tpl.php but can't figure this out!
I'd like to do three things:
When a user is logged in - the files are visible and available to download.
When a user is not logged in - the files are still visible but clicking on them takes you to the register page AND a message is displayed saying 'please login'.
The files table is visible in node teasers.
Help appreciated! Thanks.