Views for Developers

 

These are my notes from a talk at DrupalCon Denver 2012 by Bevan Rudge @BevanR from PreviousNext. The slides from this talk are available here.

Views is a very powerful tool that has a lot of features which many people have not discovered, despite having used views for a long time.

Site building can be quite difficult. It's engineering, and any medium to large website will concern several content types, vocabularies, entities and relationships. There are a lot of moving parts to get your head around, and as software engineers, we're always looking for the simplest ways to solve this problem.

Don't tell me how complicated the problem is... tell me how simple your solution is.

-Anonymous

You can build awesome things with views. Examples:

  • Carousels: landing page for Girlosophy, with two carousels using views slideshow
  • Lists: MPRarchive: list of news items "displaying 1-12 of 197 stories"
  • Displaying tabular data
  • Grid showing data-row as table cells with images
  • Collections of news articles, sorted by number of stories in each collection
  • User profile pages: content region has been overridden and shows blog posts by the user, and the user profile is rendered as a block in the sidebar using views.
  • Admin user interface using header views, attached views

Custom Code

One of the great things about Views is that you can implement UIs without implenting custom code. Custom code is expensive, it's buggy, upgrading it takes time, and it's a pain to add features. Time is money, especially for professional services companies, but even for organizations working on internal Drupal projects.

Before you start writing custom code and extend Drupal views with code, it's really useful to look for ways that you can get closer to what you need (or perhaps exactly what you need) using existing code. The closer you get, the less custom code you need to write. The more config you know, the less custom code you need to write.

Anatomy of a View

  • Title
  • Display
  • Header area
    • Area plugin
    • ...
  • Exposed Filters
  • Attachments (before)
  • Rows
    • Row (Entity or Field Field Field...)
    • Row (Entity or Field Field Field...)
    • ...
  • pager
  • Attachments (after)
  • Footer area
    • Area plugins

Field Configuration

Each field has custom markup that you can configure through the Views UI, for example, an h2 element. You can assign CSS classes and wrapping elements using a dropdown. In the row style options, you can provide default wrapper elements for the entire view, which allows you to get rid of all the views wrapper elements, which can speed things up. However, if you have multiple views that need the same configuration for that markup, you're going to run into the same configuration across your views, which can be hard to maintain. At that point, look at View Modes. If you need more view modes, there is a module called Display Suite which extends View Modes and allows you to create custom modes.

Another option is the Fences module, which allows you to configure the wrapping element in the Field API configuration form. I haven't used it, so I can't speak about it much more than that, but check it out.

Field Concatenation and Linking

You can rewrite the output of each field using the "Rewrite Results" area of the field configuration. Views has a token integration that work very well.

You may also want to link a field to a particular page; views lets you customize the target that the field links to, from a custom system path to replacement patterns. This is useful if you want a row or a field in your configuration to link to another view that takes the node ID as an argument, rather than linking to the node page itself.

Contextual Filters

Contextual filters are always int eh first group, even though they do not appear in the UI. So, you can actually "Or" together the contextual filters, which would mean that only one of them is required, for example.

Result Summaries

I only just discovered this feature. It allows you to add text along the lines of "Displaying 1-7 of 7." It is distinct from a summary view. Result summary is an area plugin. You can configure the text and include various different numbers in that text.

Aggregate/COUNT queries is difficult to find because it clutters the user interface. Unfortunately the Views UI interface doesn't scale to exactly the level that we need for this feature. If you're not familiar with SQL enough to read and debug it while you're configuring the COUNT query, you may not want to use this feature. However, it is very powerful. If you add it to your sort criteria, you'll be able to use this in many different places.

Alternative Pagers

Spmetimes it's useful to have alternative pagers that sort content by date, for example. In most cases these are contextual filters, but the Date pager is actually configured in the Pager settings. There is also an Alpha/Glossary type of pager. You can also build pagers like this using views attachments, which gives you more flexibility regarding the rendering of each pager item, but it may be a bit harder to get it to render by a pager.

Views Link Area

This exposes an area plugin that lets you add a link to an area of a view, which lets you handle links even if the site moves between directories or whatever.

Documentation

  • By topic: api.drupal.org/api/views/groups
  • By keyword: api.drupal.org
  • Doxgen: drupal.geek.nz/views

Exposing a class to views:

  1. Implement hook_views_api(). Tell it what version your module supports, and where in your module your views include file is.
  2. Create a subclass of the appropriate Views class
  3. Expose the class/include in the *.info file: files[] = views_linkarea.module; files[] = views_linkarea.views.inc;
  4. Expose the class via hook_views_data()

Questions and Answers

Q:
A:

Did you enjoy this post? Please spread the word.