Skip to main content

Working with Drupal views

The views module is what always brings a smile to peoples faces when we do our Drupal training, even the most die-hard anti-CMS I-want-to-write-everything-myself-in-raw-PHP developer grudgingly offers up some praise when we do a demonstration.

We've been using views since the dark ages of Drupal 4 all the way up to it's inclusion as a core module in Drupal 8. I don't think we have ever built a site without at least a couple of custom views, and most have a few custom views handlers thrown in to handle those cheeky listing or exporting situations.

We've blogged a bunch about Drupal views over the years, you can read it all below ...

Read some of our articles about Drupal views

Views caching

3rd Aug 2010

A little while ago I blogged about views content cache as a way to increase performance of your Drupal site. Today I released version 2.2 of the module, that adds lots of lovely features.

The module has basically been re-written by Young Hahn from Development seed to provide a plugable mechanism for exposing cache segments. This means that any contrib module can contribute to keeping track of the what content has changed on the site really easily. I’ve even written a small development guide to getting started, in the drupal.org handbooks.

The module should be stable and working enough to...

Read more

Nodequeues for developers

27th Aug 2009

Nodequeues are a really useful way of building ordered lists of content and they integrate amazingly with views and have quite a nice interface for adding/removing nodes. Out of the box you can create queues and choose node types that can be added to the queues, and roles that can manipulate the queues, with a little more effort and some code you can do a lot more. We had built a media competition feature for a client; they can create a competition into which users can post photos or videos that can then be voted and commented on. However we...

Read more

Using Drush export content type, and views

6th Jul 2009

We're not sure what to do with these: we've written two simple Drush scripts for exporting content types and views. Saving us from having to navigate a few pages and precious seconds.

Download them and pop them somewhere Drush can find them, a good candidate would be in ~/.drush

Then you can run them by typing:

drush export view <view_name>

or

drush export content type <type_name>

You should now either have an error message or and exported view or content type in a file. The files are saved in the current working directory (in most cases where you ran drush...

Read more

DrupalCon DC - Business Analytics in Drupal with Views

12th Mar 2009

One of the interesting sessions that I attended at DrupalCon DC was one entitled: Business Analytics in Drupal with Views. In it, the presenters showcased two of their modules. One was a charts display plugin for views, and one was a 'group by' views field.

Views charts

This was a simple module to take the output of a views query and pump it into a flash charting framework. Being able to display the output of your views with charts is really nothing new, and the presenters admitted to duplicating existing modules. Hopefully a module will emerge from the mess...

Read more

Views 2: Making a drupal views block title link back to the view

13th Feb 2009

We wrote an article about linking a view's block title to the companion page view, in Drupal 5. Someone asked for a Drupal 6 version, which can be quite easily done with a preprocess function, but it's not very flexible.

Instead we just need to create a new display plugin for Views 2, and that will allow us to be much more flexible in our approach.

This is my first display plugin, so it may be completely broken (though it works for me!) You'll just need to download and install the attached file like any other module.

Instead of...

Read more

Drupal node operations

7th Feb 2009

We love the views bulk operations module here at ComputerMinds, and frequently use it to create some lovely administrative interfaces.

However we often find a need for an administrator to be able to perform operations in a quick and easy way on an individual node. While some of the core operations (publish, make sticky etc) can be performed from the edit tab of the node, many other node operations do not offer this facility ("rebuild image thumbnails" for example).

So we have put together a simple module that exposes a form just like the one you get on the node...

Read more

Some nice quick views theming tricks in Drupal 5

27th Jan 2009

Thought I would post this little function on here, we tend to use it a lot and it gives some nice little extras for you to play with when you come to template your nodes.

  • $node->view_name : the name of the view this node is being rendered in
  • $node->position_in_view : the position within the view for this node

function phptemplate_views_view_nodes($view, $nodes, $type, $teasers = false, $links = true) {
  static $count;

foreach ($nodes as $n) { $node = node_load($n->nid); $node->view_name = $view->name; $node->position_in_view=$count[$view->name];

$count[$view->name]+=1;
$output .= node_view($node, $teasers, false, $links);

}
return $output;
}

Read more

Making a drupal views block title link back to the view

24th Jan 2009

One for the small but handy category this. The standard "more" link the views module adds to it's blocks has no place on a modern accessible website, you really need a more verbose link in there. One solution has always been to add a bit of footer or header text into the block, but it's a little tedious - and clients always find views hard to edit.

This simple solution will turn the block title into a link to the view, it goes into your template.php _phptemplate_variables function


  if ($hook=='block'){
    $block = $vars['block'];
    if ($block->module == 'views'){
      $view = views_get_view($block->delta)...
Read more

Organising your views in Views 1.x

17th Jan 2009

If you're still stuck in the Views dark ages, and 1.6 is as good as it gets for you here's a quick tip to make things a little easier:

Exporting your views

You've just spent a good hour creating and tweaking that view until it shows exactly the right thing, and now you want to save it for posterity, portability and all that jazz.

Simple. Click on the 'Export' tab within Views and you'll get a lovely version of your view in code, which you can pop in an implementation of hook_default_views() and you're done, almost.

That's all well and...

Read more

Displaying exposed filter form for views in Drupal 6 (views 2)

15th Aug 2008

One of the joys of working with Drupal 6, and views 2, is that you have to relearn a lot of things you used to take for granted ... one trick we use in most projects is embedding views filters in blocks, nodes or custom code. There are plenty of scenarios this is useful, the classic being to create a flexible views based replacement for the normal Drupal search. Anyway, enough of the blurb - heres the good stuff - some code to give you the filter form for a Drupal 6 views 2 view.


  $view = views_get_view('your_view_name');
  $view->set_display('default');
  $view->init_handlers()...
Read more