Drupal Views — A brief review
Views is an extremely popular Drupal module. As of time of writing, it has been downloaded 6,294,998 times and reported to be used in 876,607 sites. It’s so popular, in fact, that it has been included in core for the Drupal 8. So why is Views such an essential module?
If you’re not one of the 6,294,998 people who downloaded Views, or if you did but somehow just couldn’t grok it and just uninstalled it again, you may be wondering what the fuss is all about. Fact is, I almost fell into the latter category. Even though using Views is pretty intuitive to me now, I can still remember when it felt as complicated as trying to land Curiosity on Mars. Let me try to sum it up in three words.
Let’s start off by installing this module. You can download it from the Views project page on Drupal.org. Refer to Installing modules (Drupal 7) if you need help installing Drupal modules. As there is a dependency on the Chaos Tools Suite or CTools, you’ll have to install that module too. You have to enable the following 3 modules to use Views:
- Views
- Views UI → If you miss this, you’ll end up wondering why you can’t get to the Views admin page
- Chaos Tools Suite
If you use Drush, use the following command to download and enable Views:
drush dl views
drush en views views_ui -y
This will install and enable all the required modules, including CTools, for you automatically.
Go to the Views administration page and you will see a list of Views that come out of the box with the module. They will all be greyed out as they are disabled by default. The only one that I actually use is Taxonomy Term but most of the time I create my own Views.
A view is the selected content you pull from your database. Once you create a view, you can choose how to present this content through a variety of displays. We’ll call them view displays.
Clicking on ➕ Add new view will take you to the View creation wizard. It’s sort of a simplified version of the main Views editor. Here you can enter the name of your view and its description. For me, I tend to create views for each content type, as that’s how I structure my site data. But regardless, it’s always advisable to name your views in an understandable manner. It also helps to fill in the description to supplement your name. There are also basic filters you can apply.
You get 4 types of view displays out of the box:
- Attachment — A view attachment allows you to attach one view display to another and use it on your site as though it was a single view. I found this brilliant article that walks you through exactly how view attachments work.
- Block — A view block allows your list to be displayed as a block, which you can move around and position accordingly from the block admin page.
- Feed — A view feed allows your list to be rendered in an RSS feed format, which is good if you need to submit content to aggregators or want to allow people to subscribe to content on your site.
- Page — A view page basically turns your list of content into a page, complete with it’s own unique path.
Views main editor
There are a lot of options and settings available here. Remember the Master* panel earlier? Whenever you apply an option or setting, you can choose to apply them to All displays (except overridden), which modifies the Master display and will affect all other displays as well. Or you can apply it just to the current display. You can see how your view display would potentially look by scrolling down to the Live preview section below the editor.
- Display name
- Naming the display will help when you have lots of displays for the same view. It will not be shown on the actual content display.
- Title
- This will be displayed with the view. Enter <none> if you don’t want the title to display.
- Format
- Views come with default templates so you have a choice in how your content is displayed. You can choose to override them through your theme template files. The options are:
- Show
- You can show content based on how it’s displayed on the node, or you can choose which individual fields you want to display.
- Fields
- Select the fields you want displayed. They will appear in the order listed, so you can rearrange the order by clicking on the downward triangle next to the Add button to reveal the Rearrange option. This option is not available if you chose Content as the display mode.
- Filter criteria
- You can further fine-tune your list of content based on various conditions. You can use multiple criteria.
- Sort criteria
- Similarly, you can determine how the list will be sorted based on various conditions as well. You can use multiple criteria for this as well.
- Path
- Only applicable to Page and Feed view displays. You’ll need to specify a unique path for these displays.
- Access
- You can control the visibility of each view display based on user role or permission access. It is advised to set this for every one of your view displays for security purposes. The default option is the permission to view published content.
- Header and Footer
- You can create a header as well as a footer for your views display. The most commonly used option would be Global: Text area, which allows you to add custom markup.
- Use pager
- Odds are your list may be very long and hence require some type of pagination. This option is applicable to the page and block displays only. The attachment views display allows you to inherit the pager of the parent display it’s attached to. You can choose from either the full pager or mini pager, as well as specify the number of items to be displayed.
- More link
- If there is more content than is displayed, you can choose to display a more link which will link to the page view of the display. The text on the link can be customised here.