Drupal: Creating a View of files associated with the current node


In Drupal you can create a content type with a File field that allows multiple values. This means that when a new node is created you can upload several files to attach to the node.

When this list is displayed on the front end it is shown as a list in the order the files are in on the edit screen. Although you can drag and drop these items into any order, your options for ordering files automatically are limited.

Using a view

An alternative to using the standard node listing would be to create a View.

Create a new View and make sure it is showing Files.

We’ll be using a Block display.

Add a Relationship to your View using File: Content using File. Make sure it is using the field that you set on your node (you can verify this on the next screen).

Once you have your relationship set up you can add a Filter of Content:Type. Make sure this is set to your node’s content type.

Make sure the filter is using your relationship and set it to your node’s content type.

This should update your preview to only showing files uploaded for your content type. However, it will show files for all nodes of that type. We’re going to display this View as a block on the node page so we can get the current node’s nid from the page URL.

Add a Contextual Filter of Content: Nid.

Make sure it is using the relationship set up earlier. In the When the filter value is NOT available section, select Provide default value then Content ID from URL in the dropdown list that appears.

This will limit our View to only showing files associated with the current node by reading the node id from the URL.

The preview area will be empty now but you can test the View by entering a node ID into the Preview with contextual filters box and clicking Update preview.

Finishing Up

Now you can add any Sort criteria you like and add the block to your content pages. Don’t forget to hide the File field in Manage Display otherwise you’ll see the list of files duplicated.

Leave a Reply

Your email address will not be published. Required fields are marked *