Drupal 7: Draggable Views and Custom Sorting of Content

David G - DrupalThis topic is 1 of the banes of my existence. There is little else so annoying and so frustrating as, “How do we Sort it?“. If we’re showing a list of campus courses what is the sort order? Alphabetical? By some hidden serial value? By Content Type? Newest to Oldest? … for any listing of content the sort order is either simple — or completely arbitrary. I run into the issue quite a lot. Sometimes a solution such as a Serial Field suffices, but usually it’s more annoying than that.

Recently I stumbled across the Draggable Views module. It looks like this module is slated for inclusion into Drupal 8 (but I’m uncertain of its final status in D8). The features listed on the project page are:

  1. Order any type of entity
  2. Set different order for different set of arguments
  3. Create one view that sets the order, and create as many views as you like that shows that order
  4. Use core tabledrag.js or jQuery UI draggable javascripts to set the order

So, this module allows you to create special Views display that can act as the sort order for the same view. This new View can act as the Admin UI used to set the sort for a certain view for site administrators! Nice!

An example of this module for a logged in admin user having been properly configured can look like the following image. The 2 tables at the bottom are the content of the View(s) at the top of the page — when you click-and-drag a row around it’s position is saved and updated in the master public view.

Draggable views admin example. The table row weights become the real view's weight per item.

Draggable views admin example. The table row weights become the real view’s weight per item.

This is an example for the actual Sorting view, the key element is the DraggableView field in this View, this essentially links the hidden NID value in the view as the row weight when sorting.

Draggable View's field configuration.

Draggable View’s field configuration.

Lastly, it’s important to consider the performance issues when using this module. Basically we’re using a View to be the sort of another View. A View is not a piece of content — a view is a query. So, when using draggable views you should consider enabling the views Caching under the Advanced tab for the view. There is no need to run the SQL to define the sort order on every page load of the public view. It should be able to be pulled from a cache to help maintain high performance on your website.

Looking for quality web hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , , , , , , , , , , , | Posted under Drupal | RSS 2.0

Author Spotlight

David Gurba

I am a web programmer currently employed at UCSB. I have been developing web applications professionally for 8+ years now. For the last 5 years I’ve been actively developing websites primarily in PHP using Drupal. I have experience using LAMP and developing data driven websites for clients in aviation, higher education and e-commerce. If you’d like to contact me I can be reached at david.gurba@arvixe.com

Leave a Reply

Your email address will not be published. Required fields are marked *