Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking to a "File type" Dexterity Content Type

We're creating a database of files on a new Plone 4.3.2 website, using eea.facetednav as the main interface. In order to have multiple searchable "tags" plus additional information like publish year, article type, etc etc, we've created custom Dexterity content types for these "files". One of the fields on the Dexterity content type is to upload the actual file.

Everything was going swimmingly UNTIL I realized...

One of the basic functionalities of linking to "files" is now broken. Whereas we would expect creating an internal link to a file will prompt opening the file or downloading it when clicked, now it just takes you to the "view" for the new Dexterity content type, where there's a link to the actual file.

This is pretty cumbersome. Has anyone done this before, or found a way to have it automatically link to download the file? If we need to disable/scrap the Dexterity content type view in favor of the direct link, that's probably fine, but bonus points if you can think of a way to make this work both ways (i.e. have a linking method that takes it to the file and a linking method that takes it to the view).

This is the page I mean by "view", in case it's unclear: enter image description here

... Off the top of my head, I can think of a weird Diazo solution of making a custom template that parses this view and automatically redirects to the file link, but that seems wrong/weird. Other thoughts?

like image 479
feesh Avatar asked Feb 12 '23 00:02

feesh


2 Answers

I'll go for the bonus points, though I haven't fully tested this...

First, you have to designate your file field as the content type's "primary" field. Unfortunately this option doesn't show up in the UI so you have to edit the XML version of the schema (there's a button for this in recent versions of the dexterity control panel). Add xmlns:marshal="http://namespaces.plone.org/supermodel/marshal" as a namespace at the top, and add marshal:primary="true" to the field tag in question.

Then, find your type in portal_types and change the "Default view method" to @@download. (It usually starts out as view for Dexterity content types.) This should make the item's URL download the file rather than showing the normal view. It knows which field to use because you designated it as primary.

You can still access the normal view by adding /view to the end of the item's URL. If you want Plone to do this when linking to the type from navigation and folder contents (just like it does for built-in images and files), go to portal_properties/site_properties and add the type's id to the typesUseViewActionInListings list.

like image 125
David Glick Avatar answered Feb 14 '23 14:02

David Glick


If I had to solve this problem, I'd take the "link" content type view in plone.app.contenttypes as a model.

When someone asks to see a link content type object, the view looks to see if the requestor has the Modify portal content permission on the link object. If they do, they see the conventional view, giving them the opportunity to edit. If they don't, the view returns a redirect to the target URL.

Your code would be simpler. After checking the permission, you'd only need to append @@download/field_name to get the redirect URL.

like image 37
SteveM Avatar answered Feb 14 '23 13:02

SteveM