Propeople Blog

A regular dose of fresh news, events, products, Drupal development resources and more.

October 23, 2012

Drupal: Views Ajax

Propeople developers sleep and code and then go back in the circle in order to bring to life beautiful projects.

Of course they also do a lot of other fun geeky stuff, but the point is that they do contribute to the Drupal community, as this is the idea of a community in the first place - to get knowledge and to give it back in an improved form.

We have started a series of articles provided by our developers and each time there is a chance to share something they have, we post it here as well.

So here it goes, this time it is about Drupal Views and the Ajax functionality they have - hence getting through to know its functionality will definitely help Drupal developers achieve their goals.

This article is provided by Sergiu Nagailic.

Most of us know that Views module has its own Ajax functionality. If we enable the ajax checkbox in the view settings, a bunch of Ajax-related JavaScript files will be loaded for us on our views' page.

One of the files which will be loaded is: /views/js/ajax_view.js

If we drill down this file and analyze it, we will understand some of the Views' ajax functionality. Lets take a case and see how we can reuse Views' Ajax.

Reloading a View with specific arguments via Ajax:
Lets say we have 2 views on the same page, for instance we use Panels and we have a left-side view and a content view. The content view is empty on the first page load, but when user selects something in the left-side view we want to reload dynamically the content view with specific arguments passed to it.

First idea that came to my head is to use my custom JavaScript script that will take some arguments onclick from the left-side view row, will make a custom Ajax call to one of my modules' callback, that will use views_embed_view to render the new output and use drupal json to output it back to the page. Then I'd simply replace the contents of the view and we're done.

But I kept thinking that declaring a custom callback, processing functions (and where are callback functions there are access functions) and custom ajax calls is a wrong way, because Views module handles all of those by default.

All of the Ajax-enabled views instances are stored in Drupal.views.instances and all of them are of Drupal.views.ajaxView type. We can use these instances to make the drupalish ajax calls which will update the views in the right way.

But first we need to inject our JavaScript into a specific view. We can use preprocessing functions but I'd like to use my .module file and hook_views_pre_render(). One interesting thing to notice here, make sure your JavaScript file is loaded after ajax_views.js, this is why we used JS_THEME group.

Once the JavaScript is loaded, we can proceed to implementation.

You can tweak viewData and/or instance.element_settings how you want (for instance to load the view onmouseover event) to achieve the right functionality. And we all can forget about writing custom callbacks and just re-using Views' Ajax functionality.

0 Responses to this post