| Drupal

file filterOne of our members asked an interesting question about Views.

They had a file field on their user profiles. In that field, the user could upload an image, an audio file, or link to a YouTube video. So far, so good. However, in Views, they only wanted to show that field if it contained a video.

Here's the solution to that problem. We're going to show you how to filter Drupal content based on the type of file that's attached to it.

First, you'll need a content type with a file field:

media_1418215569896.png

Make sure that your settings for that field allow users to upload multiple different types of file:

media_1418215548790.png

Now, go to Structure > Views. We're going to create a View that shows only content with image files attached. At this point, we don't need to do anything different. Just make sure to choose the content type with files attached:

media_1418215766912.png

Let's add the field field so that it's visible on the page:

media_1418215814095.png

Now, we can start to create the filter that will select on content with a particular type of attachment.

  • Under Advanced, click Add next to Relationships.
  • Search for your file field. This needs to be the option with the fid (file ID number) attached:
media_1418215962418.png
  • Click Apply twice.
  • Click Add next to Filter Criteria.
  • Search for "File: Mime type". In this case, mime means "Internet Media Type":
media_1418216076142.png
  • Choose "Starts with".
  • For the Value, you can enter "image", "audio" or the choice that you need. Harvard has a complete list of which file types correspond to which media types.
media_1418216103313.png

Save that Filter Criteria and your view should be complete! In this example, you would only see content that has an image attached.


About the author

Steve is the founder of OSTraining. Originally from the UK, he now lives in Sarasota in the USA. Steve's work straddles the line between teaching and web development.