If files are marked as private and I have links in a view to the files they do not work as they reference the file directly. Is there a way to have the link reference the file through System in a view?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

starbow’s picture

Category: support » feature

Views 1 integration exists, but I (or some other ambitious soul) needs to updated it for views 2.

meppy’s picture

OK, what would it cost to get you to do that sooner rather than later?

derjochenmeyer’s picture

Status: Needs review » Active
FileSize
17.17 KB

I dont know how to create a patch that includes adding a file.

Here is a first views 2 integration approach that works. Hope to learn something from follow ups ;)

derjochenmeyer’s picture

Status: Active » Needs review
starbow’s picture

Status: Active » Needs review

Great, this has been on my TODO list forever. Thanks.

Grodji’s picture

Hi,

I just wanted to know if it is possible to post the patch for version 5 of Drupal? Thanks in advance.

sean porter’s picture

Hi there - I downloaded this new version posted by derjochenmeyer, however my links to download files in views 2 still don't work (they point to the 'files/private/' directory) Is there something I need to do/update in my view to make this work?

thanks in advance!
Sean Porter

sean porter’s picture

sean porter’s picture

Sorry for the multiple posts...

I found another work around: in your view (a file-based view, although a node-based view would probably work)

make a relationship to upload : attached files.

then for your field, select group "uploads" and Upload : Attached files

Then label the field as you wish, and select "Link this field to download the file"

this generates the name of the file with proper url. it's still technically displaying the "wrong" file name as I mentioned in that other issue, however this isn't as important since it is pointing to the correct file.

HTH
Sean

rjacobs’s picture

Attached is the code from #3 is patch form. It seems to be working for me fine with private uploads 6.x-1.0-rc2 and views 6.x-2.11.

Basically I took the code from derjochenmeyer and did a full directory diff against private uploads 6.x-1.0-rc2. I did make 2 adjustments though:

  1. derjochenmeyer's code added the attribute target="_blank" to the links, which seemed strange to me... so I removed this.
  2. derjochenmeyer's version had some strange hidden characters before the opening <?php line which was leading to a bunch of "Headers already set" php errors. I took me some time to track this down and these characters are removed from the patch

I've had good results so far... hope others can review.

If this works for others derjochenmeyer should surely get credit

Best,
Ryan

rjacobs’s picture

Hi again,

The patch in #10 had some superfluous code. I also noticed that derjochenmeyer's implementation changed some of the "Upload: Attached File" field options in views (no tokens available, etc.). As a result, I re-worked things a bit in the attached patch so that the views options are now as close to "native" views functionality as possible.

One thing I did notice is that this fix depends on a "private" function (_private_upload_create_url()) being called from inside a view handler (views_handler_field_upload_fid.inc). I'm assuming this is alright given that the views handler is actually part of the private uploads module? I'm still somewhat new to the Drupal coding standards, so I wanted to ask about this.

Best,
Ryan

rjacobs’s picture

This patch has proven really handy for me on a couple sites, but I've noticed that there are heaps of views plugins and handlers that could define download links... each of which will continue to create incorrect paths. It seems that this is related to the fact that there is no "file_create_url" hook... so it's up to each module/handler to build the file download link (not Drupal). So I think getting every module/handler to play nice with Private Upload would mean patching every module/handler that builds file paths for node file attachments. I could be wrong about this, but this seems accurate based on some experience.

Anyway, it seems that the only fully robust way I could get private upload supported was to use this patch along with a rewrite (a concept explored at #636516: Option to use "mod_rewrite" in .htaccess rather than "deny from all" (avoid "Access Denied" errors from incorrect file paths)).

selinav’s picture

Assigned: Unassigned » selinav

You can also rewrite it as a link with the path system/files/private/[upload_fid-name] and uncheck 'Link this field to download the file'.

It works for me.

rjacobs’s picture

Hi selinav,

I believe that solution will work well most of the time, but there are some known cases where it does break down. I think things are discussed more at #575784: Views + Private Upload: Attached files: 'Link this field to download the file': does not work with private files (see comment #27 as well as the link to issue #706672). I suppose there may be other non-view modules that output file download links which may get the link structure wrong as well, that's why a rewrite (as discussed in #636516: Option to use "mod_rewrite" in .htaccess rather than "deny from all" (avoid "Access Denied" errors from incorrect file paths)) seems like the most robust solution from what I have learned so far.