Drupal Views and jQuery Tools
So this entry's a bit techy, but it's something I know I would have found very useful a few months ago. I'm going to attempt to explain the general idea behind combining a jQuery library such as jQuery Tools with the dynamic goodness of Drupal Views. And I say the "general idea" because this will not be a traditional tutorial detailing the a step-by-step walkthrough of a particular project. I may include something like that on down the line as a screencast (which it would probably require), but for now, I'm interested in covering the concept behind combining these two powerful tools.
For starters, you will want to have at least a basic knowledge of both Drupal Views and jQuery Tools before attempting to use them together. Resources on both can be found in the links provided.
GETTING STARTED
Anyone who has worked with Views in Drupal knows how powerful it is. Basically, it enables you to display content on the site in a dynamic and controlled fashion. For instance, my blog teaser listing (evanwillhite.com/blog) is a very simple View displaying titles, teasers and post dates sorted by the latter. However, a better example for our purposes here might be a scrollable gallery of thumbnails that displays a larger image when clicked (see here). jQuery Tools provides you with the scrollable gallery/overlay functionality, but Views provides the means to pull in new images dynamically. Put together with a module like CCK, you could then provide your clients with a way to easily add to photos to this scrollable galleries themselves simply by entering a new piece of content through the CMS.
Some of you may be asking why you would go through all this trouble when there are ready-made modules with this kind of functionality. A completely valid question, because if you are happy with the module functionality and it works, I say go for it! However, modules are, in the end, naturally confined to the author's intent, sometimes providing unnecessary overhead/functionality or falling short on providing enough. I've also run into walls with modules because they provide their own content types or means of adding content, which by nature is more confining than the traditional CCK/Views method.
THE METHOD & THE MADNESS
So how do we do it? Well, in theory, it's surprisingly simple! First of all, jQuery was pretty much built on the idea of simplicity, and jQuery Tools tries to stick with that ideal. Basically, it's a 3 step process:
- Call the script. You can call the jQuery Tools library either directly using their provided link or by downloading the specific tool(s) that you need and linking to them locally (see more on that here). You will also of course want to have the jQuery library linked as well.
- Create the View. Perhaps the most intimidating part, this is where you define the HTML output necessary for the script to run properly. - I'll speak more to this below.
- Link the script to your content. You can call the script from within your View (e.g., in the header) or a relevant template file. Again, fairly simple, as you're just latching the jQuery script to elements defined in the View.
So assuming you have the script itself (as well as the jQuery library) running properly on your site, you are ready to get started. But before jumping into the Views interface, I would suggest quickly scoping out what HTML/CSS are needed for the jQuery effects to properly display. What are the containing elements? How is the content structured? What will the script latch onto to provide the desired behavior? This understanding will directly influence the construction of the View, and the better it is, the faster the build will go. For example, in the scrollable image gallery mentioned above, there are two containing div's required by the scrollable thumbnail section alone - elements that may or may not be made available in the default output of your View.
THE VIEW
Once we have a clear picture for how the jQuery action works concerning the HTML/CSS, we can confidently build our View. After we've done the usual housekeeping items typically associated with the View (such as filtering published nodes/by content type as well as adding display information and/or sort criteria), we'll want to add the fields necessary for our view. In the example above, we would need the thumbnail images and the large images. If you're accustomed to using the excellent ImageCache module, you'll only need a single piece of content, which you can then display with 2 ImageCache settings (thumbnail and large image). Once your View is constructed, go ahead and take a look at it in action and view the source code to get an idea of what HTML the View has to offer in comparison to what is required by the jQuery tool.
A word of caution when looking at the output of your view - if you haven't done this before and you're obsessed with clean code... prepare yourself. Drupal Views (and arguably Drupal in general) can bring clean coders to their knees with it's bizarre naming conventions, seemingly endless elements, ids and classes, and so on. The good news is that multiple elements enable us to make very in-depth styling choices and give us a lot to work with for our purposes here. The bad news of course is that it may be overkill. However, what may look intimidating at first becomes innocuous once you understand what you're looking at, because many of the elements are universal to all Views and can come to be expected. (If you don't find yourself taking to the naming conventions automatically given by Drupal, there is a very neat module called Semantic Views, which allows you to change the long and winding id's and class names to whatever you like!)
JQUERY TOOLS
Now, if you are a glutton for punishment and are actually attempting to try to build the jQuery scrollable gallery while following along, then you will have likely noticed a problem. While Views gives you plenty of elements, you don't actually have both of the div's you need surrounding the thumbnail fields (scrollable images) - you only have one. But the real issue arises when you realize you can't just insert those divs in your View header/footer (because they will then surround all of your fields including your large images) or in the output of a field (because the div will then be repeated with every printed field, as opposed to containing all of the items printed within that field, e.g. all thumbnails).
But never fear! One of the reasons Views is so powerful is because you have the power to override the output on a more basic theming level. By clicking on Theme Information in your View, you will find an assortment of templates being used to construct your view, from the overarching display templates to the specific field templates themselves. If you haven't learned these already, I encourage you to open all of them up and take a look at the output. These give you nearly limitless control over your View output, and it helps to know what information each of these templates delivers, especially for our simple goal here (inserting an extra containing div). Once you've found the correct template and have inserted the div, you then save the new template in your local directory with one of the more specific naming conventions offered via the Theme Information. Voila!
THAT'S IT!
The hard work is over, or at least it should be. Once your View is constructed properly and your HTML matches that needed by the jQuery tool, it is merely a matter of making sure the correct elements are being referenced by the script handler in the 3rd step. If linked correctly, jQuery should be doing it's magic, and you should have a lovely display of images scrolling through content which is added via your CMS. A word of caution here though. Before attempting to delve too deep into a project, run a simple test to make sure jQuery and your specific tool(s) are running correctly. Although I have successfully implemented various jQuery tools with Views using the method above, I have also run into issues with clashing scripts, particularly with the Tabs script as it concerns Drupal (it appears Drupal possibly uses a similar version of that script elsewhere and therefore has trouble implementing both simultaneously). In this scenario, it would be helpful to have a bit more advanced knowledge of jQuery and javascript in order to track down and possibly work around this clash.
In closing, I feel the need to stress that this is not the average cut-and-dry tutorial kind of material. While it may leave you frustrated, especially if you're attempting to throw together a View using jQuery Tools on a limited timeline, you should by now realize why I wanted to cover the conceptual aspects before trying to delve into a screencast or other form of step-by-step instructions. Anyway, I really hope this has been helpful for you in your efforts to tie these two great systems together into powerful and even beautiful displays.
Add new comment