Ajax Comment Pagination & Overriding Default Views as a Feature

Ajax Comment Pagination

You know the biggest problem with comments? That’s right, getting them to page using ajax so that users watching videos don’t have to reload the page. Good answer!

So how does one go about making this happen in Drupal 6? Very easily actually. One simply makes their comments load in a view, and uses Views built in ajax to handle the pagination.

In Drupal 6, using the module CommentBlock will handle all the heavy lifting for you by providing a view for comments as a block, and preventing the output of comments on node pages.

So to start, download and install that module, then place the Comments and Comment Form blocks it provides in the content region of your site.

Next, visit the views page and edit the Comments view with the following settings:

  • Use Ajax: Yes
  • Use Pager: Yes
  • Items to display: 50 (or however many you want)

Save the view and voila! you now have comments that page using ajax.

Wow! you say? That’s so awesome I want to use it on all my sites!, you say? No problem.

Enter Features

If you’re interested in reusing site aspects you create on other sites, you must learn the Features module. It can package different components of your site into a downloadable and easily deploy-able psuedo-module for you.

Additionally, grab the Strongarm module. It lets you save drupal variables into features, and in this specific case is necessary.

After installing those modules visit your feature’s page (Site Building » Features) and create a new feature. Name the feature, describe it and give it a version number, then add the following components.

  • Dependencies: CommentBlock, Views
  • Strongarm: clean_url

The reason we added the clean_url variable is because features that only have dependencies don’t show up correctly on the features page.

Click Download, and save the feature you created.

Overriding default views in a Feature using hook_views_default_views_alter()

This is where it gets a little tricky. Since the commentblock module provides the Comments view as a default view within the module, we can’t export the modified view into a feature, we’re going to have to override that default view with code. Lucky us, one great thing about features is that you can add real module code into them!

First, go back to the comments view you edited earlier and click on ‘Export’. Just keep that page open.

In your freshly created and downloaded feature, open the module file in your favorite editor, and add the following at the bottom of the file.


/*
 * Implementation of hook_views_default_views_alter()
 * 
 * Override the view provided by commentblock module
 * http://drupal.org/node/1014774
 */
function FEATURENAME_views_default_views_alter(&$views){
  // Alter only the 'comments' view.
  if (array_key_exists('comments', $views))
  {
    /*
     * How To:
     *
     * 1) Install the commentblock module and edit the view provided as desired (comments)
     * 2) Export the edited view and paste the code below
     * 3) Cache flushing likely needed
     */

    // paste exported view here.

    /*
     * Stop.  Do not remove or change
     */
    // Override the existing view with this new definition.
    $views['comments'] = $view;
  }
}

Now simply go back to the page where you have exported the edited Comments view and copy all of the code provided in the textarea.

Then paste that code in the feature function above where it says ‘paste exported view here.

Note: You’ll need to replace the FEATURENAME in the function above with the folder name of your feature.

Save that file, and there you have it. Now to have ajax comment pagination on any other drupal 6 websites you need only to have the modules commentblock, views, features, and strongarm, along with your newly created feature.

I know that’s a lot of steps crammed into a small blog post, but if you don’t need to reuse the feature on other sites you only need to follow the first section.

Happy hunting!

0 Thoughts

Discussion

Leave a Reply

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