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

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...

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 =...
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...

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

Multilingual views in Drupal when using i18n module

1st Mar 2008

Building multilingual sites in Drupal using the i18n module always throws up a few fun problems to be solved. One of the most awkward being views.

The i18n views integration module does an excellent job of allowing you to return only nodes matching a particular language, but it doesn't help when it comes to translating the view itself - and more importantly allowing a view to site nicely into multiple positions in the menu (one per language).

The solution is to use the excellent insert view module (http://drupal.org/project/insert_view - you can use this guide to get the module...

Read more

Drupal - changing the calander view to display single letter for day of the week

30th Nov 2007

By default the Drupal event module will provide a nice calander block, listing the days of the week accross the top using 3 letter abbreviations (mon, tue etc).

This little theme snippet will override this default behaviour and display the first letter of each day of the week (i.e. M T W) etc.

Pop the following into your template.php file and you should be in business


function phptemplate_event_calendar_month($op, $header, 
$rows, $attributes = array(), $caption = NULL) {
  //run through each item in the old header and just use the first letter 
//instead of the entire word
  foreach ($header as...
Read more