One 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:
Make sure that your settings for that field allow users to upload multiple different types of file:
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:
Let’s add the field field so that it’s visible on the page:
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:
- Click Apply twice.
- Click Add next to Filter Criteria.
- Search for “File: Mime type”. In this case, mime means “Internet Media Type“:
- 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.
Save that Filter Criteria and your view should be complete! In this example, you would only see content that has an image attached.