Drupal: Views Ajax

Professional Article
This is an OLD POST...
October 24, 2012

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.

  1. /**
  2.  * Implements hook_views_pre_render().
  3.  */
  4. function campaigns_feature_views_pre_render(&$view) {
  5. if ($view->name == 'active_campaigns' && $view->current_display == 'panel_pane_sidebar') {
  6. drupal_add_js(drupal_get_path('module', 'campaigns_feature') . '/js/active_campaigns.ui.js', array('group' => JS_THEME));
  7. }
  8. }

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

  1. (function($) {
  2. Drupal.behaviors.activeCampaigns = {
  3. attach: function(context, settings) {
  4. // First we use the settings object to get the views' details
  5. var views = settings.views.ajaxViews;
  6.  
  7. // view_dom_id is an unique value, it's unique per view and per pageload
  8. var view = view_dom_id = view_name = undefined;
  9. for (var viewsName in views) {
  10. if (views.hasOwnProperty(viewsName)) {
  11. view = views[viewsName];
  12. // I used view_display_id to actually identify my view
  13. // and store it in the view variable
  14. if (view['view_display_id'] == "pane_active_camp_users") {
  15. view_name = viewsName;
  16. view_dom_id = view["view_dom_id"];
  17. }
  18. }
  19. }
  20.  
  21. // Then we use view_name (which is views' key that contains unique view_dom_id)
  22. // to do our manipulations
  23. var instance = Drupal.views.instances[view_name];
  24. // Then we pass through all of the left-side views items
  25. $('.pane-active-campaigns-panel-pane-sidebar .views-row', context).each(function(index, element) {
  26. var $this = $(element),
  27. viewData = {},
  28. nid = $('.views-field-nid .field-content', $this).text();
  29. // Load the argument (it's actually hidden in the view, not excluded)
  30. instance.settings['view_args'] = nid;
  31. // This is the way we update the viewData object with defaults and
  32. // later with overridden values
  33. $.extend(viewData, instance.settings);
  34. instance.element_settings.submit = viewData;
  35. // Create the Drupalish ajax call
  36. new Drupal.ajax(false, $this, instance.element_settings);
  37. });
  38.  
  39. }
  40. }
  41. })(jQuery);

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.

Comments:

Feel free to ask any question / or share any suggestion!