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.

[pastacode lang=”php” manual=”%0A%2F*%0A%20*%20Implementation%20of%20hook_views_default_views_alter()%0A%20*%20%0A%20*%20Override%20the%20view%20provided%20by%20commentblock%20module%0A%20*%20http%3A%2F%2Fdrupal.org%2Fnode%2F1014774%0A%20*%2F%0Afunction%20FEATURENAME_views_default_views_alter(%26%24views)%7B%0A%20%20%2F%2F%20Alter%20only%20the%20’comments’%20view.%0A%20%20if%20(array_key_exists(‘comments’%2C%20%24views))%0A%20%20%7B%0A%20%20%20%20%2F*%0A%20%20%20%20%20*%20How%20To%3A%0A%20%20%20%20%20*%0A%20%20%20%20%20*%201)%20Install%20the%20commentblock%20module%20and%20edit%20the%20view%20provided%20as%20desired%20(comments)%0A%20%20%20%20%20*%202)%20Export%20the%20edited%20view%20and%20paste%20the%20code%20below%0A%20%20%20%20%20*%203)%20Cache%20flushing%20likely%20needed%0A%20%20%20%20%20*%2F%0A%0A%20%20%20%20%2F%2F%20paste%20exported%20view%20here.%0A%0A%20%20%20%20%2F*%0A%20%20%20%20%20*%20Stop.%20%20Do%20not%20remove%20or%20change%0A%20%20%20%20%20*%2F%0A%20%20%20%20%2F%2F%20Override%20the%20existing%20view%20with%20this%20new%20definition.%0A%20%20%20%20%24views%5B’comments’%5D%20%3D%20%24view%3B%0A%20%20%7D%0A%7D” message=”” highlight=”” provider=”manual”/]

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


Leave a Reply

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