Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I think would be very useful to have the ability to set in "Display Field Settings" and Views an option to have the file open a new window. This can be easily themed, but for non-coders this would be a simple, handy feature. Thanks! Let me know if I can help in any way.
Comment | File | Size | Author |
---|---|---|---|
#81 | Screenshot-2018-1-22 Edit menu link cppp.png | 49.64 KB | Abhinaw |
#20 | cck_filefield_new_proposed_displaysettings.png | 55.75 KB | manuel.adan |
Comments
Comment #1
Mac Clemmens CreditAttribution: Mac Clemmens commentedTo build on this idea, what about having some options like:
Display Filename as Link to File
Display Node Title as Link to File
Display Filename as Link to File in New Window
Display Node Title as Link to File in New Window
etc.,
Comment #2
anthonym CreditAttribution: anthonym commentedFor Views, I would love to have an icon-only field display option for table views. Right now, there is only the "Default" option which shows title (as link) and icon.
Comment #3
dopry CreditAttribution: dopry commentedWrite your own formatter... This isn't going into filefield.
Comment #4
raintonr CreditAttribution: raintonr commentedUnless I can't see it anywhere this feature didn't get implemented in D6 version. Such an option would be very useful.
Comment #5
quicksketchWe discussed this once in the D6 queue and I did implement the link opening in a new window, but only on the node edit form, since leaving the page would cause your new uploads to be lost. However this still won't be added to the front-facing file display. I'd suggest putting some jQuery in your theme such as this:
Or you can use any number of popup scripts out there to make your files open in a window that is a certain size.
Comment #6
quicksketchComment #7
ali_b CreditAttribution: ali_b commentededit function theme_filefield_file in filefield_formatter.inc
best way is to add to your template.php:
Comment #8
Xagarsan CreditAttribution: Xagarsan commentedOk!, it is everything correct.
Thank you very much, I brought while attempting to find the solution.
Sincerely,
Xavier Garcia
Comment #9
dppeak CreditAttribution: dppeak commentedTry the following with slight modifications to open in a new window based on the MIME type. The idea was borrowed from an idea jpetso said here http://drupal.org/node/190566.
Comment #10
maverick14 CreditAttribution: maverick14 commentedI like the suggested solution in #1. That would be very cool.
Comment #11
freatida CreditAttribution: freatida commented#7 works great for me. Thanks for posting.
Comment #12
leon85321 CreditAttribution: leon85321 commented#7 resolve my problem too, great job!
Comment #13
aiphes#7 solve my interrogation...great thx
Comment #14
gustavoiranzo CreditAttribution: gustavoiranzo commented#7 and #9 solve my problen ...great
Thanks ali_b and dppeak
Comment #15
davepoon CreditAttribution: davepoon commented#7 and #9 solutions are great! Thanks!
Comment #16
arpieb CreditAttribution: arpieb commentedI wound up going with #9, which works fine but shouldn't be required just to get this kind of functionality, IMNSHO. I had to update code in SVN, migrate the changes onto a server, test it, etc before deploying when it would have been great to have either told the client (or done it myself) to just click a checkbox that said "Open in new window" on the field settings page and hit "Save Settings."
The Link CCK field offers it as an option on its field (along with quite a few other nice options like link text on/off/optional/static which would have been handy on so many occassions for this field, but I digress), not quite sure why there is so much resistance to adding this feature when so many people have asked for it?
It'd make the module a lot more user-friendly for those people setting up Drupal sites that are not ripping into the template files, or people administering sites that have no clue how (and whose job it isn't) to hack on a template file...
Comment #17
thrash632 CreditAttribution: thrash632 commentedI agree with #16, couldn't said it better myself.
User friendliness is key.
Comment #18
Danny EnglanderThank you for this, exactly what I needed. #9 worked great.
Comment #19
meirbr CreditAttribution: meirbr commented#9 worked great when i changed in the filefield_formatter.inc
but when i copy this function to mu template.php it doesnt work!!!!
what is my problem?????
Comment #20
manuel.adanHi,
Code base solution (in theme template.php file) is not the best way since:
I think a better (and complete) approach should be a new field display related settings, something like proposed in attached screenshot.
Comment #21
quicksketchSorry, this isn't going to happen. A theme-based solution is the best one in my opinion. FileField has been moved into core for Drupal 8 and won't be changing any more functionality. At least that means that you're safe to override the code in your theme, since it shouldn't change for the lifetime of Drupal 6 or 7.
Comment #22
manuel.adanOk, I'll made this change for my personal drupal base installation. I don't like to do the same work once and once again for every website I made. If somebody wants the patch, just ask me. Thx.
Comment #23
pvanerk CreditAttribution: pvanerk commentedAdan, can you please send me the patch or indicate where I can download the patch.
Thanks!
Comment #24
rmwolinski CreditAttribution: rmwolinski commentedHow can I use "open a File in new window" in drupal 7?
Comment #25
Ganganation CreditAttribution: Ganganation commented#24 @rmwolinski
Hope this helps: http://drupal.org/project/extlink
Comment #26
rmwolinski CreditAttribution: rmwolinski commentedI tried this module but this is for external links. Maybe I can't configure it.
Comment #27
jasondecamp CreditAttribution: jasondecamp commentedThe solutions given above do not work for Drupal 7 due to changes in the filefield module in core. Use the following code (placed in your template.php file) to accomplish the same as #9 in Drupal 7:
Comment #28
pitxels CreditAttribution: pitxels commented#27 is working great :)
Comment #29
aiphes#27 works :) for D7 thanks
do you think is it possible to do the same but with the W3C javascript standards ?
Comment #30
petersmeekens CreditAttribution: petersmeekens commented#27 did it for me too. Thanks!
Comment #31
superdorx CreditAttribution: superdorx commentedI inserted code from #27 in my template.php and changed the THEMENAME. My PDFs are still opening in same window. Am I missing something?
Comment #32
rschuetzler CreditAttribution: rschuetzler commentedI'm in the same boat. Not having any luck getting them to open in a new window. It would appear the function isn't being called to create the link. Any hints?
Comment #33
DRIVE CreditAttribution: DRIVE commented#27 worked for me as well.... 1 minute and I found it G0ogle.... saved me much time to write from scratch... thanks!
*having trouble with not opening in new links after you insert the code in your theme template? Must flush cache any time you update theme template unless using a mechanism which is clearing it for you (90% of the time you will need to manually flush, so just do that anyway)
Comment #34
sheathe CreditAttribution: sheathe commentedThank you - #27 worked for me in Drupal 7 (after clearing the site cache.)
Comment #35
colorado CreditAttribution: colorado commentedBoth #7 and #9 worked for me.
Thanks for sharing this all!!
Comment #36
Chitotakun CreditAttribution: Chitotakun commentedthankful, #7 #9 #27 and #34
Comment #37
Danny EnglanderThough many of the above methods are valid to do this, you could also use JQuery (attribute), I tried this and it works. Obviously your specific code might change a bit depending on what you are targeting. This may not be as comprehensive for some of the methods above but in some cases may be fine. In this example, I had a filefield called "App Landing". This would work for Drupal 6 but could be adapted for 7 as well.
Note change "mytheme" to your theme name and refer to # 39 below for instructions how to add this.
Comment #38
colorado CreditAttribution: colorado commented#highrockmedia -
Where do you put this, JQuery (attribute)?
Comment #39
Danny Englander@colorado -- you could do it a few ways.
Comment #40
colorado CreditAttribution: colorado commentedOK then. Thank you very much!
Comment #41
bensoi CreditAttribution: bensoi commented#27 worked for me. thanks a bunch!
if it doesn't work for you, flush the cache. i just replaced the THEMENAME with my own theme name, added some array elements on $new_window_mimetypes, flushed the cache and tadaaaaaah! ^^
Comment #42
mcchrome CreditAttribution: mcchrome commented#27 worked just as expected. From an end-user perspective it's a shame the options won't even make it to D8.
Comment #43
4kant CreditAttribution: 4kant commented#27 worked for me - thanks to all of you!
Comment #44
ladybug_3777 CreditAttribution: ladybug_3777 commented#27 worked the way I wanted. Perfect! Thank you
Comment #45
docker CreditAttribution: docker commented#27 solution works fine. Thanks!
Comment #46
ilfelice CreditAttribution: ilfelice commentedFWIW, #27 works here too (in D7).
Comment #47
thentha18 CreditAttribution: thentha18 commentedThanks a lot. Tried #27 works perfect on D7.
Comment #48
loko CreditAttribution: loko commentedMe too. Thank you.
Comment #49
tsaks CreditAttribution: tsaks commentedThanks. #27 works in Drupal 7.
Is there anyway to adapt that so that html files that are uploaded can open in a new window?
Comment #50
dvdooren CreditAttribution: dvdooren commentedI have done as #27 told me. Copied the code into my template.php file, replaced the "THEMENAME" with my actual theme name. After saving, I cleared the cache on my website. But for some reason, this still isn't working.
I have very little knowledge of PHP, so figuring out the problem would be very hard for me. Is there anyone who might know how to solve this?
Comment #51
mcchrome CreditAttribution: mcchrome commented#27 still works great.
Comment #52
nitin.k CreditAttribution: nitin.k commented#27 Thanks...It works great.
Comment #53
PEP3 CreditAttribution: PEP3 commented#27 worked perfectly for me in drupal 7... thank you!
Comment #54
konstantinalz CreditAttribution: konstantinalz commented#27 worked for me in drupal 7. thank you!!!
Comment #55
scott.browne CreditAttribution: scott.browne commented27# worked for me. Had to replace the YOURTHEMENAME in the code with mine lol.
Silly I always forget that!
This is a nice feature for ease of use. A lot of requests on websites for this type thing.
Scott
www.coverclubmedia.com
Comment #56
bcobin CreditAttribution: bcobin commented#27 worked a treat for me... thank you, @jasondecamp - almost three years on!
Comment #57
magendiran CreditAttribution: magendiran commentedThank you - #27 worked for me in Drupal 7 (after clearing the cache)
Comment #58
ladybug_3777 CreditAttribution: ladybug_3777 commented3 years later and #27 still works for me! Thank you!
Comment #59
hockey2112 CreditAttribution: hockey2112 commentedThanks for #27!
Comment #60
miniwebs2 CreditAttribution: miniwebs2 commented#27 works well for document attachments however when using file path links within views, it doesn't appear to do the same thing.
i.e. I have a Title and a File Attachment as Content Type fields
Within my Views settings:
Fields:
Works perfectly to the pdf it needs to go to but opens in the same window - would very much like it to go to target _blank.
Any thoughts??
FIXED - Sorry - there's a file within views under re-writing for target _blank - works now!
Comment #61
Tsymi CreditAttribution: Tsymi commentedThanks for #27 it's worked for me too !
Comment #62
maxplus CreditAttribution: maxplus commentedThanks,
#27 is working for me to.
Other solutions like rewriting the field in views did not give me a solution because I had a multi value field
Comment #63
stevenx CreditAttribution: stevenx commented#27 worked, thx
Comment #64
prsnjtbarman CreditAttribution: prsnjtbarman commented#27 - It works fine for me. Thank you.
Comment #65
phanophite CreditAttribution: phanophite as a volunteer commented#27 worked for me as well. Thank you!
Comment #66
thalemn CreditAttribution: thalemn commented#27 works as expected. Thanks!
Comment #67
anouThanks to jasondecamp. Must say: 4 years and #27 still does the trick. ;-)
Comment #68
handkerchiefHere is my code for drupal 8 if anyone search this.
Comment #69
danimanie CreditAttribution: danimanie commented@handkerchief Thanks for drupal 8 hook! It helped a lot.
But there was a little mistake in that line where you set $link variable.
I changed it a little bit to get it working in my theme.
Here is my result:
Comment #70
handkerchief@danimanie Thank you for your response, of course, that was my fault, a typical copy paste mistake i guess :) In my productive code i have exactly the same code as you. Thanks for the note and the correction.
Comment #71
Arslan23 CreditAttribution: Arslan23 as a volunteer commented#Danny Englander
Thanks. jQuery work for me in template script.js, But #27 not work with my D7 Theme,i am using zeropoint theme.
Comment #72
Anonymous (not verified) CreditAttribution: Anonymous at Sprintive commented#27 worked for me , thanks .
Comment #73
Adrian83 CreditAttribution: Adrian83 commentedFor Drupal 8, I copied and pasted #69 into the CUSTOM_THEME.theme file in my custom theme. The only change was to change the "THEME" in
function THEME_preprocess_file_link(&$variables) {
to my custom theme's name. Clear cache, and all links created by the file field will open in new tab.Comment #74
Xave54 CreditAttribution: Xave54 commented#27 doesn't work for me, help!
Comment #75
thalemn CreditAttribution: thalemn commented@Xave54
Which theme are you using? Did you flush all caches?
Comment #76
knalstaaf CreditAttribution: knalstaaf commentedTo people who don't get #27 to work: is the mime-type specified of the file you're trying to open in a new window?
By default the following mime-types are mentionned in the code:
application/pdf
andtext/plain
- you can find these in the provided code.Add more of your own if necessary, e.g. for jpg and png files:
$new_window_mimetypes = array('application/pdf','text/plain', 'image/jpeg', 'image/png');
And indeed: flush Drupal's cache aferwards.
Comment #77
grasmash CreditAttribution: grasmash commentedSee https://drupal.stackexchange.com/questions/242763/how-to-change-the-targ...
Comment #78
april26 CreditAttribution: april26 commentedIt always amazes me that the tiniest function that is blindly obvious and included in every other CMS as default, requires programming in Drupal. I love Drupal, but sometimes it just requires SO much effort to do something so obvious.
Comment #79
handkerchief@april26 You speak right from my soul :)
Comment #80
Taliesin84 CreditAttribution: Taliesin84 commentedWhat if I want to apply the #27 workaround to file links of a specific content type? Is it still viable editing the template.php file and adding a similar function?
Won't this workaround make my website a little slower each time a page with a file link is loaded? (noob question, I know)
Comment #81
Abhinaw CreditAttribution: Abhinaw commentedi want to add node url which will open in next tab for eg.
Title :- news
url :- bbcnewse.com/newse (In new tab )
Thanks
Comment #82
Abhinaw CreditAttribution: Abhinaw commentedComment #83
david.qdoscc CreditAttribution: david.qdoscc commentedFor D7 this module does the trick: FileField Target
Comment #84
ras-ben CreditAttribution: ras-ben commentedFor Drupal 8, if you want a more readable and Drupal-esque way of solving it, i suggest doing something like this:
It technically results in the same as #69
To the top of your module/template file you'll also need to add:
Comment #85
illutek CreditAttribution: illutek as a volunteer commented#69 worked for me , thanks @danimanie
Comment #86
Kris77 CreditAttribution: Kris77 commented@qdoscc FileField Target module does not work with "file table" format, but only with "Generic File with Target".
#27 works for me too.
Thanks a lot @jasondecamp.
Comment #87
auxiliaryjoel CreditAttribution: auxiliaryjoel commentedUsing Drupal 8, #69 worked for me as per #73 notes
Comment #88
aiphesgrateful with #69 + #73 for D8. Is it possible to do the same for linkfields ?
Comment #89
Michèle CreditAttribution: Michèle commented#84 works perfect for me (with Drupal 8.7.8). Thank you, @ras-ben!
Comment #90
aiphesTo #84 :
How do you use your code ? in a module or in the .theme file ? I think it can fix my issue.
Because I encounter an WSOD after updating to D8.8.1 with this watchdog message:
Error : Call to a member function setGeneratedLink() on array dans gasquet_d8_2019_preprocess_file_link() (/home/XXX/www/XX/sited8/themes/custom/THEME/THEME_d8_2019.theme ligne 228)
Line which contain:
Thanks
Comment #91
watson.sm#90
I was running into this issue as well. $variables['link'] is not an object, it's only an array. However, within that array it does have an URL object.
I'm not sure if this is the correct Drupal way of doing things, but I worked around my particular issue with this code:
I put this code in MYTHEME.theme
Hope this helps.
Comment #92
aiphes@#91
I'm testing a module that do the work:https://www.drupal.org/project/issues/file_download_link
So I'll abandon this code way for the moment.